Edge detection in openCv gives runtime error
I have used cvCanny function to detect Edges.
cvCanny( img_b, out, lowThresh*N开发者_开发知识库*N, highThresh*N*N, aperature_size );
But in run time it gives runtime error. The error msg not clear at all. It refers some memory location. Please help me..!!
code:
void switch_callback_h( int position ){
highInt = position;
}
void switch_callback_l( int position ){
lowInt = position;
}
int _tmain(int argc, _TCHAR* argv[])
{
const char* name = "Edge Detection Window";
// Kernel size
int N = 7;
CvCapture* capture = cvCaptureFromCAM(1);
IplImage* frame;
while(1) {
frame = cvQueryFrame( capture );
// Add convolution boarders
CvPoint offset = cvPoint((N-1)/2,(N-1)/2);
cvCopyMakeBorder(frame, img_b, offset, IPL_BORDER_REPLICATE, cvScalarAll(0));
// Make window
cvNamedWindow( name, 1 );
// Edge Detection Variables
int aperature_size = N;
double lowThresh = 20;
double highThresh = 40;
// Create trackbars
cvCreateTrackbar( "High", name, &high_switch_value, 4, switch_callback_h );
cvCreateTrackbar( "Low", name, &low_switch_value, 4, switch_callback_l );
highThresh = 800;
lowThresh = 100;
cvCanny( img_b, out, lowThresh*N*N, highThresh*N*N, aperature_size );
cvShowImage(name, out);
cvReleaseImage( &frame );
cvReleaseImage( &img_b );
cvReleaseImage( &out );
cvDestroyWindow( name );
if( cvWaitKey( 15 ) == 27 )
break;
return 0;
}
You were very close to making this work. Basically, here are your problems:
- You're creating a window inside the loop. Since the window has the same name every time, you only to create it once.
- You're destroying your images before you're able to show them. Your images will be shown not when you called
cvShowImage
, but when you callcvWaitKey
. By that time you've already freed the image, so nothing will be shown. - You're freeing frames loaded from
CvCapture
. The documentation explicitly says not to do this:
The function cvQueryFrame grabs a frame from a camera or video file, decompresses it and returns it. This function is just a combination of GrabFrame and RetrieveFrame , but in one call. The returned image should not be released or modified by the user. In the event of an error, the return value may be NULL.
- You're not initializing
image_b
orout
anywhere. They're not even declared in the code you posted. I don't know how your code even compiled, let alone ran. - You're specifying
image_b
as the source to the canny edge detection, when it should really beframe
- You're not freeing the video capture struct
Like I said, a number of tiny points. Here's code that works:
#include <stdlib.h>
#include <cv.h>
#include <highgui.h>
//
// Comment this out to use the webcam.
//
#define LOAD_IMAGE "/home/misha/Desktop/Lenna.png"
int main(int argc, char **argv)
{
const char *name = "Edge Detection Window";
int N = 7;
#ifndef LOAD_IMAGE
CvCapture *capture = cvCaptureFromCAM(1);
#endif
IplImage *frame = NULL;
IplImage *out = NULL;
cvNamedWindow(name, 1);
while (1)
{
#ifdef LOAD_IMAGE
frame = cvLoadImage(LOAD_IMAGE, 0);
#else
frame = cvQueryFrame(capture);
#endif
out = cvCreateImage(cvGetSize(frame), frame->depth, frame->nChannels);
int aperature_size = N;
double lowThresh = 20;
double highThresh = 40;
highThresh = 800;
lowThresh = 100;
cvCanny(frame, out, lowThresh * N * N, highThresh * N * N,
aperature_size);
cvShowImage(name, out);
#ifndef LOAD_IMAGE
if (cvWaitKey(15) == 27)
break;
#else
cvWaitKey(0);
break;
#endif
}
cvReleaseImage(&out);
cvDestroyWindow(name);
#ifdef LOAD_IMAGE
cvReleaseImage(&frame);
#else
cvReleaseCapture(&capture);
#endif
return 0;
}
Compiles with:
gcc -ggdb -Wall -o devan.out devan.c `pkg-config --cflags --libs opencv`
Standard image and output:
精彩评论