0

我使用 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;
}
4

1 回答 1

2

非常接近完成这项工作。基本上,这是您的问题:

  • 您正在循环内创建一个窗口。由于窗口每次都具有相同的名称,因此您只需创建一次。
  • 在你能够展示它们之前,你正在破坏你的图像。您的图像不会在您拨打电话时显示cvShowImage,而是在您拨打电话时显示cvWaitKey。到那时你已经释放了图像,所以什么都不会显示。
  • 您正在释放从CvCapture. 文档明确表示要这样做:

函数 cvQueryFrame 从相机或视频文件中抓取一帧,解压缩并返回。这个函数只是 GrabFrame 和 RetrieveFrame 的组合,但在一次调用中。返回的图像不应由用户发布或修改。如果发生错误,返回值可能为 NULL。

  • 你没有初始化image_bout任何地方。它们甚至没有在您发布的代码中声明。 我什至不知道您的代码是如何编译的,更不用说运行了
  • 您正在指定image_b作为精明边缘检测的来源,实际上应该是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`

标准图像和输出:

莲娜 输出

于 2011-01-31T14:21:36.553 回答