openCV mixing IplImage with cv::Mat

前端 未结 1 721
萌比男神i
萌比男神i 2021-01-07 09:11

I have experienced some memory managment ambiguitys with opencv. You could do this with the new opencv c++ classes:

cv::Mat theimage = cvLoadImage(\"rawimag         


        
1条回答
  •  無奈伤痛
    2021-01-07 10:07

    The ambiguity comes from an extremelly horrible practice: mixing the C interface with the C++ interface of OpenCV. Don't do it, use cv::imread() instead.

    The destructor of cv::Mat will always deallocate memory when needed, except when it is initialized from a IplImage, then it's up to you to free it's resources with deallocate(). I wrote a simple experiment to verify this information:

    void load()
    {
        cv::Mat img = cvLoadImage("out.png", 1);
    
        std::cout << "* IMG was loaded\n";
        getchar();
    
        img.deallocate();
    }
    
    int main()
    {
        load();
    
        std::cout << "* IMG was deallocated\n";
        getchar();
    
        return 0;
    }
    

    The getchar() calls pause the program so you can check the memory footprint of the application. If you comment out deallocate() you'll notice that the memory footprint doesn't decrease after loading the image.

    On a side note, Mat::release() only decreases the reference counter and deallocates the data if needed (but it will also not deallocate in case the Mat was initialized from IplImage).

    0 讨论(0)
提交回复
热议问题