Reputation: 1143
I know this question can be wired for experts, but I want to access pixels of grayscale image in openCV. I am using the following code:
cv::Mat img1 = cv::imread("bauckhage.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Now if I want to print any pixel(say at position 255,255) using img1.at<float>(255,255)
I get 0
, which is not actually black pixel. I thought the image has been read as 2d Matrix.
Actually I want to do some calculation on each pixel, for that I need to access each pixel explicitly.
Any help or suggestion will be appreciated.
Upvotes: 1
Views: 2799
Reputation: 711
You will have to do this
int x = (int)(img.at<uchar>(j,i))
This is because of the way the image is stored and the way img.at
works. Grayscale image is stored in CV_8UC1 format and hence it is basically an array of uchar
type. So the return value is a uchar
which you typecast into an int
to operate on it as ints.
Also, this is similar to your questions: accessing pixel value of gray scale image in OpenCV
Upvotes: 3
Reputation: 39816
float is the wrong type here.
if you read an image like this:
cv::Mat img1 = cv::imread("bauckhage.jpg", CV_LOAD_IMAGE_GRAYSCALE);
then it's type will be CV_8U, (uchar, a single grayscale byte per pixel). so, to access it:
uchar &pixel = img1.at<uchar>(row,col);
Upvotes: 1