openCv 中的边缘检测给出运行时错误

发布于 2024-10-15 07:02:39 字数 1367 浏览 3 评论 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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除会话 2024-10-22 07:02:39

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

  • 您正在循环内创建一个窗口。由于窗口每次都有相同的名称,因此您只需创建一次。
  • 在你能够展示你的图像之前,你就已经毁掉了它们。您的图像不会在您调用 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`

标准图像和输出:

lenna 输出

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 call cvWaitKey. 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 or out 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 be frame
  • 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:

lenna output

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文