Access to OpenCV Elements CV_8UC1 cv :: Mat

I have cv :: Mat of type CV_8UC1 (8-bit single-channel image), and I would like to access the elements using the at<> operator as follows: image.at<char>(row, column) . However, when you press int : (int) image.at<char>(row, column) some values ​​become negative, for example, 255 becomes -1.

This may be a stupid question, but I can’t understand why this is happening, and what would be the best way to convert records to int .

Thanks in advance!

+6
source share
2 answers

You must indicate that the elements are unsigned char , between 0 and 255, otherwise they will be char (signed), from -128 to 127. Casting would be this way:

 (int) image.at<uchar>(row,column); 
+19
source

In the matrix type CV_8, UC 1 means Unsigned Char.

So you need to write

 image.at<unsigned char>(row, column) 
+5
source

Source: https://habr.com/ru/post/926825/


All Articles