resize a Matrix after created it in OpenCV

steo picture steo · Jul 8, 2013 · Viewed 49.9k times · Source

I'm new to OpenCV and I was looking at the Canny tutorial for Edge Detection. I was looking on how to resize a mat just created. The code is this:

 src = imread( impath );
 ...
 dst.create( src.size(), src.type() );

now I tried to resize the mat with this:

resize(dst, dst, dst.size(), 50, 50, INTER_CUBIC);

But it does not seems to change anything.

My doubts are two : 1 : Am I doing well calling resize() after create() ? 2 : How can I specify the dimensions of the mat ?

My goal is to resize the image, if it was not clear

Answer

jet47 picture jet47 · Jul 8, 2013

You create dst mat with the same size as src. Also when you call resize you pass both destination size and fx/fy scale factors, you should pass something one:

Mat src = imread(...);
Mat dst;
resize(src, dst, Size(), 2, 2, INTER_CUBIC); // upscale 2x
// or
resize(src, dst, Size(1024, 768), 0, 0, INTER_CUBIC); // resize to 1024x768 resolution

UPDATE: from the OpenCV documentation:

Scaling is just resizing of the image. OpenCV comes with a function cv2.resize() for this purpose. The size of the image can be specified manually, or you can specify the scaling factor. Different interpolation methods are used. Preferable interpolation methods are cv2.INTER_AREA for shrinking and cv2.INTER_CUBIC (slow) & cv2.INTER_LINEAR for zooming. By default, interpolation method used is cv2.INTER_LINEAR for all resizing purposes. You can resize an input image either of following methods:

import cv2
import numpy as np
img = cv2.imread('messi5.jpg')
res = cv2.resize(img,None,fx=2, fy=2, interpolation = cv2.INTER_CUBIC)
#OR
height, width = img.shape[:2]
res = cv2.resize(img,(2*width, 2*height), interpolation = cv2.INTER_CUBIC)

Also, in Visual C++, I tried both methods for shrinking and cv::INTER_AREA works significantly faster than cv::INTER_CUBIC (as mentioned by OpenCV documentation):

cv::Mat img_dst;
cv::resize(img, img_dst, cv::Size(640, 480), 0, 0, cv::INTER_AREA);
cv::namedWindow("Contours", CV_WINDOW_AUTOSIZE);
cv::imshow("Contours", img_dst);