Ask Your Question
0

How to resize Float Matrix in OpenCV

asked Apr 25 '13

Karthik gravatar image

I have a Mat object of type CV_32FC1 of size 300x300 ....

I tried :

B=Mat::ones(rows,cols,CV_32FC1)*100;

...process B...

Mat res_B=Mat::zeros(480,640,CV_32FC1); 

resize(B, res_B, Size(640,480), 0, 0, INTER_CUBIC);

How to resize it to size 640x480 ?? When I used the resize() function in OpenCV it doesnt seem to work...Any suggestions??

Preview: (hide)

Comments

what exactly does not work ?

berak gravatar imageberak (Apr 25 '13)edit

I could reproduce your code and it works. I printed the matrix using the following code: cv::namedWindow("Display Image", CV_WINDOW_AUTOSIZE); imshow("Display Image", res_B); waitKey(0);

Nyenna gravatar imageNyenna (Apr 25 '13)edit

@berak All values in res_B are 100.... But after processing B contains several floating point nos... In the resized Mat res_B it is not !!

Karthik gravatar imageKarthik (Apr 25 '13)edit

@Nyenna Did you use Float Mat ??? I think,imshow function supports only CV_8UC1 ..

Karthik gravatar imageKarthik (Apr 25 '13)edit

I used the very same code that you posted (without the processing part, of course). Are you sure you process B correctly? Can you verify that you do by showing the image just before the resize() line?

Nyenna gravatar imageNyenna (Apr 26 '13)edit

No...aftr processing there will be floating values in Mat.... You manually assign certain pixels with float values such as 21.11,19.33,88.43 and then resize ...and display...do you get same result???

Karthik gravatar imageKarthik (Apr 26 '13)edit

1 answer

Sort by » oldest newest most voted
1

answered Apr 26 '13

Nyenna gravatar image

updated Apr 26 '13

The following code works:

cv::Mat B = cv::Mat::ones(300,300,CV_32FC1);

for (int i = 0; i < 100; i++)
{
    B.at<int>(50, i) = 0;
    B.at<int>(51, i) = 0;
}

cv::Mat res_B;

resize(B, res_B, Size(640, 480));

namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", B );
imshow( "Display Resized Image", res_B );

waitKey(0);

I hope it will help.

Preview: (hide)

Comments

Maybe there is something I don't get, but isn't it normal if you put data in a matrix, then expand it to not have the same result everywhere because of the interpolation ?

Even if the matrix is homogeneous, i noticed that OpenCV do some weird things with floating point values (maybe some double => float truncation) that I couldn't explain by the fact that the set of floating point values is not continuous.

So the essence of interpolation is to "create" data that could be correct regarding the neighbourhood.

Or am I just really far of the problem here ?

Rogeeeer gravatar imageRogeeeer (Apr 26 '13)edit

Yes..It resizes properly.... if I give the interpolation option as INTER_NEAREST or INTER_LINEAR... if i use INTER_CUBIC , there are several negative values in the resized matrix whereas I had all positive values in the original matrix..... Do you know why INTER_CUBIC generates negative float values in resized matrix.... I still used it for an Image(uchar) and got the resized image pretty well

Karthik gravatar imageKarthik (Apr 27 '13)edit

Question Tools

Stats

Asked: Apr 25 '13

Seen: 8,848 times

Last updated: Apr 26 '13