在openCV中访问某些像素RGB值

Accessing certain pixel RGB value in openCV(在openCV中访问某些像素RGB值)
本文介绍了在openCV中访问某些像素RGB值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经彻底搜索了互联网和 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值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)