Ask Your Question
0

Trouble accessing the intensity value of a CV_U8 gray image in C++

asked 2016-06-20 14:21:18 -0600

rtlumb gravatar image

updated 2016-06-20 14:33:10 -0600

In a larger context, I'd like to perform a sweep on detected circle object in a image, but cannot get a reasonable intensity value or I guess access the pixel correctly. I expect a uchar pixel type with values from 0-255. When I run the code, I get outputs that either involve letters or numbers that are much too low to be correct.

I use cvtColor() to first change a RGB image to Gray. The RGB image is originally a CV_8UC3 type mat image. I am also using openCV 3.0.

Here's a small snippet of code where I try to display a pixel value in the terminal. Here, circles is a vector of vectors, and I'm using it to access the center point of the detected circle object. When I draw the detected circles on the image and display it, there seems to be no issue.

Point2f center(cvRound(circles[0][0]), cvRound(circles[0][1]));
int x = round(circles[0][0]);
int y = round(circles[0][1]);
printf("center: %x\n", image.at<uchar>(center));
printf("center: %x\n", image.at<uchar>(circles[0][0], circles[0][1]));
printf("center: %x\n", *image.ptr<uchar>(x, y));

Thanks! Rowan

edit retag flag offensive close merge delete

Comments

welcome to row-col world ! (y,x), not (x,y)

berak gravatar imageberak ( 2016-06-20 14:27:59 -0600 )edit

be nice to us (bleeding eyes ..), and shorten your question's title ;)

berak gravatar imageberak ( 2016-06-20 14:29:26 -0600 )edit

I have tried switching the rows and cols to no avail. I think the problem is a bit deeper than that. Also, shortened the title a bit.

rtlumb gravatar imagertlumb ( 2016-06-20 14:34:05 -0600 )edit

Convert the uchar to an int to get comprehensible answers. If you don't, it tries to treat it as a character, not a number.

Tetragramm gravatar imageTetragramm ( 2016-06-20 18:06:00 -0600 )edit

I have tried that with code like:

char g = image.at<uchar>(center); int r = int(g); cout << "r: " << r << endl;

Unfortunately this gives me the same output as the printf line above, and does return an integer value, but nothing like I'd expect. The values I currently receive are single digits values while I expect something about 150 or above.

rtlumb gravatar imagertlumb ( 2016-06-21 14:18:36 -0600 )edit

Note that you didn't keep a consistent type there. You went from uchar to char, which is not the same thing. The typical effect (for almost every C++ compiler you'll use) is that 0-128 is the same, but anything above that becomes negative. Try going directly from a uchar to an int.

Tetragramm gravatar imageTetragramm ( 2016-06-21 19:02:17 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-06-22 14:29:19 -0600

rtlumb gravatar image

The proper intensity value was able to be accessed with the .at() function once the proper object types were used. In this case, and unsigned char object was being assigned to a char object.

"The typical effect (for almost every C++ compiler you'll use) is that 0-128 is the same, but anything above that becomes negative." -Tetragramm

Once the conversion was from uchar directly to int, instead of uchar to char to int, the expected 0-255 range of intensities were observed.

unsigned char g = image.at<uchar>(center); int r = int(g); cout << "r: " << r << endl;

Special Thanks to Tetragramm for helping resolve the issue.

-Rowan

edit flag offensive delete link more

Comments

It works :)

mmmsss gravatar imagemmmsss ( 2017-10-25 16:35:50 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-20 14:21:18 -0600

Seen: 987 times

Last updated: Jun 22 '16