传递IplImage *功能,原始没有更新

时间:2013-02-10 16:42:41

标签: c++ c opencv parameter-passing iplimage

我在函数中传递IplImage *参数时遇到问题。这是一个学校项目,不幸的是我不应该编辑调用我的过滤器功能的文件。在main中,图像作为命令行参数引入,然后复制它并传递给正确的过滤函数,如下:

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 *变量时,我是否缺少一些东西?

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

您的功能永远不会编辑传递给它的图像。

这条线可能是制作图像的副本。

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

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

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

修改 Vaughn Cato的答案具体说明了如何解决这个问题。

答案 1 :(得分:2)

而不是:

image = cvCloneImage(&(IplImage)matImage); 

使用它:

cvCopy(&(IplImage)matImage,image);

答案 2 :(得分: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] = ......;
}