Ask Your Question
0

cv::Mat convertion to double type

asked 2013-09-09 23:55:28 -0600

HorrorCV gravatar image

updated 2013-09-10 00:58:57 -0600

Moster gravatar image

Hi,

I am not really familiar with openCV, so I am probably using its functions wrong.

The code is as follows.

cv::Mat im0Bgr = cv::imread("colormapTest.png"); // 32x32 test image

cv::Mat im0BgrDouble;

im0Bgr.convertTo(im0BgrDouble, CV_32F);

// Then //

cv::namedWindow("Figure 1");

cv::namedWindow("Figure 2");

cv::imshow("Figure 1", im0Bgr);

cv::imshow("Figure 2", im0BgrDouble);

//... and destroyed just after

My original image is of type CV_8U. It is displayed correctly. My converted image is supposed to be in the range [0,1], while it is not ([0,255] instead). More than that, only the half top part is displayed, and the bottom half is displayed white, though the converted object contains some 0 there.

image description

Can someone point out where I am mistaken? I guess such a simple thing does not come from OpenCV. Thank you.

edit retag flag offensive close merge delete

Comments

It cant really work. The only thing you do is convert the values from an 8bit unsigned to 32bit float which will create crap. What is your goal actually? Do you just want to have a binary image ( 0 or 1) or do you want that all values are between 0 and 1 (so also 0.1, 0.11232 etc.)

Moster gravatar imageMoster ( 2013-09-10 00:56:34 -0600 )edit

My program needs more accuracy (filtering, reduction by the average... this kind of operations). So the first thing I need is to use floating points. But the behavior I have, just by converting it, is unexpected. And since 32 = 8 x 4, why does it display half of my image?

HorrorCV gravatar imageHorrorCV ( 2013-09-10 01:41:20 -0600 )edit
2

you'd want im0Bgr.convertTo(im0BgrDouble, CV_32F, 1.0/255); // it does not scale automatically.

berak gravatar imageberak ( 2013-09-10 02:18:49 -0600 )edit
1

And CV_32F is a float. If you want double precision - use CV_64F.

Vladislav Vinogradov gravatar imageVladislav Vinogradov ( 2013-09-10 02:41:19 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-09-10 03:05:37 -0600

HorrorCV gravatar image

Thank you for the scaling factor. As for the double precision, apparently some openCV functions I want to use are made for the CV_32F type only. I am using the 2.4.1 version, but maybe it has been rectified after. The only remaining problem is about the display of the result (convertTo's first parameter).

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-09-09 23:55:28 -0600

Seen: 7,106 times

Last updated: Sep 10 '13