Get Pixel Mat :: at

I am trying to get a pixel from a Mat object. To check, I try to draw a diagonal line on a square and expect to get a perfect line, crossing the upper left and right vertices down.

for (int i =0; i<500; i++){ //I just hard-coded the width (or height) to make the problem more obvious (image2.at<int>(i, i)) = 0xffffff; //Draw a white dot at pixels that have equal x and y position. } 

The result, however, is not as expected. Here is the diagonal line drawn in the color picture. enter image description here Here it is in the halftone picture. enter image description here Does anyone see a problem?

+4
source share
2 answers
 (image2.at<int>(i, i)) = 0xffffff; 

It looks like your color image is 24 bits, but your address pixels are in int terms that seem 32 bits.

+3
source

The problem is that you are trying to access each pixel as an int (32-bit image per pixel), while your image is a three-channel unsigned char (24-bit image per pixel) or a single-channel unsigned char (8 bit per pixel image) for grayscale. You can try to access every pixel like this for a shade of gray.

 for (int i =0; i<image2.width; i++){ image2.at<unsigned char>(i, i) = 255; } 

or how is it for color

 for (int i =0; i<image2.width; i++){ image2.at<Vec3b>(i, i)[0] = 255; image2.at<Vec3b>(i, i)[1] = 255; image2.at<Vec3b>(i, i)[2] = 255; } 
+6
source

All Articles