Ask Your Question

amazingguo's profile - activity

2014-02-17 19:59:05 -0600 commented answer cvReshapeMatND after cvCreateMatND,memory can not be released.

This is how I do the 3-dimensional to 2-dimensional convert for now(Ca -> Ca2) I wonder if there is any better solution.

int mat_size[3] = {100, 700, 1000};
Mat Ca = Mat(3, mat_size, CV_32FC1);

int new_size[2] = {7000, 10000};    

CvMatND TpCa = Ca;
CvMatND *Caimg1 = cvCreateMatNDHeader(2, new_size, CV_32F);

Caimg1 = (CvMatND*)cvReshapeMatND(&TpCa, sizeof(CvMatND), Caimg1, 0, 2, new_size);

Mat Ca2(Caimg1);
2014-02-17 06:05:23 -0600 received badge  Editor (source)
2014-02-17 06:01:09 -0600 received badge  Supporter (source)
2014-02-17 05:58:56 -0600 commented answer cvReshapeMatND after cvCreateMatND,memory can not be released.

Thank you for you answer! I know Mat class can automatically manage the memory which is more convinient and safer. But I really needs a method to convert a 3-dimensional Mat to a 2-dimensional Mat, so I have to use that CvMatND struct as a bridge to do the convertion. As far as I know the cvReshapeMatND() is the only method that can do so(the normal reshape method seems to only deal with the 2-dimensional Mat). The program above is only a small demo, not my original code. It will be very appreciated if you can give a better sollution, thank you very much!

2014-02-17 05:48:57 -0600 commented answer cvReshapeMatND after cvCreateMatND,memory can not be released.

Thank you! You are right! This is indeed why the memory is lost somehow... I figured out the solution:use cvCreateMatNDHeader() instead of the cvCreateMatND(), that solved the problem.

2014-02-17 03:19:20 -0600 asked a question cvReshapeMatND after cvCreateMatND,memory can not be released.

I use OpenCV 2.4.8 with VS2010. When I try to release some memory using the following code, the memory is not released. I think something went wrong with the cvReshapeMatND() function. If you comment this function out, the memory can be released.(Please open the windows task manager and watch the memory of the excution.) Please shed me some light about this problem, thank you very much!

#include "stdafx.h"
#include <opencv.hpp>
#include <Windows.h>

using namespace std;
using namespace cv;

int _tmain(int argc, _TCHAR* argv[])
{
    Mat Ca = Mat::zeros(7000, 10000, CV_32FC1);
    for(int i = 0; i < 1000; i++)
    {
        int new_size[2] = {7000, 10000};    

        CvMatND TpCa = Ca;
        CvMatND *Caimg1 = cvCreateMatND(2, new_size, CV_32F);

        cvReshapeMatND(&TpCa, sizeof(CvMatND), Caimg1, 0, 2, new_size);

        cout<<"Before"<<endl;
        Sleep(2000);    
        cvReleaseMatND(&Caimg1);
        cout<<"After"<<endl;
        Sleep(2000);
    }
    return 0;
}