Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resize a Matrix after created it in OpenCV

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

like image 589
steo Avatar asked Jul 08 '13 18:07

steo


People also ask

Which function is used to resize an image in OpenCV?

resize() Function. To resize images with OpenCV, use the cv2. resize() function. It takes the original image, modifies it, and returns a new image.


1 Answers

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);
like image 82
jet47 Avatar answered Oct 17 '22 05:10

jet47