Ask Your Question

huongnhat's profile - activity

2018-05-10 21:25:19 -0600 received badge  Popular Question (source)
2014-06-24 23:46:25 -0600 commented question Release external data pointer in Cv::Mat constructor

Everytime I use delete dst_ptr the program crash

2014-06-24 23:41:38 -0600 asked a question Release external data pointer in Cv::Mat constructor

I have these following lines of code:

cv::Mat obj(,, CV_8UC3, dst_ptr/*, ( * channels + 3) & -4*/);
StretchDIBits( hdc, 0, 0, rect.right, rect.bottom, 0, 0, arr.cols, arr.rows, dst_ptr, binfo, DIB_RGB_COLORS, SRCCOPY );

These lines cause memory leak in my program. As document:

Pointer to the user data. Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically deallocated, so you should take care of it

So how can I release the external data? Indeed it's the void *dst_ptr var

2014-06-24 00:52:18 -0600 commented answer Strange memory leak using CvMat structure

Thanks, got it. I fixed by remove all of the CvMat var, and assign dst_ptr = The source of leak is the cvConvertImage, it fill data to dst var but never free it

2014-06-23 23:49:03 -0600 commented answer Strange memory leak using CvMat structure

OK I fixed it. Btw, does opencv pass Mat as ref or value? Function such as cv::resize(src, dst, size) will creates a local copy or does operation on src?

2014-06-23 04:30:40 -0600 commented answer Strange memory leak using CvMat structure

I think that the program still used data of the dst / dst_ptr / image when the call to functioni cvShowImageHWND() is done because when I tried to release their data, the program crash. Am I correct?

2014-06-23 01:29:57 -0600 commented answer Strange memory leak using CvMat structure

I tried to modify the display function so as to be able to display opencv processed image in the MFC/Win32 windows. I know CvMat and IplImage were deprecated but I can't find other solution. The problem here is the data pointed by the CvMat *image var is somewhat retained outside the scope of the function (because when I tried to release the data, the program will raise error). Pls correct me if I were wrong or tell me how to deal with the problem :(

2014-06-22 22:41:13 -0600 received badge  Editor (source)
2014-06-22 22:39:47 -0600 asked a question Strange memory leak using CvMat structure
Dear all,

I've tried to modify the cvShowImageHWND in opencv/modules/highgui/src/window_w32.cpp in order to show image by a HWND window specified instead of using window name. My modified one worked well until I relized it's memory leak. I found that the line cvConvertImage() causes the leak and try to prevent it from leaking memory. Can anyone help me? Actually I tried to use smart pointer cv::Ptr<cvmat> to wrap it but it raises error whenever program goes out of cvShowImageHWND scope, I guess that the data is retained for displaying after the function cvShowImageHWND done, but if I didn't free the memory, my program memory keeps increasing roughly 4MB each loop. Please show me a hint or tell me what should I do

