c++和opencv获取并设置像素颜色到Mat。

时间:2022-02-07 20:05:00

I'm trying to set a new color value to some pixel into a cv::Mat image my code is below:

我试着将一个新的颜色值设置成cv::Mat图像我的代码如下:

    Mat image = img;
    for(int y=0;y<img.rows;y++)
    {
        for(int x=0;x<img.cols;x++)
        {
        Vec3b color = image.at<Vec3b>(Point(x,y));
        if(color[0] > 150 && color[1] > 150 && color[2] > 150)
        {
            color[0] = 0;
            color[1] = 0;
            color[2] = 0;
            cout << "Pixel >200 :" << x << "," << y << endl;
        }
        else
        {
            color.val[0] = 255;
            color.val[1] = 255;
            color.val[2] = 255;
        }
    }
    imwrite("../images/imgopti"+to_string(i)+".tiff",image);

It seems to get the good pixel in output (with cout) however in the output image (with imwrite) the pixel concerned aren't modified. I have already tried using color.val[0].. I still can't figure out why the pixel colors in the output image dont change. thanks

在输出图像中(使用cout),它似乎得到了良好的像素,但在输出图像中(使用imwrite),有关的像素没有被修改。我已经试过使用color.val[0]..我仍然不明白为什么输出图像中的像素颜色不会改变。谢谢

3 个解决方案

#1


44  

You did everything except copy the new pixel value back to the image.

除了将新像素值复制到图像之外,你做了所有事情。

This line takes a copy of the pixel into a local variable:

这一行将像素复制到一个局部变量中:

Vec3b color = image.at<Vec3b>(Point(x,y));

So, after changing color as you require, just set it back like this:

所以,在改变颜色之后,就像这样把它放回去:

image.at<Vec3b>(Point(x,y)) = color;

So, in full, something like this:

所以,就这样,

Mat image = img;
for(int y=0;y<img.rows;y++)
{
    for(int x=0;x<img.cols;x++)
    {
        // get pixel
        Vec3b color = image.at<Vec3b>(Point(x,y));

        // ... do something to the color ....

        // set pixel
        image.at<Vec3b>(Point(x,y)) = color;
    }
}

#2


12  

just use a reference:

只使用一个参考:

Vec3b & color = image.at<Vec3b>(y,x);
color[2] = 13;

#3


8  

I would not use .at for performance reasons.

由于性能原因,我不会使用。

Define a struct:

定义一个结构体:

//#pragma pack(push, 2) //not useful (see comments below)
struct RGB {
    uchar blue;
    uchar green;
    uchar red;  };

And then use it like this on your cv::Mat image:

然后在你的简历上使用它::

RGB& rgb = image.ptr<RGB>(y)[x];

image.ptr(y) gives you a pointer to the scanline y. And iterate through the pixels with loops of x and y

图像。ptr(y)给你一个指向扫描线y的指针,并通过x和y的循环遍历像素。

#1


44  

You did everything except copy the new pixel value back to the image.

除了将新像素值复制到图像之外,你做了所有事情。

This line takes a copy of the pixel into a local variable:

这一行将像素复制到一个局部变量中:

Vec3b color = image.at<Vec3b>(Point(x,y));

So, after changing color as you require, just set it back like this:

所以,在改变颜色之后,就像这样把它放回去:

image.at<Vec3b>(Point(x,y)) = color;

So, in full, something like this:

所以,就这样,

Mat image = img;
for(int y=0;y<img.rows;y++)
{
    for(int x=0;x<img.cols;x++)
    {
        // get pixel
        Vec3b color = image.at<Vec3b>(Point(x,y));

        // ... do something to the color ....

        // set pixel
        image.at<Vec3b>(Point(x,y)) = color;
    }
}

#2


12  

just use a reference:

只使用一个参考:

Vec3b & color = image.at<Vec3b>(y,x);
color[2] = 13;

#3


8  

I would not use .at for performance reasons.

由于性能原因,我不会使用。

Define a struct:

定义一个结构体:

//#pragma pack(push, 2) //not useful (see comments below)
struct RGB {
    uchar blue;
    uchar green;
    uchar red;  };

And then use it like this on your cv::Mat image:

然后在你的简历上使用它::

RGB& rgb = image.ptr<RGB>(y)[x];

image.ptr(y) gives you a pointer to the scanline y. And iterate through the pixels with loops of x and y

图像。ptr(y)给你一个指向扫描线y的指针,并通过x和y的循环遍历像素。