Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing elements of OpenCV CV_8UC1 cv::Mat

Tags:

c++

opencv

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

This might be a stupid question, but I can't tell why this happens and what would be a better way to convert the entries to int.

Thanks in advance!

like image 859
mo5470 Avatar asked Oct 03 '12 08:10

mo5470


1 Answers

You have to specify that the elements are unsigned char, between 0 and 255 , otherwise they will be char (signed), from -128 to 127. The casting will be this way:

(int) image.at<uchar>(row,column);
like image 139
Jav_Rock Avatar answered Sep 19 '22 17:09

Jav_Rock