Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scale a Mat in OpenCV

Tags:

java

opencv

I'm frustrated trying to find a method that would allow me to scale a Mat object to a different size. Please, could anybody help me with this?

My project is using the Java wrapper, but I'll be happy if the answer provided is for the C++ native OpenCV library.

like image 336
xarlymg89 Avatar asked Mar 27 '13 14:03

xarlymg89


2 Answers

If by resizing you mean scaling an image use the resize() function as follows:

resize(src, dst, dst.size(), 0, 0, interpolation);

Otherwise if you just need to change the number of rows of your Mat use the Mat::reshape() function. Pay attention that the reshape return a new Mat header:

cv::Mat dst = src.reshape ( 0, newRowVal );

Finally if you want to arbitrarily reshape the Mat (changing rows and cols) you probably need to define a new Mat with the destination dimensions and copy the src Mat to it:

Mat dst(newRowVal, newColVal, src.type());
src.setTo(0);
src.copyTo(dst(Rect(Point(0, 0), src.size())));
like image 98
Tomas Camin Avatar answered Oct 06 '22 00:10

Tomas Camin


You can use resize() function

Create a new Mat result of new dimensions

resize(input             // input image 
       result            // result image
       result.size()     // new dimensions
       0, 
       0, 
       INTER_CUBIC       // interpolation method
      );

to know more interpolation methods, you can check this doc: geometric_transformations.html#resize

like image 29
Y.AL Avatar answered Oct 05 '22 23:10

Y.AL