将IplImage*传递给函数,原始未更新



我在函数中传递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输出到我的filter函数中时,它会显示过滤后的图像。在我转换回IplImage并将IplImage*输入参数设置为等于转换后的过滤版本之后。但这些变化并不能反映主功能中显示的图像。

这使得我很难创建其他一些过滤器,如Gaussian和Sobel,因为这些过滤器本身在进行操作之前会调用其他函数,而我不会得到编辑后的数据。在如何编辑传递的IplImage*变量方面,我是否缺少一些东西?

提前感谢您的帮助!

您的函数从不编辑传递给它的图像。

这行大概是在复制图像。

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

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

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

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

而不是这个:

image = cvCloneImage(&(IplImage)matImage); 

使用这个:

cvCopy(&(IplImage)matImage,image);

要遍历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] = ......;
}

相关内容

  • 没有找到相关文章