-1

I have the following code:

cvtColor (image, image, CV_BGRA2RGB);
Vec3b bottomRGB;
bottomRGB=image.at<Vec3b>(821,1232);

When I display bottomRGB[0], it displays a value greater than 255. What is the reason for this?

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
Khawar Ali
  • 3,462
  • 4
  • 27
  • 55
  • The most likely explanation is that your image has a higher bit depth than 8-bit (which ranges from 0-255 for unsigned int).. For details, please refer to [the documentation](http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-depth).. – scap3y Jan 27 '14 at 07:05
  • I have tried converting it into CV_8U too but to no help – Khawar Ali Jan 27 '14 at 07:08
  • Could you upload the original image (not using the default uploader but on a site where the properties can be preserved)..? – scap3y Jan 27 '14 at 07:12
  • This is the original image: http://upload.wikimedia.org/wikipedia/commons/4/48/Greek_Motorway_8_(National_Road_8A)_-_Korinthos_Exit.jpg – Khawar Ali Jan 27 '14 at 07:15
  • okay, my bad. cout< – Khawar Ali Jan 27 '14 at 07:38
  • Possible duplicate of [cout not printing unsigned char](https://stackoverflow.com/questions/15585267/cout-not-printing-unsigned-char) – phuclv Sep 03 '18 at 10:02
  • *"When I display `bottomRGB[0]`..."* - You should show the output. At the moment people can only guess at the output and the reason for the values. – jww Sep 03 '18 at 10:22

1 Answers1

3

As you have commented, the reason is that you use cout to print its content directly. Here I will try to explain to you why this will not work.

cout << bottomRGB[0] << endl;

Why "cout" works weird for "unsigned char"?

It will not work because here bottomRGB[0] is a unsigned char (with value 218), cout actually will print some garbage value (or nothing) as it is just a non-printable ASCII character which is getting printed anyway. Note that ASCII character corresponding to 218 is non-printable. Check out here for the ASCII table.

P.S. You can check whether bottomRGB[0] is printable or not using isprint() as:

cout << isprint(bottomRGB[0]) << endl; // will print garbage value or nothing

It will print 0 (or false) indicating the character is non-printable


For your example, to make it work, you need to type cast it first before cout:

cout << (int) bottomRGB[0] << endl; // correctly printed (218 for your example) 
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174