Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

EXC_BAD_ACCESS when passing image to class method and trying to set instance ofImage with setFromPixels

$
0
0

@johndryan wrote:

I’m pretty new to OpenFrameworks and C++, so this comes with the usual caveats that I’m probably not understanding pointers and references correctly… or something along those lines.

I’m building an openCV app that recognizes letter shapes on a wall.

I have a Letter class that is managed by ofxCV’s RectFollower, so that a new instance is created to track each blob/contour. I want to pass the webcam image through to a class method, where I will crop it and save to a local ofImage variable for later display/classification.

Here’s the relevant code from the class and ofApp.

Letter.h

#include "ofMain.h"
#include "ofxOpenCv.h"
#include "ofxCv.h"

using namespace ofxCv;
using namespace cv;

class Letter : public ofxCv::RectFollower {
protected:
    ofImage img;
    cv::Rect rect;
public:
    Letter(){}
    void setup(const cv::Rect& track);
    void setImage(const ofxCvColorImage * camImage);
    void classify();
    void update(const cv::Rect& track);
    void kill();
    void draw();
};

Letter.cpp

#include "letter.h"

void Letter::setup(const cv::Rect& track) {
    rect = track;
    // Does img need to be allocated?
    // img.allocate(640, 480, OF_IMAGE_COLOR);
}

void Letter::setImage(const ofxCvColorImage * camImage) {
    img.setFromPixels(camImage->getPixels());
    img.crop(rect.x, rect.y, rect.width, rect.height);
}

// … rest of class methods ignored for brevity …

ofApp.cpp

// … rest of file ignored for brevity …
//--------------------------------------------------------------
void ofApp::update(){
    if (!bPause) {
    cam.update();
    }
    if(cam.isFrameNew())
    {
        // get grayscale image and threshold
        colorImage.setFromPixels(cam.getPixels());
       // …other CV stuff ignored for brevity …

        letterTracker.track(contourFinder.getBoundingRects());
        
        // update new Letters with image
        vector<Letter>& letters = letterTracker.getFollowers();
        const vector<unsigned int>& newLabels = letterTracker.getNewLabels();
        for(int i = 0; i < newLabels.size(); i++) {
            const int index = letterTracker.getIndexFromLabel(newLabels[i]);
            // HERE'S WHERE THIS METHOD IS CALLED:
            letters[index].setImage(&colorImage);
        }
    }
}
// … rest of file ignored for brevity …

It looks like usually the first instance works ok, and then on a subsequent instance I get Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) when img.setFromPixels(camImage->getPixels()); is called. If I trace it through the thread you see the following:

What am I missing?!

Full code is on GitHub here: https://github.com/johndryan/Sugraph

Thanks in advance for any help or pointers (excuse the pun).

Posts: 3

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles