0

I try to do a loop over a new zero matrix and change every pixel to white.

cv::Mat background = cv::Mat::zeros(frame.rows, frame.cols,frame.type());

for (int i=0; i<frame.rows; i++)
{
    for (int j=0; j<frame.cols; j++)
    {
        background.at<char>(i,j)=255;
    }
}

Normally at the end i have to have a matrix totally white But i don't understand why finally i have this picture:

enter image description here

Thanks

EDIT: solution:

cv::Mat background = cv::Mat::zeros(frame.rows, frame.cols,frame.type());

for (int i=0; i<frame.rows; i++)
{
    for (int j=0; j<frame.cols; j++)
    {

         Vec3b bgrPixel = Vec3b(255,255,255);
         background.at<Vec3b>(i,j)=bgrPixel;

    //    background.at<char>(i,j)=255;


    }
}

Thank you !

lilouch
  • 1,054
  • 4
  • 23
  • 43
  • What is expected output ? – Emadpres Jan 12 '15 at 20:51
  • Totally white as you can see on my code. – lilouch Jan 12 '15 at 20:54
  • 1
    Uh you only seem to be setting 1 of 3 (or 4) channels to white. Which to me explains why only 1/3 of the image is white. You probably end up setting all 3 channels for 1/3 of the pixels. – Borgleader Jan 12 '15 at 20:55
  • I don't understand... Can you explain me again ? What i have to do ? However i saw on the internet that in order to change the pixel, i have to do this. – lilouch Jan 12 '15 at 20:56
  • 1
    Your matrix is made up of i\*j pixels - each pixel is made up of 3 (RGB) or 4 (RGBA) chars (bytes/channels). You are only looping over the first i\*j bytes of the matrix, when you need to be looping over i*j pixels. I'm guessing whatever type you're passing in as the third argument is the 'pixel type'. Look here for an example usage: http://stackoverflow.com/questions/7899108/opencv-get-pixel-information-from-mat-image Edit: made my comment an answer since I think it should do the trick. – mbgda Jan 12 '15 at 21:02

1 Answers1

1

Your matrix is made up of i*j pixels - each pixel is made up of 3 (RGB) or 4 (RGBA) chars (bytes/channels). You are only looping over the first i*j bytes of the matrix, when you need to be looping over i*j pixels. I'm guessing whatever type you're passing in as the third argument is the 'pixel type'.

Look here for an example usage: OpenCV get pixel channel value from Mat image

Community
  • 1
  • 1
mbgda
  • 787
  • 5
  • 8