c - 如何从c语言程序中调用LSD(LineSegmentDetector)?

标签 c opencv computer-vision feature-detection

我正在使用 LSD 检测图像中的直线,我下载的代码包含调用 LSD 的最小示例,但它是静态的(即它仅输出主函数中的值)我想应用代码在视频中,这是输出静态结果的最小示例。

#include <stdio.h>
#include "lsd.h"

int main(void)
{
  image_double image;
  ntuple_list out;
  unsigned int x,y,i,j;
  unsigned int X = 512;  /* x image size */
  unsigned int Y = 512;  /* y image size */

  /* create a simple image: left half black, right half gray */
  image = new_image_double(X,Y);
  for(x=0;x<X;x++)
    for(y=0;y<Y;y++)
      image->data[ x + y * image->xsize ] = x<X/2 ? 0.0 : 64.0; /* image(x,y) */
   IplImage* imgInTmp = cvLoadImage("C:\Documents and Settings\Eslam farag\My Documents\Visual Studio 2008\Projects\line\hand.JPEG", 0);

  /* call LSD */

  out = lsd(image);

  /* print output */
  printf("%u line segments found:\n",out->size);
  for(i=0;i<out->size;i++)
    {
      for(j=0;j<out->dim;j++)
        printf("%f ",out->values[ i * out->dim + j ]);
      printf("\n");
    }

  /* free memory */
  free_image_double(image);
  free_ntuple_list(out);

  return 0;
}

如果有人能帮我在视频上应用代码,我会很高兴。谢谢 最好的问候,

最佳答案

由于找不到完整的示例,我将分享我编写的代码,该代码使用 OpenCV 从磁盘加载视频文件并对其执行一些图像处理。

应用程序将 文件名 作为输入(在 cmd 行上)并使用 OpenCV 内置函数 cvCvtColor() 将视频的每一帧转换为等效的灰度做这个。

我在代码上添加了一些注释以帮助您理解基本任务。

read_video.cpp:

#include <stdio.h>
#include <highgui.h>
#include <cv.h>

int main(int argc, char* argv[])
{
    cvNamedWindow("video", CV_WINDOW_AUTOSIZE);

    CvCapture *capture = cvCaptureFromAVI(argv[1]);
    if(!capture)
    {
        printf("!!! cvCaptureFromAVI failed (file not found?)\n");
        return -1;
    }

    IplImage* frame;
    char key = 0;
    while (key != 'q') // Loop for querying video frames. Pressing Q will quit
    {
        frame = cvQueryFrame( capture );
        if( !frame )
        {
            printf("!!! cvQueryFrame failed\n");
            break;
        }

        /* Let's do a grayscale conversion just 4 fun */

        // A grayscale image has only one channel, and most probably the original
        // video works with 3 channels (RGB). So, for the conversion to work, we
        // need to allocate an image with only 1 channel to store the result of 
        // this operation.
        IplImage* gray_frame = 0;
        gray_frame = cvCreateImage(cvSize(frame->width, frame->height), frame->depth, 1);
        if (!gray_frame)
        {
            printf("!!! cvCreateImage failed!\n" );
            return -1;
        }

        cvCvtColor(frame, gray_frame, CV_RGB2GRAY); // The conversion itself

        // Display processed frame on window
        cvShowImage("video", gray_frame);

        // Release allocated resources
        cvReleaseImage(&gray_frame);

        key = cvWaitKey(33);
    }

    cvReleaseCapture(&capture);
    cvDestroyWindow("video");
}

编译:

g++ read_video.cpp -o read `pkg-config --cflags --libs opencv`

如果您想知道如何遍历帧的像素以进行自定义处理,您需要查看以下答案,因为它展示了如何进行手动灰度转换。你去:OpenCV cvSet2d.....what does this do

关于c - 如何从c语言程序中调用LSD(LineSegmentDetector)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6400186/

相关文章:

c++ - 为什么互斥锁不需要互斥锁(而那个互斥锁需要互斥锁...)

image-processing - 图像交叉检测

python - OpenCV 2.4.1 - 在 Python 中计算 SURF 描述符

C - 嵌套链表

c - 判断一个数组是否为算术序列

algorithm - 在图像中查找棋盘

ios - 自适应阈值 OpenCV IOS

azure - 计算机视觉 2.0 PDF 到文本不起作用

正确关闭工作窃取线程池

c++ - 如何在编译时提取没有路径和后缀的源文件名?