1

我想使用双三次插值执行图像调整大小。但是,我的输出图像 new_img 看起来与我的原始加载图像 img 完全相同。此外,当我找到 new_img 的宽度和高度时,它的尺寸与我的原始图像相同。我认为目标图像应该已调整大小?这是我的代码。有人会看看我是否做错了什么吗?先感谢您。

#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;
    }
}

修改后的代码:

#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;
}
4

2 回答 2

1

IplImage* new_img = img;做一个浅拷贝,所以 new_img 和 img 指向相同的数据

于 2012-03-08T05:58:46.770 回答
1

你需要放大..你有两种方法(只是显示高度的比例):

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

所以openCV使用“sz”自动计算testIn与testOut之间的比率(例如:“Y”因子是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);

所以openCV使用“2.0f”参数作为比率(对于“Y”),但在这里你必须确保testOut有足够的大小来满足这个比率

于 2017-02-13T10:07:51.800 回答