How to free memory used by a seq which used to store a contour in opencv 2.4.3 -
this code used capture cam , on each frame convert hsv image apply color segmentation on find red, greenish , bluish objects find contour of each , store in cvseq.
the code working problem after 10 minutes throws run time exception because no memory can allocate new contour although utilize cvclearmemstorage()
release memory.
#include <cv.h> #include <highgui.h> cvseq *contours; iplimage* frame=0; cvmemstorage* g_storage; //this function threshold hsv image , create binary image iplimage* getthresholdedredimage(iplimage* imghsv) { iplimage* imgthresh = cvcreateimage(cvgetsize(imghsv),ipl_depth_8u, 1); cvinranges(imghsv, cvscalar(160,160,60), cvscalar(180,256,256), imgthresh); //cvmemstorage* g_storage = cvcreatememstorage(0); cvfindcontours( cvcloneimage(imgthresh), g_storage, &contours ); if( contours ) { cvdrawcontours(frame, contours, cvscalar(0, 255, 0), cvscalarall(255), 100); cvclearseq(contours); } cvclearmemstorage(g_storage); homecoming imgthresh; } iplimage* getthresholdedgreenimage(iplimage* imghsv) { iplimage* imgthresh=cvcreateimage(cvgetsize(imghsv),ipl_depth_8u, 1); cvinranges(imghsv, cvscalar(38,160,60), cvscalar(75,256,256), imgthresh); //cvmemstorage* g_storage = cvcreatememstorage(0); cvfindcontours( cvcloneimage(imgthresh), g_storage, &contours ); if( contours ) { cvdrawcontours(frame, contours, cvscalar(0, 255, 255), cvscalarall(255), 100); cvclearseq(contours); } cvclearmemstorage(g_storage); homecoming imgthresh; } iplimage* getthresholdedblueimage(iplimage* imghsv) { iplimage* imgthresh=cvcreateimage(cvgetsize(imghsv),ipl_depth_8u, 1); cvinranges(imghsv, cvscalar(75,160,60), cvscalar(130,256,256), imgthresh); //cvmemstorage* g_storage = cvcreatememstorage(0); cvfindcontours( cvcloneimage(imgthresh), g_storage, &contours ); if( contours ) { cvdrawcontours(frame, contours, cvscalarall(255), cvscalarall(255), 100); cvclearseq(contours); } cvclearmemstorage(g_storage); homecoming imgthresh; } int main() { cvcapture* capture =0; capture = cvcapturefromcam(0); if(!capture) { printf("capture failure\n"); homecoming -1; } //iplimage* frame=0; cvnamedwindow("video"); cvnamedwindow("red"); //cvnamedwindow("yellow"); //cvnamedwindow("orange"); cvnamedwindow("green"); cvnamedwindow("blue"); //cvnamedwindow("violet"); g_storage = cvcreatememstorage(0); //iterate through each frames of video while(true) { frame = cvqueryframe(capture); if(!frame) break; frame = cvcloneimage(frame); cvsmooth(frame, frame, cv_gaussian,3,3); //smooth original image using gaussian kernel iplimage* imghsv = cvcreateimage(cvgetsize(frame), ipl_depth_8u, 3); cvcvtcolor(frame, imghsv, cv_bgr2hsv); //change color format bgr hsv iplimage* redimgthresh = getthresholdedredimage(imghsv); iplimage* blueimgthresh = getthresholdedblueimage(imghsv); iplimage* greenimgthresh = getthresholdedgreenimage(imghsv); //cvclearseq(contours); cvsmooth(redimgthresh, redimgthresh, cv_gaussian,3,3); //smooth binary image using gaussian kernel cvsmooth(blueimgthresh, blueimgthresh, cv_gaussian,3,3); cvsmooth(greenimgthresh, greenimgthresh, cv_gaussian,3,3); cvshowimage("red", redimgthresh); cvshowimage("blue", blueimgthresh); cvshowimage("green", greenimgthresh); cvshowimage("video", frame); //clean used images cvreleaseimage(&imghsv); cvreleaseimage(&redimgthresh); cvreleaseimage(&blueimgthresh); cvreleaseimage(&greenimgthresh); cvreleaseimage(&frame); //wait 50ms int c = cvwaitkey(10); //if 'esc' pressed, break loop if((char)c==27 ) break; } cvdestroyallwindows() ; cvreleasecapture(&capture); homecoming 0; }
one part of code not is:
frame = cvcloneimage(frame); ... cvreleaseimage(&frame);
since, highgui.h
/* combination of cvgrabframe , cvretrieveframe !!!do not release or modify retrieved frame!!! */ cvapi(iplimage*) cvqueryframe( cvcapture* capture );
i suggest using different pointer retrieve image , cloning one.
iplimage* tempframe = cvcloneimage(frame); frame = cvcloneimage(tempframe); ... cvreleaseimage(&frame);
also, read: opencv introduction
see working video sequences section.
opencv
No comments:
Post a Comment