[SOLVED] App crash when using cvDilate() in openCV

hello,

i am facing a problem. I am using a ofxKinect to grab a depth image and ofxOpenCV to detect blobs.

Here is a part of the code :

void kinectTracker::update() {
    
    kinect.update();
    
    if(kinect.isFrameNew())
    {
        
        
        
        //////////////////////////////////////////
        // TODO : threshNear, threshFar, mirror //
        //////////////////////////////////////////
        
        
        
        // load grayscale depth image from the kinect source
        depthImage.setFromPixels(kinect.getDepthPixels(), kinect.width, kinect.height);
        
        // ---------- ROI -----------
        CvRect cvROI = cvRect(roi.x,roi.y,roi.width,roi.height);
        cvSetImageROI(depthImage.getCvImage(),cvROI);
        
        //thresholdImage = depthImage;
        depthImage.threshold(threshold);
        
        // update the cv images
        depthImage.flagImageChanged();
        
        //find blob in ROI
        contourFinder.findContours(depthImage, minBlobSize, roi.width*roi.height, 1, false);
    }
    
    if (contourFinder.nBlobs > 0 && contourFinder.blobs[0].area > minBlobSize)
    {
        // Optimize blob options
        if (bDilate){
            for(int i = 0; i < 10; i++){
                depthImage.dilate();    // APP CRASH HERE !!!
            }
        }
        
        if (bErode) {
            for(int i = 0; i < 10; i++){
                depthImage.erode();     // APP CRASH HERE !!!
            }
        }
        
        pos = contourFinder.blobs.at(0).centroid;
        pos.z = kinect.getDistanceAt(pos.x, pos.y);
    }
}

i was trying to optimize blob detection by using dilate() and erode() methods but the app crashes when i activate one of those.

I get a signal SIGABRT error :

OpenCV Error: Assertion failed (src.size() == dst.size() && src.type() == dst.type()) in cvDilate, file /Users/theo/Downloads/OpenCV-2.3.1/modules/imgproc/src/morph.cpp, line 1232
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /Users/theo/Downloads/OpenCV-2.3.1/modules/imgproc/src/morph.cpp:1232: error: (-215) src.size() == dst.size() && src.type() == dst.type() in function cvDilate

it seems like there is a problem with image size or image type but i can’t figure out why !

If you need more code, please see the github page :

thanks a lot for any help you may give

Seems like issue is due to ROI management…

Am i doing it right ?

replaced cvRect for ROI by ofRectangle.
seems better now.

but now have the same problem when using cvAnd()

seems there is a problem with everything relative to cv methods.

here is my ROI class :

class ROI : public ofBaseDraws, public ofRectangle {
    
public:
    void draw(float _x,float _y) {
        draw(_x, _y, getWidth(), getHeight());
    }
    
    void draw(float _x,float _y,float w, float h)  {
        
        ofPushMatrix();
        
        ofTranslate(_x, _y);
        ofScale(w/CAM_WIDTH, h/CAM_HEIGHT);
        
        ofPushStyle();
        ofSetColor(255, 0, 0);
        ofNoFill();
        ofSetLineWidth(1);
        ofRect(*this);
        ofPopStyle();
        
        ofPopMatrix();
    }
    
    float getWidth()  {
        return CAM_WIDTH;
    }
    
    float getHeight()  {
        return CAM_HEIGHT;
    }
};

and my update() code where openCV processes images :

// load grayscale depth image from the kinect source
depthImage.setFromPixels(kinect.getDepthPixels(), kinect.width, kinect.height);

// ---------- ROI -----------
ofRectangle roiMat = ofRectangle(roi.x, roi.y, roi.width, roi.height);
depthImage.setROI(roiMat);

//thresholdImage = depthImage;
//depthImage.threshold(threshold);
threshNear = depthImage;
threshFar = depthImage;
threshNear.threshold(nearThreshValue, true);
threshFar.threshold(farThreshValue);

// APP CRASH HERE !
cvAnd(threshNear.getCvImage(), threshFar.getCvImage(), depthImage.getCvImage(), NULL); 
    

// update the cv images
depthImage.flagImageChanged();

//find blob in ROI
contourFinder.findContours(depthImage, minBlobSize, roi.width*roi.height, 1, false);

by the way, when commenting out the ROI part :

ofRectangle roiMat = ofRectangle(roi.x, roi.y, roi.width, roi.height);
depthImage.setROI(roiMat);

i don’t have SIGABRT error anymore…

OK. Wasn’t applying ROI to thresholded image.
problem solved.