C++ 如何在 OpenCV 中将图像调整为特定大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11678527/
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 15:24:43  来源:igfitidea点击:

How to resize an image to a specific size in OpenCV?

c++opencvimage-resizing

提问by Milad R

IplImage* img = cvLoadImage("something.jpg");
IplImage* src = cvLoadImage("src.jpg");
cvSub(src, img, img);

But the size of the source image is different from img.

但是源图像的大小与img.

Is there any opencv function to resize it to the imgsize?

是否有任何 opencv 函数可以将其调整为img大小?

回答by Mohammad

You can use cvResize. Or better use c++ interface (eg cv::Matinstead of IplImageand cv::imreadinstead of cvLoadImage) and then use cv::resizewhich handles memory allocation and deallocation itself.

您可以使用cvResize. 或者更好地使用 c++ 接口(例如cv::Mat代替IplImagecv::imread代替cvLoadImage),然后使用cv::resizewhich 处理内存分配和释放本身。

回答by Régis B.

The two functions you need are documented here:

此处记录了您需要的两个功能:

  1. imread: read an image from disk.
  2. Image resizing: resize to just any size.
  1. imread:从磁盘读取图像。
  2. 图像调整大小:调整到任意大小。

In short:

简而言之:

// Load images in the C++ format
cv::Mat img = cv::imread("something.jpg");
cv::Mat src = cv::imread("src.jpg");

// Resize src so that is has the same size as img
cv::resize(src, src, img.size());

And please, please, stop using the old and completely deprecated IplImage* classes

并且请,请停止使用旧的和完全弃用的 IplImage* 类

回答by Alexandre Mazel

For your information, the python equivalent is:

供您参考,python 等效项是:

imageBuffer = cv.LoadImage( strSrc )
nW = new X size
nH = new Y size
smallerImage = cv.CreateImage( (nH, nW), imageBuffer.depth, imageBuffer.nChannels )
cv.Resize( imageBuffer, smallerImage , interpolation=cv.CV_INTER_CUBIC )
cv.SaveImage( strDst, smallerImage )

回答by LovaBill

Make a useful function like this:

做一个像这样的有用函数:

IplImage* img_resize(IplImage* src_img, int new_width,int new_height)
{
    IplImage* des_img;
    des_img=cvCreateImage(cvSize(new_width,new_height),src_img->depth,src_img->nChannels);
    cvResize(src_img,des_img,CV_INTER_LINEAR);
    return des_img;
} 

回答by Muhammed Tanr?verdi

You can use CvInvoke.Resizefor Emgu.CV 3.0

您可以CvInvoke.Resize用于Emgu.CV 3.0

e.g

例如

CvInvoke.Resize(inputImage, outputImage, new System.Drawing.Size(100, 100), 0, 0, Inter.Cubic);

Details are here

详情在这里