29
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.

Is there any opencv function to resize it to the img size?

Noha Kareem
  • 1,748
  • 1
  • 22
  • 32
Milad R
  • 1,854
  • 9
  • 25
  • 36

5 Answers5

39

You can use cvResize. Or better use c++ interface (eg cv::Mat instead of IplImage and cv::imread instead of cvLoadImage) and then use cv::resize which handles memory allocation and deallocation itself.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Mohammad
  • 1,253
  • 1
  • 10
  • 26
  • Thanks. I saw cv::Mat in lots of topics which are about opencv, but I don't what it is. Where can I find some information(esp basic info) about it, and related functions like one you said? – Milad R Jul 27 '12 at 00:39
36

The two functions you need are documented here:

  1. imread: read an image from disk.
  2. Image resizing: resize to just any size.

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

Régis B.
  • 10,092
  • 6
  • 54
  • 90
10

For your information, the python equivalent is:

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 )
Seanny123
  • 8,776
  • 13
  • 68
  • 124
Alexandre Mazel
  • 2,462
  • 20
  • 26
5

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;
} 
LovaBill
  • 5,107
  • 1
  • 24
  • 32
0

You can use CvInvoke.Resize for Emgu.CV 3.0

e.g

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

Details are here

PinkTurtle
  • 6,942
  • 3
  • 25
  • 44
Muhammed Tanriverdi
  • 3,230
  • 1
  • 23
  • 24