1
votes

I am trying to set RGB values of some pixels in a binary image. But whenever the coordinate is more than (89, 89) its giving me an assertion error! My image resolution is okay because I am accessing RGB values from (150, 150) coordinate. If the coordinate is (89, 89) or less it works fine. My Code:

cv::Mat img_gray, img_bw;
//read an image
cv::Mat3b img_bgr = cv::imread("test.jpg");
cv::imshow("Original Image", img_bgr);

//conversion to binary from color
cv::cvtColor(img_bgr, img_gray,CV_RGB2GRAY);
cv::threshold(img_gray, img_bw, 75.0, 255.0, THRESH_BINARY);

//accessing BGR of position (150, 150) from a color image
std::cout<<"Pixel at position (x, y) : ("<<150<<", "<<150<<") ="<<img_bgr(150,150)<<std::endl;  

//Setting BGR of position (150, 150) in binary image
img_bw.at<Vec3b>(150, 150)[0] = 255;
img_bw.at<Vec3b>(150, 150)[1] = 255;
img_bw.at<Vec3b>(150, 150)[2] = 255;
std::cout<<"Pixel at position (x, y) : ("<<150<<", "<<150<<") ="<<img_bw.at<Vec3b>(150, 150)<<std::endl;

Here if I put 89 instead of 150 in the "Setting BGR" section then it works. Otherwise the full error is:

OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p1*channels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file e:\opencv\opencv\build\include\opencv2\core\mat.hpp, line 538

So is this any type of memory space error? Thanks in advance for the helps! :)

UPDATE: I've tried it this way! But the output is blank now.

cv::Mat img_gray, img_bw;
//read an image
cv::Mat3b img_bgr = cv::imread("test.jpg");
cv::imshow("Original Image", img_bgr);

//conversion to binary from color
cv::cvtColor(img_bgr, img_gray,CV_RGB2GRAY);
cv::threshold(img_gray, img_bw, 75.0, 255.0, THRESH_BINARY);

//accessing BGR of position (150, 150) from a color image
std::cout<<"Pixel at position (x, y) : ("<<150<<", "<<150<<") ="<<img_bgr(150,150)<<std::endl;  

//Setting BGR of position (150, 150) in binary image
img_bw.at<uchar>(150, 150) = 255;
std::cout<<"Pixel at position (x, y) : ("<<150<<", "<<150<<") ="<<img_bw.at<uchar>(150, 150)<<std::endl;

My test image is here

This is my test image here

And the output is here

Output image here

1
Your img_bw is of type gray. So you cannot access any pixel by typing img_bw.at<Vec3b>. You should use instead img_bw.at<ucahr>(150,150) = 255alex
@alex I tried that already. In that case my std::cout<<img_bw.at<ucahr>(150,150) is simply blank!Tousif Zaman
@TousifZaman We cannot fix code you do not show us. You know that using Vec3b is incorrect. If you show us your code using uchar along with the error and your input image, perhaps we can help you.beaker
@TousifZaman Updates should be added to the question itself using edit, not in the comments. We also still don't have your input image, so we cannot reproduce your error.beaker
You're printing out a <uchar> with a value of 0 or 255. Neither of these (in ascii or utf-8) are printable characters. Try assigning the pixel value to an int variable and then printing.beaker

1 Answers

0
votes

Okay I have the answer now thanks to beaker's hint in the comment :) I'm putting the solution for others help!

I just need to define the output as an integer. So the last cout will be like

std::cout<<"Pixel at position (x, y) : ("<<150<<", "<<150<<") ="<<(int)img_bw.at<uchar>(150, 150)<<std::endl;