xarlymg89
xarlymg89

Reputation: 2617

How to scale a Mat in 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.

Upvotes: 6

Views: 29392

Answers (2)

Tomas Camin
Tomas Camin

Reputation: 10096

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())));

Upvotes: 12

Y.AL
Y.AL

Reputation: 1793

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

Upvotes: 11

Related Questions