0

我在函数中传递 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
}

问题是,主要显示的过滤图像不反映实际图像。它只显示原始图像。当我将 Mat 风格的图像 matImage 输出到我的过滤器函数中时,它会显示过滤后的图像。在我转换回 IplImage 并将 IplImage* 输入参数设置为等于转换后的过滤版本之后立即。但是这些变化并没有反映主函数中显示的图像。

这使得我很难创建其他一些过滤器,例如 Gaussian 和 Sobel,因为这些过滤器本身在进行操作之前会调用其他函数,而我没有得到编辑后的数据。在如何编辑传递的 IplImage* 变量时,我有什么遗漏吗?

预先感谢您的任何帮助!

4

3 回答 3

3

您的函数永远不会编辑传递给它的图像。

这条线大概是在复制图像。

Mat matImage(image);        //convert IplImage to Mat for work

然后您的代码会修改该matImage副本。

并且在函数结束时,该本地副本被销毁,并且IplImage指向的 byimage从未被修改过。

编辑: Vaughn Cato 的回答具体说明了如何解决这个问题。

于 2013-02-10T16:50:13.783 回答
2

而不是这个:

image = cvCloneImage(&(IplImage)matImage); 

用这个:

cvCopy(&(IplImage)matImage,image);
于 2013-02-10T16:56:42.433 回答
0

要遍历 3 个通道,您应该使用迭代器:

Mat out;
*********open out********
Mat_<Vec3b>::iterator it = out.begin<Vec3b>();
MatConstIterator_<Vec3b> it_end = out.end<Vec3b>();

for(; it != it_end; ++it)
{
    //your 3 channels
    (*it)[0] = ...;
    (*it)[1] = ....;
    (*it)[2] = ......;
}
于 2013-02-11T00:45:42.230 回答