2011-03-30 41 views
1

我有一個奇怪的問題。如果我使用cvCvtColor圖像上它的工作原理,但如果我想修改圖像,並使用cvCvtColor它有一個錯誤:OpenCV錯誤:輸入參數的大小不匹配

OpenCV Error: Sizes of input arguments do not match() in cvCvtColor, file /build/buildd-opencv_2.1.0-3-i386-PaiiLK/opencv-2.1.0/src/cv/cvcolor.cpp, line 2208 terminate called after throwing an instance of 'cv::Exception'

,因爲我有作爲輸出應該不會出現這個錯誤

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; 
} 
+0

你可以發佈subImage的代碼嗎? – razlebe 2011-03-30 16:41:47

+0

我剛剛看到您的評論,抱歉的延遲;) – marinila 2011-03-30 17:07:58

+0

我認爲你的'widthStep'和'imageSize'都不正確,你將不得不重新計算那些基於新圖像的尺寸,因爲子窗口。 – 2011-03-30 17:39:33

回答

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; 
} 
相關問題