Question

I want to perform an image resize using bicubic interpolation. However, my output image, new_img looks exactly the same as my original loaded image, img. Also, when I found the width and height of new_img, it has the same dimensions as my original image. I thought that the destination image was supposed to have been resized? This is my code. Would someone see if I have done anything inaccurately please? Thank you in advance.

#include "cv.h"
#include "highgui.h"
#include "iostream"

using namespace std;
int main( int argc, char* argv ) {
IplImage* img = NULL;
const int maxScale = 1;
img = cvLoadImage("C:\\walk mii.jpg");
if (!img){return -1;}
for(int s = 1; s <= maxScale; s++)
{
    IplImage* new_img = img;
    if( s > 1 ) {
        new_img = cvCreateImage(cvSize(img->width*s,img->height*s), img->depth, img->nChannels );
        cvResize( img, new_img, CV_INTER_CUBIC );}
    cvNamedWindow( "original", CV_WINDOW_AUTOSIZE );
    cvShowImage( "original", img);
    cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
    cvShowImage( "result", new_img);

    CvSize dim = cvGetSize(new_img);
        cout <<" dimensions:: height:" <<  dim.height<<" width:"<< dim.width<< endl;

    cvWaitKey(0);
    cvReleaseImage( &img );
    cvReleaseImage( &new_img );
    cvDestroyWindow( "result" );
    return 0;
    }
}

Altered code:

#include "cv.h"
#include "highgui.h"
#include "iostream"

using namespace std;
int main( int argc, char* argv ) {
IplImage* img = NULL;
IplImage* new_img = NULL;
img = cvLoadImage("C:\\walk mii.jpg");
if (!img){
    return -1;}
new_img = cvCreateImage(cvSize(img->width,img->height), img->depth, img->nChannels );
cvResize( img, new_img, CV_INTER_CUBIC );
cvNamedWindow( "original", CV_WINDOW_AUTOSIZE );
cvShowImage( "original", img);
cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
cvShowImage( "result", new_img);

CvSize dim = cvGetSize(new_img);
cout <<" dimensions:: height:" <<  dim.height<<" width:"<< dim.width<< endl;

cvWaitKey(0);
cvReleaseImage( &img );
cvReleaseImage( &new_img );
cvDestroyWindow( "result" );
return 0;
}
Was it helpful?

Solution

IplImage* new_img = img; does a shallow copy so new_img and img point to the same data

OTHER TIPS

You need to scale up.. you have 2 ways (just showing up scale for Height):

1)

Mat testIn(inputRows, inputCols, CV_32F);
Mat testOut(inputRows*2, inputCols, CV_32F);
Size sz = testOut.size();

resize(testIn, testOut, sz, 0.0f, 0.0f, INTER_CUBIC);

so openCV automatically figures the ratio between the testIn to the testOut using the "sz" (example: "Y" factor is sz.height / testIn.Height)

2)

Mat testIn(inputRows, inputCols, CV_32F);
Mat testOut(inputRows*2, inputCols, CV_32F);
Size sz = testOut.size();

resize(testIn, testOut, sz, 2.0f, 0.0f, INTER_CUBIC);

so openCV uses the "2.0f" param as ratio (for "Y") , but here you must make sure testOut has enough size for such ratio

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top