C++ OpenCV 从 Mat 图像中获取像素通道值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7899108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV get pixel channel value from Mat image
提问by HRóDóLFR
Maybe I'm not looking hard enough, but everything seems to want me to use an array. Thus, how do I get the channel value for a particular pixel for foo if foo is something like Mat foo = imread("bar.png")
?
也许我看起来不够努力,但似乎一切都希望我使用数组。因此,如果 foo 类似于 ,我如何获得 foo 特定像素的通道值Mat foo = imread("bar.png")
?
回答by mevatron
Assuming the type is CV_8UC3 you would do this:
假设类型是 CV_8UC3 你会这样做:
for(int i = 0; i < foo.rows; i++)
{
for(int j = 0; j < foo.cols; j++)
{
Vec3b bgrPixel = foo.at<Vec3b>(i, j);
// do something with BGR values...
}
}
Hereis the documentation for Vec3b. Hope that helps! Also, don't forget OpenCV stores things internally as BGRnot RGB.
这是 Vec3b 的文档。希望有帮助!另外,不要忘记 OpenCV 在内部将事物存储为BGR而不是 RGB。
EDIT :
For performance reasons, you may want to use direct access to the data buffer in order to process the pixel values:
编辑:
出于性能原因,您可能希望使用对数据缓冲区的直接访问来处理像素值:
Here is how you might go about this:
以下是您可能会如何处理此问题:
uint8_t* pixelPtr = (uint8_t*)foo.data;
int cn = foo.channels();
Scalar_<uint8_t> bgrPixel;
for(int i = 0; i < foo.rows; i++)
{
for(int j = 0; j < foo.cols; j++)
{
bgrPixel.val[0] = pixelPtr[i*foo.cols*cn + j*cn + 0]; // B
bgrPixel.val[1] = pixelPtr[i*foo.cols*cn + j*cn + 1]; // G
bgrPixel.val[2] = pixelPtr[i*foo.cols*cn + j*cn + 2]; // R
// do something with BGR values...
}
}
Or alternatively:
或者:
int cn = foo.channels();
Scalar_<uint8_t> bgrPixel;
for(int i = 0; i < foo.rows; i++)
{
uint8_t* rowPtr = foo.row(i);
for(int j = 0; j < foo.cols; j++)
{
bgrPixel.val[0] = rowPtr[j*cn + 0]; // B
bgrPixel.val[1] = rowPtr[j*cn + 1]; // G
bgrPixel.val[2] = rowPtr[j*cn + 2]; // R
// do something with BGR values...
}
}
回答by Yoda
The below code works for me, for both accessing and changing a pixel value.
下面的代码适用于我,用于访问和更改像素值。
For accessing pixel's channel value :
访问像素的通道值:
for (int i = 0; i < image.cols; i++) {
for (int j = 0; j < image.rows; j++) {
Vec3b intensity = image.at<Vec3b>(j, i);
for(int k = 0; k < image.channels(); k++) {
uchar col = intensity.val[k];
}
}
}
For changing a pixel value of a channel :
改变一个通道的像素值:
uchar pixValue;
for (int i = 0; i < image.cols; i++) {
for (int j = 0; j < image.rows; j++) {
Vec3b &intensity = image.at<Vec3b>(j, i);
for(int k = 0; k < image.channels(); k++) {
// calculate pixValue
intensity.val[k] = pixValue;
}
}
}
`
`
Source : Accessing pixel value
来源:访问像素值
回答by Derzu
The pixels array is stored in the "data" attribute of cv::Mat. Let's suppose that we have a Mat matrix where each pixel has 3 bytes (CV_8UC3).
像素数组存储在 cv::Mat 的“data”属性中。假设我们有一个 Mat 矩阵,其中每个像素有 3 个字节 (CV_8UC3)。
For this example, let's draw a RED pixel at position 100x50.
在这个例子中,让我们在 100x50 的位置绘制一个红色像素。
Mat foo;
int x=100, y=50;
Solution 1:
解决方案1:
Create a macro function that obtains the pixel from the array.
创建一个宏函数,从数组中获取像素。
#define PIXEL(frame, W, x, y) (frame+(y)*3*(W)+(x)*3)
//...
unsigned char * p = PIXEL(foo.data, foo.rols, x, y);
p[0] = 0; // B
p[1] = 0; // G
p[2] = 255; // R
Solution 2:
解决方案2:
Get's the pixel using the method ptr.
使用方法 ptr 获取像素。
unsigned char * p = foo.ptr(y, x); // Y first, X after
p[0] = 0; // B
p[1] = 0; // G
p[2] = 255; // R