1
void doCorrectIntensityVariation(Mat& image)
{   
    Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(19,19));
    Mat closed;
    morphologyEx(image, closed, MORPH_CLOSE, kernel);
    image.convertTo(image, CV_32F); // divide requires floating-point
    divide(image, closed, image, 1, CV_32F);
    normalize(image, image, 0, 255, NORM_MINMAX);
    image.convertTo(image, CV_8UC1); // convert back to unsigned int

}

inline void correctIntensityVariation(IplImage *img)
{
//Mat imgMat(img); copy the img
Mat imgMat;
imgMat = img; //no copy is done, imgMat is a header of img
doCorrectIntensityVariation(imgMat);
imshow("gamma corrected",imgMat); cvWaitKey(0);
}

当我打电话

cvShowImage ("normal", n_im); cvWaitKey (0);
correctIntensityVariation(n_im);//here n_im is IplImage*
cvShowImage ("After processed", n_im); cvWaitKey (0);
// here I require n_im for further processing

我希望“处理后”与“伽马校正”相同,但我发现“处理后”与“伽马校正”不同,但与“正常”相同。为什么??出了什么问题??

4

1 回答 1

3

一个非常简单的包装器应该可以完成这项工作

openCV 的小抄

我很少使用旧的 api,因为 Mat 更容易处理,并且与旧的 c api 相比,它们没有性能损失。就像 openCV 教程页面说的 C++ 接口的主要缺点是很多嵌入式开发目前的系统仅支持 C。因此,除非您的目标是嵌入式平台,否则使用旧方法是没有意义的(除非您是受虐狂程序员并且您正在自找麻烦)。

openCV 教程

cv::Mat 到 Ipl

Ipl 到 cv::Mat 和 Mat 到 Ipl

IplImage* pImg = cvLoadImage(“lena.jpg”);
cv::Mat img(pImg,0); //transform Ipl to Mat, 0 means do not copy 
IplImage qImg; //not pointer, it is impossible to overload the operator of raw pointer
qImg = IplImage(img); //transform Mat to Ipl

编辑:我之前犯了一个错误,如果要在函数中重新分配 Mat,您需要从 Mat 复制或尝试窃取资源(我还不知道该怎么做)。

复制数据

void doCorrectIntensityVariation(cv::Mat& image)
{
    cv::Mat kernel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(19,19));
    cv::Mat closed;
    cv::morphologyEx(image, closed, cv::MORPH_CLOSE, kernel);
    image.convertTo(image, CV_32F); // divide requires floating-point
    cv::divide(image, closed, image, 1, CV_32F);
    cv::normalize(image, image, 0, 255, cv::NORM_MINMAX);
    image.convertTo(image, CV_8UC1); // convert back to unsigned int

}

//don't need to change the name of the function, the compiler treat
//these as different function in c++
void doCorrectIntensityVariation(IplImage **img)
{
    cv::Mat imgMat;
    imgMat = *img; //no copy is done, imgMat is a header of img
    doCorrectIntensityVariation(imgMat);

    IplImage* old = *img;
    IplImage src = imgMat;
    *img = cvCloneImage(&src);
    cvReleaseImage(&old);
}

int main()
{


    std::string const name = "onebit_31.png";
    cv::Mat mat = cv::imread(name);
    if(mat.data){
        doCorrectIntensityVariation(mat);

        cv::imshow("gamma corrected mat",mat);
        cv::waitKey();
    }

    IplImage* templat = cvLoadImage(name.c_str(), 1);
    if(templat){
        doCorrectIntensityVariation(&templat);
        cvShowImage("mainWin", templat);

        // wait for a key
        cvWaitKey(0);
        cvReleaseImage(&templat);
    }



    return 0;
}

你可以写一个小函数来减轻杂务

void copy_mat_Ipl(cv::Mat const &src, IplImage **dst)
{
         IplImage* old = *dst;
        IplImage temp_src = src;
        *dst = cvCloneImage(&temp_src);
        cvReleaseImage(&old);
}

并在函数中调用它

void doCorrectIntensityVariation(IplImage **img)
{
    cv::Mat imgMat;
    imgMat = *img; //no copy is done, imgMat is a header of img
    doCorrectIntensityVariation(imgMat);    
    copy_mat_to_Ipl(imgMat, img);
}

在找到可靠的解决方案后,我将发布如何从 Mat 中“窃取”资源而不是复制资源。有人知道该怎么做吗?

于 2013-04-23T15:50:11.123 回答