Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. The type is 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? 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. The reason i put not condition is to ease the work of getting the correction done. THank u.

like image 855
rish Avatar asked Jul 08 '26 17:07

rish


1 Answers

The way you are indexing image is not right. 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. Instead you should be indexing image using .at<Vec3b>.

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:

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);
like image 121
Danny Avatar answered Jul 14 '26 07:07

Danny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!