简体   繁体   English

Opencv Mat 创建图像

[英]Opencv Mat create Image

I am trying to load BGR image and than copy some pixels that certifies some condition to a new image which i created using the loaded images width, height and type.我正在尝试加载 BGR 图像,然后将一些证明某些条件的像素复制到我使用加载的图像宽度、高度和类型创建的新图像中。 The type is CV_8UC3.类型为 CV_8UC3。

Mat initial_Image = imread("image.jpg");
Mat image(img.rows,img.cols, CV_8UC3);

cout<<initial_Image.type()<<endl;

for(int i = 0;i < img.cols ;i++)
{
for(int j = 0;j < img.rows ;j++)
{
Vec3b intensity = initial_Image.at<Vec3b>(j,i);
uchar blue = intensity.val[0];
uchar green = intensity.val[1];
uchar red = intensity.val[2];


image.at<uchar>(j,i) = blue;
image.at<uchar>(j+1,i+1) = green;
image.at<uchar>(j+2,i+2) = red ;

    }
}
out.close();

imshow("im", image);

I have not placed the condition but i just tried to copy all the respective pixels from the read image into the new create image.我没有放置条件,但我只是尝试将读取图像中的所有相应像素复制到新创建的图像中。

My problem is the the read image is 3 channle RGB but when i plot the image by copying the pixel into the new created image it gives me a black and white image which is divide by 3. WHY?我的问题是读取的图像是 3 通道 RGB,但是当我通过将像素复制到新创建的图像来绘制图像时,它给了我一个除以 3 的黑白图像。为什么? Since i have to change the pixles values once i get this working based on some conditions, i cannot use copyto function as some might suggest.由于我必须根据某些条件在获得此功能后更改像素值,因此我无法像某些人建议的那样使用 copyto 功能。 The reason i put not condition is to ease the work of getting the correction done.我不设置条件的原因是为了简化完成更正的工作。 THank u.感谢你。

The way you are indexing image is not right.您索引image方式不正确。 The current way you are indexing image will write blue to index j,i , and then when you reach the next column a blue pixel will get written to j+1,i+1 effectively overwriting the green value.当前索引image会将blue写入索引j,i ,然后当您到达下一列时,蓝色像素将写入j+1,i+1从而有效地覆盖绿色值。 Instead you should be indexing image using .at<Vec3b> .相反,您应该使用.at<Vec3b>索引image

The easiest way would be to do it like this:最简单的方法是这样做:

image.at<Vec3b>(j,i) = initial_Image.at<Vec3b>(j,i);

If you want to change red , blue , and green you could also do it this way:如果你想改变redbluegreen你也可以这样做:

Vec3b intensity = initial_Image.at<Vec3b>(j,i);
uchar blue = intensity.val[0];
uchar green = intensity.val[1];
uchar red = intensity.val[2];

//Do stuff with blue, green, and red

image.at<Vec3b>(j,i) = Vec3b(blue,green,red);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM