Ask Your Question

ragav's profile - activity

2015-08-07 04:59:39 -0600 received badge  Editor (source)
2015-08-06 23:54:17 -0600 asked a question Possible memory leak cause - Videocapture ??

I am trying to use opencv for simple image capture and saving the image. I am fairly new to OPENCV and currently using 2.4.11 version.

It has been noted whenever i close my camera and open it - i notice memory leak and thread count increase in windows resource monitor.However, if i open my camera once and capture images i do not notice leak in the system. so i doubt videocapture. Request some lights in addressing this leak.

However, after fixing this leak will have to create a CPP wrapper to access api via C

Platform : Win 7 32 bit compiler : Mingw Gcc OPenCv : 2.4.11

#include <iostream>
#include <stdio.h>
using namespace std;
#include "cameraCore.h"
#include "opencv2/highgui/highgui.hpp"
#include <windows.h>
using namespace cv;
VideoCapture *cap = NULL;
int cameraClass::closeCamhandle()
{

        if(cap != NULL)
        {
            if (cap->isOpened() )  // if not success, exit program
            {
                cap->release();
            }
            delete(cap);
            cap = NULL;
        }
        return 0;

}

bool cameraClass::setCamhandle(int camId)
{
     bool bSuccess= FALSE;
     try
     {
        cap=new VideoCapture();
        bSuccess=cap->open(camId);
        if(bSuccess == TRUE)
        {
            cout << "!!!Camera open success!!!" << endl;
        }else{
            delete(cap);
            cap=NULL;
            cout << "!!!Camera open FASILURE!!!" << endl;
        }
    }
    catch ( cv::Exception & e )
    {
          const char* err_msg = e.what();
          cout << "exception caught: " << err_msg << endl;
          if(cap != NULL){
              delete(cap);
              cap=NULL;
          }
    }
    return bSuccess;
}

int cameraClass::captureImage(char *imageName, int roatation)
{
     try
     {
         cout << "ImageName " << imageName << endl;
         char image[50] ={0};
        if (strlen(imageName) > 1 )
        {
            strcpy(image,imageName);
        }
        bool bSuccess= FALSE;
        if (!cap->isOpened())  // if not success, exit program
        {
            cout  << "Cannot open the video file" << endl;
            return -1;
        }
        double dWidth = cap->get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
        double dHeight = cap->get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the  video
        cout << "Frame size : " << dWidth << " x " << dHeight << endl;
        if (dWidth != frameWidth)
        {
            cap->set(CV_CAP_PROP_FRAME_WIDTH,frameWidth);
        }
        if (dHeight != frameHeight)
        {
            cap->set(CV_CAP_PROP_FRAME_HEIGHT,frameHeight);
        }
       Mat *frame = new Mat;
       int frameCount = 20;
       while (!bSuccess && frameCount)
       {
           cout << "Data for fame missing " << frameCount << endl;
           bSuccess = cap->grab();
           if(bSuccess)
           {
               bSuccess = cap->retrieve(*frame, 0);
               if(bSuccess == FALSE)
               {
                  delete(frame);
                  frame=NULL;
                  frame = new Mat;
               }
           }
           if(bSuccess)
               break;
           frameCount--;
           Sleep(5);
       }
       if (!bSuccess) //if not succes->, break loop
        {
         cout << "Cannot read a frame from video file" << endl;
         frame->release();
         delete(frame);
         return -2;
        }

       if (frame != NULL){
           bSuccess=imwrite(image,*frame);
           frame->release();
           delete(frame);
           frame=NULL;
       }
       if(bSuccess == TRUE)
           return 0;

       return -3;
     }
    catch ( cv::Exception & e )
    {
          const char* err_msg = e.what();
          cout << "exception caught: " << err_msg << endl;
    }
    return -3;
}


int main()
{
    int camOpenStatus = FALSE;
    int status=0;
    cameraClass *newCamHandle = new cameraClass(0,320,240);
    int captureCount=1;
    int failCount=0;
    char name[30] = {0};
    Sleep(5*1000);
    while (captureCount != 400)
    {

        if(captureCount % 13 == 0  || camOpenStatus == FALSE)
        {
            newCamHandle->closeCamhandle();
            Sleep(1*1000);
            camOpenStatus=newCamHandle->setCamhandle(0);
            if(camOpenStatus == TRUE)
            {
                cout << "!!!Cam handle success!!!" << endl;
                failCount=0;
            }
            else
            {
                cout << "!!!CAM handle failure!!!" << endl;
                failCount++;
                if(failCount>5)
                {
                    cout << "!!!Uanable to camera handle open limit exceeds!!!" << endl;
                    break;
                }
                continue;
            }
        }
        if(captureCount % 4 == 0)
            Sleep(2*1000);
        sprintf(name,"photo%d.jpg",captureCount);
        status = newCamHandle->captureImage(name,0);
        if ...
(more)