Read HSV value of pixel in opencv

别来无恙 提交于 2019-12-02 06:43:46

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<cv::Vec3b>(0,0); // read pixel (0,0) (make copy)
pixel[0] = 0; // H
pixel[1] = 0; // S
pixel[2] = 0; // V
image.at<cv::Vec3b>(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_<cv::Vec3b> it = image.begin<cv::Vec3b>(),
                    it_end = image.end<cv::Vec3b>();
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
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!