Trouble accessing the intensity value of a CV_U8 gray image in C++
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
welcome to row-col world ! (y,x), not (x,y)
be nice to us (bleeding eyes ..), and shorten your question's title ;)
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.
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.
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.
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.