How to use ofxOpenCv Sobel, Canny methods and the like?

Is there an easy way of using ofxOpenCv Sobel, Canny methods?

if I type cvCanny I get the following code hints

  
cvCanny(const CvArr *image, CvArr *edges, double threshold1, double threshold2);  

But I have no idea how to get those values to feed the method so I wonder if there is an easy native OF way of doing such thing?

Any help will be much appreciated

say you have these two ofxCvGrayscaleImage objects:

  
  
ofxCvGrayscaleImage gray;  
ofxCvGrayscaleImage canny;  
  

make sure you have first allocated them to the right size:

  
  
gray.allocate(w, h);  
canny.allocate(w, h);  
  

then in your update loop call:

  
  
int minVal = 60;   
int maxVal = 150;   
cvCanny(gray.getCvImage(), canny.getCvImage(), minVal,  maxVal, 3);  
canny.flagImageChanged();  
  

the canny.flagImageChanged() is important as it tells ofxOpenCv that something was done in opencv directly to the image and it should be updated.
The min and max values define how many edges are picked up.
Definitely worth playing with those values.

Hope that helps!
Theo

Very straight forward!

Thanks a lot Theo

Hi Theo,

Just trying out your example code, unfortunately not working, I get the following error message:

OpenCV Error: Null pointer (NULL array pointer is passed) in cvGetMat, file /Users/theo/Downloads/OpenCV-2.3.1/modules/core/src/array.cpp, line 2382
libc++abi.dylib: terminate called throwing an exception
sharedlibrary apply-load-rules all
[unknown]kill

any ideas?

I think you might be calling the newer API, i.e. cv::canny rather than cvCanny which is what ofxOpenCv wants? Because one wants mats and the other wants CvArr*

I’m actually using the same code that Theo post on his reply cvCanny

Hmm. Well, something must not be getting initialized because the code Theo posted looks right to me, and this is from one of my own projects that I just re-ran to sanity check that it still works:

  
  
        ofxCvGrayscaleImage canny;  
        canny.allocate(grayImage.getWidth(), grayImage.getHeight());  
        cvCanny(grayImage.getCvImage(), canny.getCvImage(), 50.0, 120.0);