使用OpenCV调整图像数组大小 [英] Resize an array of images with OpenCV

查看:633
本文介绍了使用OpenCV调整图像数组大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OpenCV将一个图像数组(IplImage **)传递给C ++中的一个对象。然后我试图迭代该数组,并将它们调整为固定大小(150x150)

I'm passing an array of images (IplImage**) to an object in C++ using OpenCV. I'm then trying to iterate over that array and resize them all to a fixed size (150x150)

我这样做:

for(int i = 0; i< this->numTrainingFaces; i++)
{
    IplImage* frame_copy = cvCreateImage( cvSize(150,150), this->faceImageArray[0]->depth, this->faceImageArray[0]->nChannels );
    cout << "Created image" << endl;
    cvResize(this->faceImageArray[i], frame_copy);
    cout << "Resized image" << endl;
    IplImage* grey_image = cvCreateImage( cvSize( frame_copy->width, frame_copy->height ), IPL_DEPTH_8U, 1 );
    cout << "Created grey image" << endl;
    cvCvtColor( frame_copy, grey_image, CV_RGB2GRAY );
    cout << "Converted image" << endl;
    this->faceImageArray[i] = grey_image;
    cvReleaseImage(&frame_copy);
    cvReleaseImage(&grey_image);
}

但是我得到这个输出,我不知道为什么:

But I'm getting this output, and I'm not sure why:

Created image
Resized image
Created grey image
Converted image
Created image
OpenCV Error: Assertion failed (src.type() == dst.type()) in cvResize, file /build/buildd/opencv-2.1.0/src/cv/cvimgwarp.cpp, line 3102
terminate called after throwing an instance of 'cv::Exception'
  what():  /build/buildd/opencv-2.1.0/src/cv/cvimgwarp.cpp:3102: error: (-215) src.type() == dst.type() in function cvResize

Aborted


$ b b

我基本上只是尝试用尽可能少的步骤来替换数组中的图像。

I'm basically just trying to replace the image in the array with the resized one in as few steps as possible.

编辑:

修改我的代码如下:

for(int i = 0; i< this->numTrainingFaces; i++)
{

    IplImage* frame_copy = cvCreateImage( cvSize(150,150), this->faceImageArray[i]->depth, this->faceImageArray[i]->nChannels );
    cvResize(this->faceImageArray[i], frame_copy);
    IplImage* grey_image = cvCreateImage( cvSize( frame_copy->width, frame_copy->height ), IPL_DEPTH_8U, 1 );
    cvCvtColor( frame_copy, grey_image, CV_RGB2GRAY );
    faceImageArray[i] = cvCreateImage( cvSize(grey_image->width, grey_image->height), grey_image->depth, grey_image->nChannels);
    cvCopy(grey_image,faceImageArray[i]);

    cvReleaseImage(&frame_copy);
    cvReleaseImage(&grey_image);
}

然后稍后我正在执行一些PCA, / p>

Then later on I'm performing some PCA, and get this output:

OpenCV Error: Null pointer (Null pointer to the written object) in cvWrite, file /build/buildd/opencv-2.1.0/src/cxcore/cxpersistence.cpp, line 4740

但我不认为我的代码有到我明确调用cvWrite的点,所以它必须是库的一部分。

But I don't think my code has got to the point where I'm explicitly calling cvWrite, so it must be part of the library. I can give a full implementation if necessary - is there anything in my code that's going to create a null pointer?

我的完整源代码在这里: http://pastie.org/1447022 - 创建一个类,设置变量,然后调用learn()。

My full source code is here: http://pastie.org/1447022 - a class is created, variables are set, then learn() is called.

推荐答案


  • 为什么在使用C ++时使用C接口?

  • 您的错误很简单 - 您的图片在类型上有所不同,因此您无法使用类型从所有图片的第一张图片。

  • IplImage* frame_copy = cvCreateImage( cvSize(150,150), this->faceImageArray[0]->depth, this->faceImageArray[0]->nChannels );
    

    应为

    IplImage* frame_copy = cvCreateImage( cvSize(150,150), this->faceImageArray[i]->depth, this->faceImageArray[i]->nChannels );
    

    另外, this-> 无操作。 faceImageArray [0] - > depth,faceImageArray [0] - > nChannels 应该足够了。

    Also, this-> is usually a no-op. faceImageArray[0]->depth, faceImageArray[0]->nChannels should suffice.

    这篇关于使用OpenCV调整图像数组大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