问题描述
我已经彻底搜索了互联网和 stackoverflow,但我还没有找到我的问题的答案:
I have searched internet and stackoverflow thoroughly, but I haven't found answer to my question:
如何在 OpenCV 中获取/设置(两个)某些(由 x,y 坐标给出)像素的 RGB 值?重要的是 - 我用 C++ 编写,图像存储在 cv::Mat 变量中.我知道有一个 IplImage() 操作符,但 IplImage 使用起来不是很舒服——据我所知它来自 C API.
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.
是的,我知道已经有这个 像素访问OpenCV 2.2 线程,但它只是关于黑白位图.
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.
非常感谢您的所有回答.我看到有很多方法可以获取/设置像素的 RGB 值.我从我的密友那里又得到了一个想法——谢谢 Benny!这是非常简单和有效的.我认为这取决于您选择哪种口味.
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);
然后你可以读/写 RGB 值:
And then you can read/write RGB values with:
p->x //B
p->y //G
p->z //R
推荐答案
尝试以下操作:
cv::Mat image = ...do some stuff...;
image.at<cv::Vec3b>(y,x);
为您提供 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值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!