返回类型为IPlImage数组

时间:2012-01-16 09:33:52

标签: c arrays opencv

我今天整个上午都在寻找一种方法来保存openCV中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;

}

现在,当我尝试在main中返回数组以便处理它时 - >

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

然而它给了我错误!!!

感谢任何帮助,谢谢

1 个答案:

答案 0 :(得分: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个元素。

由于只返回指向第一个图像而不是实际数组的单个指针,因此代码失败。