Failing to read pixel of grayscale image as float

asked 2019-04-13 10:09:14 -0600

A85 gravatar image

I am trying read\write pixels of a grayscale image as floats but get an exception no matter what I do. Can someone please show me how it's suppose to be done?

edit retag flag offensive close merge delete

Comments

1

no code as text = no answer possible

LBerger gravatar imageLBerger ( 2019-04-13 11:33:14 -0600 )edit

As LBerger said it is hard to know what your are trying. I assume you are trying something like:

Mat img(Size(256, 256), CV_8UC1, Scalar(223));
float val = img.at<float>(0, 0);

Which throws an exception since you can't extract a float from an 8u image. Most grayscale images are single channel, unsigned 8bit. If you want a float image you can convert with:

 Mat floatImg;
 img.convertTo(floatImg, CV_32FC1);
 float val = floatImg.at<float>(0, 0);

then the at access method works.

Chris gravatar imageChris ( 2019-04-13 13:30:58 -0600 )edit

Thanks LBerger and Chris. You are both right, A question should contain the relevant code. Sorry.

Anyway Chris did show me the right direction (Had ''CV_16F" and somehow failed to see that). But i thought seeing the pixel as a float will give values in the [0,1] range...Is there a way to get that?

A85 gravatar imageA85 ( 2019-04-14 03:10:00 -0600 )edit

Take a look at OpenCV's normalize function link text

Chris gravatar imageChris ( 2019-04-14 07:10:56 -0600 )edit