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?
>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.