Read HSV value of pixel in opencv

后端 未结 1 864
星月不相逢
星月不相逢 2021-01-23 23:24

how would you go about reading the pixel value in HSV format rather than RGB? The code below reads the pixel value of the circles\' centers in RGB format. Is there much differen

1条回答
  •  盖世英雄少女心
    2021-01-23 23:52

    If you use the C++ interface, you can use

    cv::cvtColor(img, img, CV_BGR2HSV);
    

    See the documentation for cvtColor for more information.

    Update:

    Reading and writing pixels the slow way (assuming that the HSV values are stored as a cv::Vec3b (doc))

    cv::Vec3b pixel = image.at(0,0); // read pixel (0,0) (make copy)
    pixel[0] = 0; // H
    pixel[1] = 0; // S
    pixel[2] = 0; // V
    image.at(0,0) = pixel; // write pixel (0,0) (copy pixel back to image)
    

    Using the image.at<...>(x, y) (doc, scroll down a lot) notation is quite slow, if you want to manipulate every pixel. There is an article in the documentation on how to access the pixels faster. You can apply the iterator method also like this:

    cv::MatIterator_ it = image.begin(),
                        it_end = image.end();
    for(; it != it_end; ++it)
    {
        // work with pixel in here, e.g.:
        cv::Vec3b& pixel = *it; // reference to pixel in image
        pixel[0] = 0; // changes pixel in image
    }
    

    0 讨论(0)
提交回复
热议问题