我已使用cvCanny函数检测边缘。
cvCanny( img_b, out, lowThresh*N*N, highThresh*N*N, aperature_size );
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;
}
最佳答案
您非常接近,无法完成这项工作。基本上,这是您的问题:
cvShowImage
时,将不会显示您的图像,而在您调用cvWaitKey
时,将显示您的图像。到那时,您已经释放了图像,因此将不会显示任何内容。 CvCapture
加载的帧。该文档明确指出而不是可以做到这一点: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.
image_b
或out
。您发布的代码中甚至都没有声明它们。 我不知道您的代码是如何编译的,更不用说运行了。 image_b
指定为canny边缘检测的源,而实际上它应该是frame
#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;
}
gcc -ggdb -Wall -o devan.out devan.c `pkg-config --cflags --libs opencv`
https://stackoverflow.com/questions/4838122/