web_ninja
web_ninja

Reputation: 2401

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

This snippet of code prints out garbage values.

cv::Mat_<cv::vec3b>::iterator it = img.begin<cv::vec3b>(),
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(100,200);

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

Any reason why this might be happening?

Upvotes: 0

Views: 195

Answers (1)

user1502952
user1502952

Reputation: 1420

I'm not sure about OpenCV but if each of your element in vector is of format say [221,123,321], then individually splitting string based on ',' seperator you can retrieve individual pixel values.

What i understand is this:

 #include<sstream>

 string buf;

 cout<<"Printing elements:"<<endl;

 for(unsigned i=0;i<img.size();i++)
 {
   istringstream is(img[i]);

   while(getline(is, buf, ','))
   {
     if(buf.at(0)=='[')
     {
       buf.erase(0, 1);
     }
     if(buf.at(buf.length()-1)==']')
     {
       buf.erase(buf.length()-1, buf.length());
     }
     cout<<buf<<endl;
   }  
 }

OUTPUT

Printing elements:

221

123

321

...

...

Upvotes: 1

Related Questions