問題描述
我已經徹底搜索了互聯網和 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值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!