在openCV中访问某些像素RGB值

2023-01-21C/C++开发问题
4

本文介绍了在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值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6