Thread: running out of memory? blobextraction

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    Exclamation running out of memory? blobextraction

    hey
    Im fairly new at C++. Im doing a project where i need to do some blobdetection, but its giving me trouble. Im using the grassfire method to register the blob. but the problem happens when i reach an end and starts backtracking in order to get every connected pixel
    im using openCV
    there is no error message the program just stops working
    can anyone tell my whats wrong because i just cant see it.
    Code:
    #include "testApp.h"
    
    //--------------------------------------------------------------
        struct coordinate
        {
            unsigned int x, y;
            void * data;
        };
    
        struct blob
        {
            coordinate min, max;
    
            coordinate center;
            int area;
    
        };
    void checkblob(unsigned int row,unsigned int coloumn,unsigned int karen[][448]);
    void testApp::setup(){
    
    
        //loading and converting the image to grayscale
        testImage.loadImage("fabia.jpg");
        farveTest.allocate(testImage.getWidth(),testImage.getHeight());
        farveTest.setFromPixels(testImage.getPixels(),testImage.getWidth(),testImage.getHeight());
        shTest.allocate(testImage.getWidth(),testImage.getHeight());
        shTest = farveTest;
        tresholdTest.allocate(testImage.getWidth(),testImage.getHeight());
        outputPic.allocate(testImage.getWidth(),testImage.getHeight());
        IplImage* cvImg = shTest.getCvImage();
        IplImage* cvImg2 = tresholdTest.getCvImage();
        IplImage* cvImg3 = outputPic.getCvImage();
        int hoejde = shTest.getHeight();
        int bredde = shTest.getWidth();
        //tresholding and converting my image to a 2d array
        unsigned int myArray[336][448];
        for(int y = 0; y < cvImg->height; y ++)
        {
            uchar* ptr = (uchar*)(cvImg->imageData + y * cvImg->widthStep);
            uchar* ptr2 = (uchar*)(cvImg2->imageData + y * cvImg2->widthStep);
            for(int x = 0; x <cvImg->width; x ++)
            {
                if(ptr[x] < 200)
                {
                    ptr2[x]= 0;
                    myArray[y][x] = 0;
                }
                else
                {
                    ptr2[x] = 255;
                    myArray[y][x] = 255;
                }
            }
        }
        //running checkblob function on white pixels
        for(unsigned int y=0;y<336;y++)
        {
            for(unsigned int x = 0;x<448;x++)
            {
                if(myArray[y][x] == 255)
                {
                    myArray[y][x]=5;
                    checkblob(y,x,myArray);
                }
            }
        }
    }
    
    //--------------------------------------------------------------
    void testApp::update(){
    
    }
    
    //--------------------------------------------------------------
    void testApp::draw(){
        shTest.draw(testImage.getWidth(),0);
        farveTest.draw(0,0);
        tresholdTest.draw(0,testImage.getHeight());
        outputPic.draw(testImage.getWidth(),testImage.getHeight());
    }
    
    //--------------------------------------------------------------
    void testApp::keyPressed(int key){
    
    }
    
    //--------------------------------------------------------------
    void testApp::keyReleased(int key){
    
    }
    
    //--------------------------------------------------------------
    void testApp::mouseMoved(int x, int y ){
    
    }
    
    //--------------------------------------------------------------
    void testApp::mouseDragged(int x, int y, int button){
    
    }
    
    //--------------------------------------------------------------
    void testApp::mousePressed(int x, int y, int button){
    
    }
    
    //--------------------------------------------------------------
    void testApp::mouseReleased(int x, int y, int button){
    
    }
    
    //--------------------------------------------------------------
    void testApp::windowResized(int w, int h){
    
    }
    
    void checkblob(unsigned int row,unsigned int coloumn,unsigned int karen[][448])
    {
        int blobid;
        vector< vector<coordinate> > blobs(20);
        
        if(karen[row][coloumn]==255)
        {
            karen[row][coloumn]=5;
            coordinate coordData = {coloumn,row};
            blobs[blobid].push_back(coordData);
        }
    
        int treshold = 255;
        if(karen[row][(coloumn+1)] == 255)
        {
            karen[row][(coloumn+1)] = 1;
            coordinate coordData = {coloumn+1,row};
            blobs[blobid].push_back(coordData);
            checkblob(row,(coloumn+1),karen);
    
        }
        else if(karen[(row+1)][coloumn] == treshold)
        {
            karen[(row+1)][coloumn] = 2;
            coordinate coordData = {coloumn,row+1};
            blobs[blobid].push_back(coordData);
            checkblob((row+1),coloumn,karen);
        }
        else if(karen[row][(coloumn-1)] == treshold)
        {
            karen[row][(coloumn-1)] = 3;
            coordinate coordData = {coloumn-1,row};
            blobs[blobid].push_back(coordData);
            checkblob(row,(coloumn-1),karen);
        }
        else if(karen[(row-1)][coloumn] == treshold)
        {
            karen[(row-1)][coloumn] = 4;
            coordinate coordData = {coloumn,row-1};
            blobs[blobid].push_back(coordData);
            checkblob((row-1),coloumn,karen);
    
        }
        else
        {
            //backtracking to find every pixel in the blob.
           switch(karen[row][coloumn])
           {
               case 1:
               checkblob(row,(coloumn-1),karen);
               break;
               case 2:
               checkblob(row-1,coloumn,karen);
               break;
               case 3:
               checkblob(row,(coloumn+1),karen);
               break;
               case 4:
               checkblob(row+1,coloumn,karen);
               break;
               case 5:
               blobid++;
               break;
               default:
               blobid++;
               cout<<blobid<<endl;
               break;
           }
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
        int blobid;
        vector< vector<coordinate> > blobs(20);
        
        if(karen[row][coloumn]==255)
        {
            karen[row][coloumn]=5;
            coordinate coordData = {coloumn,row};
            blobs[blobid].push_back(coordData);
    Maybe you should initialise variables before using them?

    Also, whatever you do to blobs is LOST when the function returns. It is a local variable.

    Did you get any warnings from the compiler?

    > unsigned int myArray[336][448];
    This is just over 0.5MB in size (by itself)
    Bear in mind that default stack sizes could be as low as 1MB. If this is all there is, you should be fine.
    But if nested functions also have similarly large arrays, you could be stuck.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It looks to me like you minunderstand the idea of backtracking.
    You don't need four additional calls after the four initial calls.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory not released after running program
    By lehe in forum Linux Programming
    Replies: 21
    Last Post: 01-01-2011, 03:32 AM
  2. Help with insert/delete binary search tree
    By Nazgulled in forum C Programming
    Replies: 39
    Last Post: 03-25-2009, 04:24 PM
  3. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM

Tags for this Thread