C++ 在 OpenCV 中创建矩阵后调整其大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17533101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:16:19  来源:igfitidea点击:

resize a Matrix after created it in OpenCV

c++imageopencvimage-resizing

提问by steo

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

我是 OpenCV 的新手,我正在查看边缘检测的 Canny 教程。我正在研究如何调整mat刚刚创建的大小。代码是这样的:

 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?

我的疑惑是两个: 1:我是做好调用resize()create()2:如何指定尺寸mat

My goalis to resize the image, if it was not clear

我的目标是调整图像大小,如果不清楚

回答by jet47

You create dstmat with the same size as src. Also when you call resizeyou pass both destination size and fx/fyscale factors, you should pass something one:

您创建dstsrc. 此外,当你调用resize你传递目标大小和fx/fy比例因子时,你应该传递一个:

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 OpenCVdocumentation:

更新:来自OpenCV文档:

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:

缩放只是调整图像的大小。为此,OpenCV 附带了一个函数 cv2.resize()。可以手动指定图像的大小,也可以指定缩放因子。使用不同的插值方法。优选的插值方法是用于缩小的 cv2.INTER_AREA 和用于缩放的 cv2.INTER_CUBIC (slow) & cv2.INTER_LINEAR。默认情况下,用于所有调整大小目的的插值方法是 cv2.INTER_LINEAR。您可以通过以下任一方法调整输入图像的大小:

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_AREAworks significantly faster than cv::INTER_CUBIC(as mentioned by OpenCVdocumentation):

此外,在 中Visual C++,我尝试了两种缩小方法,并且cv::INTER_AREAcv::INTER_CUBIC(如OpenCV文档所述)工作得更快:

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