我在函数中传递IplImage *参数时遇到麻烦。这是针对学校项目的,不幸的是,不应调用我的过滤器功能来编辑文件。首先,将图像作为命令行参数引入,然后制作该图像的副本并将其传递给正确的过滤器函数,如下所示:

IplImage * floating = cvCreateImage (cvSize (img->width, img->height), IPL_DEPTH_32F, 3);
cvConvertScale (img, floating, 1/255., 0);

IplImage * filtered;

switch (filter.ImageFormat)
{
    case YUV24:
        filtered = cvCreateImage (cvSize (floating->width, floating->height), IPL_DEPTH_32F, 3);
        cvCvtColor (floating, filtered, CV_BGR2YCrCb);
        break;
    case BGR24:
        filtered = cvCloneImage (floating);
        break;
    case Gray:
        filtered = cvCreateImage (cvSize (floating->width, floating->height), IPL_DEPTH_32F, 1);
        cvCvtColor (floating, filtered, CV_BGR2GRAY);
        break;
}

cvNamedWindow ("original", CV_WINDOW_AUTOSIZE);
cvShowImage ("original", img);

filter.ImageFilter (filtered, p);

if (filter.ImageFormat == YUV24)
{
      cvCvtColor (filtered, filtered, CV_YCrCb2BGR);
}

cvNamedWindow (filterName.c_str(), CV_WINDOW_AUTOSIZE);
cvShowImage (filterName.c_str(), filtered);

这是我的一个过滤器中的代码:
void median (IplImage * image, int k){
    cout << "image address: " << &image << endl;
    Mat matImage(image);        //convert IplImage to Mat for work
    vector<Mat> bgr_channels;   //vector to hold each channel
    int i,j,m,n;                //row/column indeces
    int kernelSize = (2*k+1)*(2*k+1);
    vector<float> vals(kernelSize); //kernelSized vector to hold all values of image
                //within kernel centered at a given pixel
    int vecIndex = 0;   //then sorted to get the median
    int chanIndex = 0;  //index used to for each channel

    //add padding to account for border issues with convolution
    copyMakeBorder( matImage, matImage, k, k, k, k, BORDER_REPLICATE);
    //split channes to do work on individual channels
    split(matImage, bgr_channels);

    for(chanIndex=0; chanIndex < matImage.channels(); chanIndex++){
        //outer loop for scanning entire image
        for(i=k; i<matImage.rows-k; i++)/*image row index*/{
            for(j=k; j<matImage.cols-k; j++)/*image column index*/{
                //inner loop for scanning image only in kernel boundaries
                vecIndex = 0;   //reset vecIndex at start of each kernel loop
                for(m=i-k; m<(i+k+1); m++)/*kernel row index*/{
                    for(n=j-k; n<(j+k+1); n++)/*kernel column index*/{
                        vals[vecIndex++] = bgr_channels[chanIndex].at<float>(m,n);
                    }
                }
                insertionSort(vals, 0, vals.size()-1);  //insertion sort from CSCI362 text, see references
                bgr_channels[chanIndex].at<float>(i,j) = vals[vals.size()/2]; //new value chosen from middle element                                                                //of sorted vector
            }
        }
    }

    merge(bgr_channels, matImage);      //merge channels together
    imshow("Median: Mat", matImage);    //left this in becuase the original doesn't seem to get modified
                                        //when converting the Mat back to an IplImage
    image = cvCloneImage(&(IplImage)matImage);  //convert backto IplImage for DesktopMain
}

问题在于,主画面中显示的滤波后的图像无法反射(reflect)实际图像。它仅显示原始图像。当我输出Mat样式图像matImage到我的过滤器函数中时,它将显示过滤后的图像。在我转换回IplImage并将IplImage *输入参数设置为等于转换后的过滤版本后,立即。但是更改不会反射(reflect)出主功能中显示的图像。

这使得创建其他一些过滤器(例如高斯和Sobel)变得很困难,因为这些过滤器本身在进行其他操作之前会先调用其他函数,而且我也不会得到已编辑的数据。在如何编辑传递的IplImage *变量时我缺少什么?

预先感谢您的任何帮助!

最佳答案

代替这个:

image = cvCloneImage(&(IplImage)matImage);

用这个:
cvCopy(&(IplImage)matImage,image);

关于c++ - 将IplImage *传递给功能,原始文件未更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14799993/

10-13 09:31