OpenCV错误:输入参数的大小不匹配

时间:2011-03-30 16:30:32

标签: c++ opencv

我有一个奇怪的问题。如果我在图像上使用cvCvtColor它可以工作,但如果我想修改该图像并在其上使用cvCvtColor,则会出现错误:

  

OpenCV Error:输入参数的大小   在cvCvtColor文件中不匹配()   /build/buildd-opencv_2.1.0-3-i386-PaiiLK/opencv-2.1.0/src/cv/cvcolor.cpp,   第2208行终止后调用   抛出一个实例   'CV ::异常'

不应该有这个错误,因为我有输出:

  

targetImage-> width = 300,   targetImage-> height = 300 cap-> width   = 300,cap-> height = 300

即:大小相同。所以这是无稽之谈.. 想知道可能的解决方案吗?

相关代码在这里:

printf("\ntargetImage->width =%d, targetImage->height =%d ",targetImage->width,targetImage->height );

cap = cvCreateImage(cvSize(targetImage->width,targetImage->height), IPL_DEPTH_8U, 1);
cvCvtColor(targetImage, cap, CV_BGR2GRAY);//HERE NO PROBLEM

CvRect xargetRect = cvRect(0,0,300,300);
subImage(targetImage, &showImg, xargetRect);
cap = cvCreateImage(cvSize(targetImage->width,targetImage->height), IPL_DEPTH_8U, 1);
printf("\ntargetImage->width =%d, targetImage->height =%d ",targetImage->width,targetImage->height );
printf("\ncap->width =%d, cap->height =%d ",cap->width,cap->height );
cvCvtColor(targetImage, cap, CV_BGR2GRAY); //HERE THE PROBLEM

由于

这是子图像代码:

/// Modifies an already allocated image header to map
/// a subwindow inside another image.
inline void subImage(IplImage *dest, const IplImage *orig, const CvRect &r) {
   dest->width = r.width;
   dest->height = r.height;
   dest->imageSize = r.height * orig->widthStep;
   dest->imageData = orig->imageData + r.y * orig->widthStep + r.x * orig->nChannels;
   dest->widthStep = orig->widthStep;
    dest->roi = NULL;
   dest->nSize = sizeof(IplImage);
    dest->depth = orig->depth;
    dest->nChannels = orig->nChannels;
   dest->dataOrder = IPL_DATA_ORDER_PIXEL;
}

1 个答案:

答案 0 :(得分:0)

我现在有一个工作的开发环境,所以我应该发布一些代码。

您的问题中的错误消息表明您正在使用OpenCV 2.1。我在OpenCV 2.2中尝试了代码示例,它工作正常,您的subImage似乎正在按预期工作。虽然CvRect &r参数的作用是X,Y的宽度,高度(而不是P1到p2)。下面是我尝试的代码(小修改,但非常相同subImage):

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

/// Modifies an already allocated image header to map
/// a subwindow inside another image.
inline void subImage(IplImage *dest, const IplImage *orig, const CvRect &r)
{
    dest->width = r.width;
    dest->height = r.height;
    dest->imageSize = r.height * orig->widthStep;
    dest->imageData = orig->imageData + r.y * orig->widthStep + r.x * orig->nChannels;
    dest->widthStep = orig->widthStep;
    dest->roi = NULL;
    dest->nSize = sizeof(IplImage);
    dest->depth = orig->depth;
    dest->nChannels = orig->nChannels;
    dest->dataOrder = IPL_DATA_ORDER_PIXEL;
}

int _tmain(int argc, _TCHAR* argv[])
{
    IplImage targetImage;
    IplImage* showImg = cvLoadImage("c:\\image11.bmp");

    //printf("\ntargetImage->width =%d, targetImage->height =%d ", targetImage->width, targetImage->height );

    //IplImage* cap = cvCreateImage(cvSize(targetImage->width, targetImage->height), IPL_DEPTH_8U, 1);
    //cvCvtColor(targetImage, cap, CV_BGR2GRAY);//HERE NO PROBLEM

    CvRect xargetRect = cvRect(100, 100, 100, 100);
    subImage(&targetImage, showImg, xargetRect);
    IplImage* cap = cvCreateImage(cvSize(targetImage.width, targetImage.height), IPL_DEPTH_8U, 1);
    printf("\ntargetImage->width =%d, targetImage->height =%d ", targetImage.width, targetImage.height );
    printf("\ncap->width =%d, cap->height =%d ", cap->width, cap->height );
    cvCvtColor(&targetImage, cap, CV_BGR2GRAY); //HERE THE PROBLEM

    int result = cvSaveImage("c:\\image11.output.bmp", &targetImage);

    return 0;
}