Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using img.at to set the color of individual pixels in OpenCV C++

I am relatively new to OpenCV. I want to set the colour of individual pixels using img.at
I also kind of understand cv::Scalar, for example cv::Scalar(0,255,0) is green, cv::Scalar(255,0,0) is blue

I tried the following code

int main( void ){
  cv::Mat img(400,400, CV_8UC3);

  for(int i=0; i < 400; ++i){
    for(int j=0; j < 400; ++j){
      img.at<cv::Scalar>(i,j) = cv::Scalar(0,255,0);
    }
  }

  cv::imshow("Tetris", img);
  cv::moveWindow("Tetris",400,400);

  cv::waitKey(0);

}

It compiles without error messages, but if I launch the program, nothing is visible at all.
How do you set the colour of an indiviual pixel to red, green or blue?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your cv::Mat type is CV_8UC3, meaning it contains 3 channels, each of them a uchar.

But cv::Scalar contains 4 elements, and by default each of them is a double.

cv::Vec3b is a convenient type to represent an RGB value, where each channle is a uchar.

Therefore instead of:

img.at<cv::Scalar>(i,j) = cv::Scalar(0,255,0);

Use:

//-----vvvvvvvvv----------vvvvvvvvv------------
img.at<cv::Vec3b>(i, j) = cv::Vec3b(0, 255, 0);

A side note:
Using cv::Mat::at in such a tight loop to set the pixels of the entire image is not very efficient (due to some validations performed in each at call).
A better approach would be to iterate over the rows, and for each row iterate via a pointer to the beginning of the row with cv::Mat::ptr.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading