Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6846037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:34:26+00:00 2026-05-27T00:34:26+00:00

I have recently been doing some circle detection in OpenCV and I have had

  • 0

I have recently been doing some circle detection in OpenCV and I have had some great success. The code below works wonderfully in C++ on a 32-bit platform. I do however have one small problem; as the program runs my RAM loads continues to load up. For example, when I start the program I am at 250mb, 30 seconds later I am at about 800mb. This continues and eventually overflows to the hard drive. I have tried to move the cvReleaseCapture inside the while loop but I just get the old “Exception at memory location….”. Any ideas how I can clean this up? Thanks a lot. Here’s the code:

#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
#include <math.h>


int main( int argc, char **argv )
{
    CvCapture *capture = 0;
    IplImage  *img = 0;
    int       key = 0;
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_PLAIN,1.0,1.0,0,1,CV_AA);

capture = cvCaptureFromCAM( 0 );

if ( !capture ) {
    fprintf( stderr, "Cannot open initialize webcam!\n" );
    return 1;
} 

cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );

while( key != 'q' ) {
    img = cvQueryFrame( capture ); 
    IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
    CvMemStorage* storage = cvCreateMemStorage(0);
    cvCvtColor( img, gray, CV_BGR2GRAY );
    cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 );
    CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray-    >height/4, 200, 100, 20, 100 );
    int i;
    for( i = 0; i < circles->total; i++ )
    {
         float* p = (float*)cvGetSeqElem( circles, i );
         //cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), 2, CV_RGB(0,255,0), -1, 8, 0 );
         cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(0,255,0), 2, 8, 0 );
         cvLine (img, cvPoint(cvRound(p[0]+40),cvRound(p[1])), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
         cvLine (img, cvPoint(cvRound(p[0]),cvRound(p[1]+40)), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
         cvLine (img, cvPoint(cvRound(p[0]-40),cvRound(p[1])), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
         cvLine (img, cvPoint(cvRound(p[0]),cvRound(p[1]-40)), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
         cvPutText(img, "Meow",cvPoint(cvRound(p[0]+45),cvRound(p[1]+45)), &font, CV_RGB(0,0,255)); 

    }


    if( !img ) break;       
    cvShowImage( "result", img ); 
    key = cvWaitKey( 1 );
    cvReleaseCapture( &capture );
}

cvDestroyWindow( "result" );
cvReleaseCapture( &capture );    

return 0;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:34:26+00:00Added an answer on May 27, 2026 at 12:34 am

    Get rid of the cvReleaseCapture inside the while loop. Instead, you should have

    cvReleaseMemStorage(storage);
    cvReleaseImage(gray);
    

    And you should probably move the if (!img) break; line up so it reads:

    img = cvQueryFrame(capture);
    if (!img) break;
    

    Edit

    Actually, instead of constantly creating and then destroying gray, you should do an initial capture (img = cvQueryFrame(capture);) outsid eof the while loop. Then create gray in the normal way. Then you don’t have to keep creating it in the while loop, which is why you’re running out of memory (you’re creating an new image every iteration without releasing it).

    So

    #include "stdafx.h"
    #include <cv.h>
    #include <highgui.h>
    #include <math.h>
    
    
    int main( int argc, char **argv )
    {
        CvCapture *capture = 0;
        IplImage  *img = 0;
        int       key = 0;
        CvFont font;
        cvInitFont(&font, CV_FONT_HERSHEY_PLAIN,1.0,1.0,0,1,CV_AA);
    
        capture = cvCaptureFromCAM( 0 );
    
        if ( !capture ) {
            fprintf( stderr, "Cannot open initialize webcam!\n" );
            return 1;
        } 
    
        cvNamedWindow( "result", CV_WINDOW_AUTOSIZE );
    
        img = cvQueryFrame( capture );
        if (!img) 
            exit(1);
        IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 );
        CvMemStorage* storage = cvCreateMemStorage(0);
    
        while( key != 'q' ) {
            img = cvQueryFrame( capture ); 
            if( !img ) break;  
    
    
            cvCvtColor( img, gray, CV_BGR2GRAY );
            cvSmooth( gray, gray, CV_GAUSSIAN, 9, 9 );
            CvSeq* circles = cvHoughCircles( gray, storage, CV_HOUGH_GRADIENT, 2, gray-        >height/4, 200, 100, 20, 100 );
            int i;
            for( i = 0; i < circles->total; i++ )
            {
                float* p = (float*)cvGetSeqElem( circles, i );
                //cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), 2, CV_RGB(0,255,0), -1, 8, 0 );
                cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])), cvRound(p[2]), CV_RGB(0,255,0), 2, 8, 0 );
                cvLine (img, cvPoint(cvRound(p[0]+40),cvRound(p[1])), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
                cvLine (img, cvPoint(cvRound(p[0]),cvRound(p[1]+40)), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
                cvLine (img, cvPoint(cvRound(p[0]-40),cvRound(p[1])), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
                cvLine (img, cvPoint(cvRound(p[0]),cvRound(p[1]-40)), cvPoint(cvRound(p[0]),cvRound(p[1])), CV_RGB(0,255,0), 1, CV_AA,0);
                cvPutText(img, "Meow",cvPoint(cvRound(p[0]+45),cvRound(p[1]+45)), &font, CV_RGB(0,0,255)); 
    
            }
    
            cvShowImage( "result", img ); 
            key = cvWaitKey( 1 );
    
        }
        cvReleaseMemStorage(storage);
        cvReleaseImage(gray);
        cvDestroyWindow( "result" );
        cvReleaseCapture( &capture );    
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently put up a site and I have been doing some SEO. However
I have an old MonoRail/ActiveRecord I've been doing some work too. Recently I decided
I have been doing some statistics work with Stata recently and not enjoying it
I've been doing some reading recently and have encountered the Law of Demeter. Now
I have recently been doing some work that has been quite in depth, i
I have been doing some work with open gl on the android platform recently
I have recently been doing a bit of investigation into the different types of
I've not used C++ very much in the past, and have recently been doing
Recently I've been doing lots of weekend coding, and have began to really need
I have been running into some troubles recently and I think I need your

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.