OpenCV cvSobel question

Hi,

I am trying to implement the cvSobel filter, here is my code:

  
cvSobel( grayImage.getCvImage(), grayImage.getCvImage(), 0, 1, 3 );  

But I get this error:
ofBaseVideoPlayer::setPixelFormat not implemented
OpenCV Error: Assertion failed (src.size() == dst.size() && src.channels() == dst.channels() && ((src.depth() == CV_8U && (dst.depth() == CV_16S || dst.depth() == CV_32F)) || (src.depth() == CV_32F && dst.depth() == CV_32F))) in cvSobel, file /Users/theo/Documents/CODE/__OPENFRAMEWORKS/gitOF/__BuildAllLibs/OpenCV-2.2.0/modules/imgproc/src/deriv.cpp, line 347
terminate called after throwing an instance of ‘cv::Exception’
what(): /Users/theo/Documents/CODE/__OPENFRAMEWORKS/gitOF/__BuildAllLibs/OpenCV-2.2.0/modules/imgproc/src/deriv.cpp:347: error: (-215) src.size() == dst.size() && src.channels() == dst.channels() && ((src.depth() == CV_8U && (dst.depth() == CV_16S || dst.depth() == CV_32F)) || (src.depth() == CV_32F && dst.depth() == CV_32F)) in function cvSobel

Any help will be much appreciated

  • rS

Found this http://opencv-users.1802565.n2.nabble.com/Assertion-failed-td4594364.html

I quote

Try making edgeImage an image with depth = IPL_DEPTH_16S instead.

How do I do that?

Cheers

  • rS

basically you have 2 options to make you own depths:

  1. use cvCreateImage() directly

  2. extend ofxCvImage

OpenCV functions can be picky (not to say annoying :slight_smile: ) with image formats…

Hi naus3a,

I follow the sobel example from this projects, http://forum.openframeworks.cc/t/opencv-basics—sources-for-beginners/2738/0

  
[bytesRequired];  
int bytesRemaining = bytesRequired;  
// loop until we've read everything  
while ( bytesRemaining > 0 ){  
  // check for data  
  if ( serial.available() > 0 )  
  {  
    // try to read - note offset into the bytes[] array, this is so  
    // that we don't overwrite the bytes we already have  
    int bytesArrayOffset = bytesRequired - bytesRemaining;  
    int result = serial.readBytes( &bytes[bytesArrayOffset], bytesRemaining );  
  
    // check for error code  
  
    if ( result == OF_SERIAL_ERROR )  
  
    {// something bad happened  
      ofLog( OF_LOG_ERROR, "unrecoverable error reading from serial" );  
      break;      // bail out  
    }//end serial error  
  
    else if ( result == OF_SERIAL_NO_DATA )  
    {  
		ofLog( OF_LOG_ERROR, "no data error" );  
		// nothing was read, try again  
    }//end no data  
    else{  
      // we read some data!  
      bytesRemaining -= result;  
	  printf("we read %i bytes successfully, %i remaining  \n", result, bytesRemaining);  
    }  
  }//end serial avaliable  
	else{  
	printf("serial not available");  
	}  
}  

  
// we will try to read N bytes, as much as we can  
		// otherwise, we may have a "lag" if we don't read fast enough  
		// or just read three every time. now, we will be sure to   
		// read as much as we can in groups of three...  
  
int byesToRead = 36;//can vary the chunk of bytes to read in   
		nTimesRead = 0;  
		nBytesRead = 0;  
		int nRead  = 0;  // a temp variable to keep count per read  
		  
		unsigned char bytesReturned[byesToRead];  
  
		memset(bytesReadString, 0, byesToRead+1);  
		memset(bytesReturned, 0, byesToRead);  
		  
		while( (nRead = serial.readBytes( bytesReturned, byesToRead)) > 0){  
			nTimesRead++;	  
			nBytesRead = nRead;  
		};  
  
		memcpy(bytesReadString, bytesReturned, byesToRead);  
		  
	}  
//then grab data from BytesReadString  
  

the full openCV way would be something like:


IplImage * src;
IplImage * dst; //you’ll have to put this into your ofxCVImage if you need to draw it in of

src = cvCreateImage(cvSize(camWidth, camHeight),IPL_DEPTH_16S,1);
dst = cvCreateImage(cvSize(camWidth, camHeight),IPL_DEPTH_16S,1);

cvSobel(src,dst,dx,dy,aperture);

I never tried doing it straight with ofxCVGrayscaleImage; when i have a min i’ll look into it

Looks like the error only happnes with the latest master branch of OF from git, in 0062 works fine with ofxCvGrayscaleImage

Cheers

  • rS

Has anyone manage to use the cvSobel method in 007 or 0071 preferably?

I need to apply the sobel filter from opencv to an image but I dont understand how to set it properly, I follow the steps as mentioned above but I cant get it to work in the latest versions of OF

Any help will be much appreciated!

i’m using opencv c++ api AND the supercool ofxCv addons.
using opencv 2.3 (i haven’t updated yet : ) but i think it is the same
the sobel filter is taken directly from opencv official tutorial.

  
  
using namespace ofxCv;  
using namespace cv;  
  
ofVideoGrabber cam;  
ofImage image;  
  
void testApp::setup() {  
    cam.setVerbose(true);  
    cam.initGrabber(320,240);  
  
    image.allocate(320,240, OF_IMAGE_COLOR);  
    cout << "Opencv version " << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << endl;  
}  
  
void testApp::update() {  
    bool bNewFrame;  
  
    cam.grabFrame();  
    bNewFrame = cam.isFrameNew();  
  
    if (bNewFrame){  
        image.setFromPixels(cam.getPixels(), 320, 240, OF_IMAGE_COLOR);  
    }  
}  
  
void testApp::draw() {  
    Mat src = toCv(image);  
    cvtColor(src, src, CV_BGR2GRAY);  
  
    int scale = 1;  
    int delta = 0;  
    int ddepth = CV_16S;  
  
    Mat grad;  
    Mat grad_x, grad_y;  
    Mat abs_grad_x, abs_grad_y;  
    
    Sobel( src, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );  
    convertScaleAbs( grad_x, abs_grad_x );  
    Sobel( src, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );  
    convertScaleAbs( grad_y, abs_grad_y );  
    addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );  
  
    image.draw(0,0);  
    drawMat(src, 320, 0);  
    drawMat(grad, 0, 240);  
}  
  

could be a solution?

EDIT: i’ve just got some trials but i can’t push back a cm::Mat into a ofImage, mayebe someone here has few minutes for doing it, because now i’m quite busy