OpenCV 2.x中的像素访问错误

时间:2022-09-11 23:45:55

I'm having trouble trying to find out how to access rgb pixel in the new version (2.x) of OpenCV. I tried using a mix of the old and the new method but without success.

在OpenCV的新版本(2.x)中,我很难找到访问rgb像素的方法。我试着将旧方法和新方法混合使用,但没有成功。

Here is my code

这是我的代码

#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\highgui\highgui.hpp>

using namespace cv;
using namespace std;

int main (int argc, char* argv[])
{
Mat img;


string winMain = "Main";

img = imread(argv[1]);

for (int j = 0; j < img.rows; j++)
{
    for (int i = 0; i < img.cols; i++)
    {
        img.data[j * img.cols + i * 3 + 0] = (uchar)0; //B
        //img.data[j * img.cols + i + 1] = (uchar)0; //G
        //img.data[j * img.cols + i + 2] = (uchar)0; //R
    }
}

namedWindow(winMain);

imshow(winMain, img);

waitKey();  

return 1;
}

As you can notice in the following example, only a third of the image is modified.

正如您在以下示例中所注意到的,只有三分之一的图像被修改。

Link to example

链接到的例子

Thanks for helping

谢谢你的帮助

2 个解决方案

#1


4  

I tested out your code, and I found the bug. You multiplied the column index by 3 (i * 3), but it's also necessary to multiply the row index by 3 (j * img.cols * 3).

我测试了你的代码,发现了bug。你将列索引乘以3 (i * 3)但也需要将行索引乘以3 (j * img)。关口* 3)。

I replaced j * img.cols with j * img.cols * 3:

我替换了j * img。cols与j * img。关口* 3:

for (int j = 0; j < img.rows; j++)
{
    for (int i = 0; i < img.cols; i++)
    {
        img.data[j * img.cols * 3 + i*3 + 0] = (uchar)0; //B
        //img.data[j * img.cols * 3 + i*3 + 1] = (uchar)0; //G
        //img.data[j * img.cols * 3 + i*3 + 2] = (uchar)0; //R
    }
}

Let's try an example.

让我们来做一个例子。

Example image (from MIT pedestrian dataset):

示例图像(来自MIT行人数据集):

OpenCV 2.x中的像素访问错误

Result using OP's code:

结果使用OP的代码:

OpenCV 2.x中的像素访问错误

Result using the revised code (with j * img.cols * 3):

结果使用修改后的代码(与j * img一起)。关口* 3):

OpenCV 2.x中的像素访问错误

#2


1  

Inside your loop, you can do:

在你的圈子里,你可以这样做:

img.at<Vec3b>(j,i)[0] = 0;    // Blue Channel
img.at<Vec3b>(j,i)[1] = 0;    // Green Channel
img.at<Vec3b>(j,i)[2] = 0;    // Red Channel

Is this what you wanted or I understood incorrectly?

这是你想要的还是我不理解的?

#1


4  

I tested out your code, and I found the bug. You multiplied the column index by 3 (i * 3), but it's also necessary to multiply the row index by 3 (j * img.cols * 3).

我测试了你的代码,发现了bug。你将列索引乘以3 (i * 3)但也需要将行索引乘以3 (j * img)。关口* 3)。

I replaced j * img.cols with j * img.cols * 3:

我替换了j * img。cols与j * img。关口* 3:

for (int j = 0; j < img.rows; j++)
{
    for (int i = 0; i < img.cols; i++)
    {
        img.data[j * img.cols * 3 + i*3 + 0] = (uchar)0; //B
        //img.data[j * img.cols * 3 + i*3 + 1] = (uchar)0; //G
        //img.data[j * img.cols * 3 + i*3 + 2] = (uchar)0; //R
    }
}

Let's try an example.

让我们来做一个例子。

Example image (from MIT pedestrian dataset):

示例图像(来自MIT行人数据集):

OpenCV 2.x中的像素访问错误

Result using OP's code:

结果使用OP的代码:

OpenCV 2.x中的像素访问错误

Result using the revised code (with j * img.cols * 3):

结果使用修改后的代码(与j * img一起)。关口* 3):

OpenCV 2.x中的像素访问错误

#2


1  

Inside your loop, you can do:

在你的圈子里,你可以这样做:

img.at<Vec3b>(j,i)[0] = 0;    // Blue Channel
img.at<Vec3b>(j,i)[1] = 0;    // Green Channel
img.at<Vec3b>(j,i)[2] = 0;    // Red Channel

Is this what you wanted or I understood incorrectly?

这是你想要的还是我不理解的?