Ask Your Question
0

Why am I getting garbage value instead of RGB while printing out colour of pixels?

asked 2013-09-11 05:35:22 -0600

debidatta gravatar image

updated 2013-09-11 06:02:27 -0600

Moster gravatar image

This snippet of code prints out garbage values.

cv::Mat_<cv::Vec3b>::iterator it = img.begin<cv::Vec3b>();
cv::Mat_<cv::Vec3b>::iterator itEnd = img.end<cv::Vec3b>();

for(; it != itEnd; ++it)
   std::cout << (*it)[1] << std::endl;

But this prints out the vector of RGB values.

std::cout << img.at<cv::Vec3b>(100,200);

The moment I try to access the individual RGB values I get garbage values printed,

Any reason why this might be happening?

edit retag flag offensive close merge delete

Comments

1

Made it more readable.

Moster gravatar imageMoster ( 2013-09-11 06:02:59 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
4

answered 2013-09-11 07:47:48 -0600

Moster gravatar image

The solution is actually so simple :D The problem is that cout tries to print out a symbol for uchar (maybe ascii?) and not a number. A simple cast to int solves it

 for(; it != itEnd; ++it){
     std::cout << (int)(*it)[1] << std::endl;
 }
edit flag offensive delete link more

Comments

I had type casted but then stored it in uchar which gives the ascii equivalent while printing. Thanks.

debidatta gravatar imagedebidatta ( 2013-09-11 10:59:42 -0600 )edit
1

answered 2013-09-11 07:04:54 -0600

SR gravatar image

Most likely cv::Vec3b has no overloaded ostream& operator << (ostream& os) const; member. Thus, it is unclear how it should be serialized to a stream and its address (not sure) is written to the stream.

edit flag offensive delete link more

Comments

But its element val[0] has << overloaded. So it should give the RGB colour. People have extensively used this method to print RGB values of pixels.

debidatta gravatar imagedebidatta ( 2013-09-11 07:48:10 -0600 )edit
1

Technically img.at&lt;cv::Vec3b&gt;(100,200) gives you a 1x3 vector of type cv::Vec3b and not a single scalar.

SR gravatar imageSR ( 2013-09-11 09:26:30 -0600 )edit

Yup that was my mistake. Type casting solved the issue.

debidatta gravatar imagedebidatta ( 2013-09-11 10:58:15 -0600 )edit

Question Tools

Stats

Asked: 2013-09-11 05:35:22 -0600

Seen: 1,511 times

Last updated: Sep 11 '13