void OpenCV::cvShowImageHWND(HWND w_hWnd, const cv::Mat arr0){
        IplImage* arr=cvCloneImage(&(IplImage)arr0);
        SIZE size = { 0, 0 };
        int channels = 0;
        void* dst_ptr = 0;
        const int channels0 = 3;
        int origin = 0;
        CvMat stub, dst;
        cv::Ptr<CvMat> image;
        bool changed_size = false;
        BITMAPINFO* binfo;
        HDC hdc = NULL;

        if( !arr ) EXIT;
        if( !w_hWnd )

        hdc = ::GetDC(w_hWnd);

        if( CV_IS_IMAGE_HDR( arr ) )
            origin = ((IplImage*)arr)->origin;

        CV_CALL( image = cv::Ptr<CvMat>(cvGetMat( arr, &stub )) );

        //image = cvGetMat( arr, &stub );
        if ( hdc )

                BITMAP bmp;
                HGDIOBJ h = GetCurrentObject( hdc, OBJ_BITMAP );

                if (h == NULL) EXIT;
                if (GetObject(h, sizeof(bmp), &bmp) == 0) //GetObject(): returns size of object, 0 if error

                channels = bmp.bmBitsPixel/8;
                dst_ptr = bmp.bmBits;

        if( != image->width || != image->height || channels != channels0 )

            changed_size = true;

            uchar buffer[sizeof(BITMAPINFO) + 255*sizeof(RGBQUAD)];
            binfo = (BITMAPINFO*)buffer;

            BOOL bDeleteObj = DeleteObject(GetCurrentObject(hdc, OBJ_BITMAP));
                    CV_Assert( FALSE != bDeleteObj );

   = image->width;
   = image->height;
            channels = channels0;

            OpenCV::FillBitmapInfo( binfo,,, channels*8, channels );

            SelectObject( hdc, CreateDIBSection( hdc, binfo, DIB_RGB_COLORS, &dst_ptr, 0, 0));

        cvInitMatHeader( &dst,,, CV_8UC3, dst_ptr, ( * channels + 3) & -4 );
        //TODO QUYETNM This line causes memory leak!!!
        cvConvertImage( image, &dst, origin == 0 ? CV_CVTIMG_FLIP : 0 );

        // Image stretching to fit the window
        RECT rect;
        ::GetClientRect(w_hWnd, &rect);
        StretchDIBits( hdc, 0, 0, rect.right, rect.bottom, 0, 0, image->width, image->height, dst_ptr, binfo, DIB_RGB_COLORS, SRCCOPY );
        // only resize window if needed
        ::InvalidateRect(w_hWnd, 0, 0);
        //Release resource


    void OpenCV::FillBitmapInfo( BITMAPINFO* bmi, int width, int height, int bpp, int origin )
        assert( bmi && width >= 0 && height >= 0 && (bpp == 8 || bpp == 24 || bpp == 32));

        BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);

        memset( bmih, 0, sizeof(*bmih));
        bmih->biSize = sizeof(BITMAPINFOHEADER);
        bmih->biWidth = width;
        bmih->biHeight = origin ? abs(height) : -abs(height);
        bmih->biPlanes = 1;
        bmih->biBitCount = (unsigned short)bpp;
        bmih->biCompression = BI_RGB;

        if( bpp == 8 )
            RGBQUAD* palette = bmi->bmiColors;
            int i;
            for( i = 0; i < 256; i++ )
                palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
                palette[i].rgbReserved = 0;
2014-06-11 22:39:52 -0600 commented answer Win32 GUI application using OpenCV lib

I know it works but the call to cvNameWindow leaves a blink window (show a gray one and immediately disappear after that). How can I fix that?

2014-06-10 21:51:09 -0600 commented answer Win32 GUI application using OpenCV lib

The technique used SW_HIDE flag left a blink window. How can I fix that?

2014-06-10 20:53:44 -0600 commented answer Win32 GUI application using OpenCV lib

Thanks for your answer. But I want to build a win32 API based application, it will have openCV as a processor for images / video frames and the win32 will take the openCV's output as input and display it to the screen. Or some functions like cvShowImage() which can take a HWND as a parameter to specify which windows I want to display the images. I found one at opencv/sources/modules/highgui/src/window_win32 but I don't know how to include / call that function

2014-06-09 23:21:22 -0600 commented question Win32 GUI application using OpenCV lib

Can someone help me please. The image display by cvShowImage() or imshow() always create a new windows instead of old one. I tried getWindowName() of the old window, then pass to cvShowImage / imshow but it create a new one with same title. ZZ

2014-06-09 05:12:12 -0600 asked a question Win32 GUI application using OpenCV lib

Dear all, I'm building a media player app using Win32 GUI (The win32 application). I have stuck at displaying images / video on win32 (which considered easy when dealing with win32 console as we can use built-in function of OpenCV highgui). After days of searching google, I found that win32 accept bitmap image and using draw tool, device context to display image. So how do I use processed data in OpenCV (Mat, IplImage object) as input for win32 application? Please give me an example / instructions / link of how to do it. Thanks in advance.