Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 41027

Possible memory leak cause - Videocapture ??

$
0
0
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 #include using namespace std; #include "cameraCore.h" #include "opencv2/highgui/highgui.hpp" #include 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(status == 0) { cout << "!!!CIamge capture success!!!" << endl; } else { cout << "!!!CIamge capture failure!!! Error val "<< status << endl; failCount++; if(failCount>2) break; } Sleep(0.6*1000); captureCount++; } newCamHandle->closeCamhandle(); delete(newCamHandle); Sleep(30*1000); return 0; } Header file #ifndef CAMERACLASS_H_ #define CAMERACLASS_H_ //#include "opencv2/highgui/highgui.hpp" //#include "opencv2/highgui/highgui.hpp" //using namespace cv; class cameraClass { public: //VideoCapture cap; int frameWidth; int frameHeight; int cameraModule; cameraClass() { cameraModule=0; frameWidth=320; frameHeight=240; } cameraClass(int cameraModel) { cameraModule=cameraModel; frameWidth=320; frameHeight=240; } cameraClass( int frameWid, int frameHght) { frameWidth=frameWid; frameHeight=frameHght; } cameraClass(int camModel, int frameWid, int frameHght) { cameraModule=camModel; frameWidth=frameWid; frameHeight=frameHght; } int captureImage(char *imageName, int roatation); bool setCamhandle(int camId); int closeCamhandle(); }; #endif /* CAMERACLASS_H_ */

Viewing all articles
Browse latest Browse all 41027

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>