2012-01-16 57 views
0

今天我花了整整一天的時間尋找一種方法來保存openCV中IplImage類型的圖像數組,並且失敗。返回類型爲數組IPlImage

這就是我要做的:

IplImage* GetThresholdedImage(IplImage* img) { 

IplImage* imageTest[2]; 

IplImage* imgHSV = cvCreateImage(cvGetSize(img), 8, 3); // hold the resulted HSV image 

cvCvtColor(img, imgHSV, CV_BGR2HSV); // convert the coming image from the camera from RGB format to HSV (Hue, Saturation, Value) 

imageTest[0] = cvCreateImage(cvGetSize(img), 8, 1); //hold the thresholded image of the yellow color 

imageTest[1] = cvCreateImage(cvGetSize(img), 8, 1); //hold the thresholded image of the red color 

cvSmooth(imgHSV, imgHSV, CV_GAUSSIAN, 11, 11); //smooth the image to remove the noise from the image 

cvInRangeS(imgHSV, cvScalar(24, 100, 150), cvScalar(34, 255, 255), 
     imageTest[0]); //this function filter out the colors in this range (This is a yellow color) 

cvInRangeS(imgHSV, cvScalar(172, 100, 150), cvScalar(179, 255, 255), 
     imageTest[1]); //this function filter out the colors in this range (This is a red color) 


cvReleaseImage(&imgHSV); 

return *imageTest; 

}

現在,當我嘗試返回主陣列,以處理它 - >

IplImage *thresholdedImage;// = cvCreateImage(cvGetSize(frame), 8, 1); // to store the thresholded image 

IplImage *yellow = cvCreateImage(cvGetSize(frame), 8, 1); 
IplImage *red = cvCreateImage(cvGetSize(frame), 8, 1); 


//=========================================== 

// start creating three windows to show the video after being thresholded, after it passes the contour function and the final video 

cvNamedWindow("display", CV_WINDOW_AUTOSIZE); 
cvNamedWindow("Threshold", CV_WINDOW_AUTOSIZE); 
cvNamedWindow("contoured", CV_WINDOW_AUTOSIZE); 

while (key != 'q') { // grab the video unless the user press q button 

    frame = cvQueryFrame(capture); 

    if (!frame) { 
     break; 
    } 

    //start the actual video processing on real-time frames 

    //first output of the threshold method 
    thresholdedImage = GetThresholdedImage(frame); 

    yellow = *thresholdedImage; 

    red = *thresholdedImage++; 

    //insert the resulted frame from the above function into the find contour function 
    cvFindContours(yellow, storage, &contours, sizeof(CvContour), 
      CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0)); 

    cvFindContours(red, storage, &contours, sizeof(CvContour), 
      CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0)); 

但它給我錯誤!

任何幫助表示讚賞,謝謝

+0

那究竟是什麼錯誤呢? – karlphillip 2012-01-16 11:23:30

+0

如果您可以使用C++,請將它們存儲在'std :: vector '中以使您的工作更輕鬆。 – karlphillip 2012-01-16 12:50:20

回答

0

指針到IplImage數組是IplImage**。另請注意,您必須從外部提供緩衝區,因爲您無法返回指向本地(非靜態)數據的指針。

void GetImages(IplImage** images, unsigned int num) { 
    // assign pointers here 
    images[0] = ... 
    images[1] = ... 
} 

IplImage *images[2]; 
GetImage(images, 2); 

作爲替代方案,你還可使用newmalloc創建函數中的數組和指針返回到它。請稍後確保您delete/free

IplImage **CreateImages(unsigned int num) { 
    IplImage **images = new IplImage*[num]; 
    // assign again 
    images[0] = ... 
    return images; 
} 

此外,請確保您在完成這些圖像後即可釋放圖像。所以你不應該使用上面使用的方法(增加指針)。相反,只需使用數組語法來訪問第n個元素。

由於僅返回一個指向第一個圖像而不是實際數組的指針,因此代碼失敗。

+0

這非常有幫助,非常感謝 – user573014 2012-01-16 12:11:25

+0

@ user573014如果您發現答案有幫助,您應該接受它。請[閱讀關於接受答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)。 – SSteve 2012-01-16 22:38:26