访问openCV中的某些像素RGB值 [英] Accessing certain pixel RGB value in openCV
问题描述
我已经搜索互联网和stackoverflow彻底,但我没有找到我的问题的答案:
如何获得/设置(由x,y坐标)像素在OpenCV?重要的是 - 我用C ++编写,图像存储在cv :: Mat变量中。我知道有一个IplImage()运算符,但IplImage使用不是很舒服 - 就我所知它来自C API。
是的,我知道已经有这个 OpenCV 2.2 中的像素访问线程,
非常感谢您的答案。我看到有很多方法来获取/设置像素的RGB值。我从我的亲密朋友得到了一个想法 - 感谢本尼!它非常简单和有效。我认为这是一个你选择的品味问题。
(...)
Point3_< uchar> * p = image.ptr< Point3_< uchar> >(y,x);
然后您可以用以下命令读取/写入RGB值:
p-> x // B
p-> y // G
p-> z // R
$ $ cv :: Mat image = ...做一些东西...;
image.at< cv :: Vec3b>(y,x)
为您提供 cv :: Vec3b
类型的RGB(可能为BGR)向量
image.at< cv :: Vec3b>(y,x)[0] = newval [0]
image.at< cv :: Vec3b>(y,x)[1] = newval [1];
image.at< cv :: Vec3b>(y,x)[2] = newval [2];
I have searched internet and stackoverflow thoroughly, but I haven't found answer to my question:
How can I get/set (both) RGB value of certain (given by x,y coordinates) pixel in OpenCV? What's important-I'm writing in C++, the image is stored in cv::Mat variable. I know there is an IplImage() operator, but IplImage is not very comfortable in use-as far as I know it comes from C API.
Yes, I'm aware that there was already this Pixel access in OpenCV 2.2 thread, but it was only about black and white bitmaps.
EDIT:
Thank you very much for all your answers. I see there are many ways to get/set RGB value of pixel. I got one more idea from my close friend-thanks Benny! It's very simple and effective. I think it's a matter of taste which one you choose.
Mat image;
(...)
Point3_<uchar>* p = image.ptr<Point3_<uchar> >(y,x);
And then you can read/write RGB values with:
p->x //B
p->y //G
p->z //R
解决方案 Try the following:
cv::Mat image = ...do some stuff...;
image.at<cv::Vec3b>(y,x);
gives you the RGB (it might be ordered as BGR) vector of type cv::Vec3b
image.at<cv::Vec3b>(y,x)[0] = newval[0];
image.at<cv::Vec3b>(y,x)[1] = newval[1];
image.at<cv::Vec3b>(y,x)[2] = newval[2];
这篇关于访问openCV中的某些像素RGB值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!