LSD直线检测

LSD直线检测

本文由 @lonelyrains出品,转载请注明出处。
文章链接: http://blog.csdn.net/lonelyrains/article/details/49493053

  • 原理
    参见链接

  • 使用环境
    OpenCV3.0
    VS2013
    Windows-7-64

  • 调用代码

#include <iostream>
#include <fstream>
#include <omp.h>

#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <opencv2\highgui.hpp>
#include <opencv2\imgproc.hpp>
#include <opencv2\imgcodecs.hpp>

using namespace cv;
using namespace std;

void openCapture(VideoCapture &cap)
{
    cap.open(""E:\\music\\MTV\\Alizee.-.[L'Alize].Live.(Bravo.Supershow.2000).avi"");//hit
}

float dist2Point(int x1, int y1, int x2, int y2)
{
    return std::sqrt(double(x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
}

//#pragma omp parallel for
int main()
{
    int i;
    //string str="C:\\image1.png";
    //std::string str="C:\\test.png";

    VideoCapture cap;
    cv::Mat img;
    int curFrame;


    for (;;)
    {
        openCapture(cap);
        if (!cap.isOpened())  // if not success, exit program
        {
            cout << "Cannot open the video cam" << endl;
            return -1;
        }
        curFrame = 0;

        for (; curFrame < cap.get(CV_CAP_PROP_FRAME_COUNT) - 10;)
        {
            curFrame = cap.get(CV_CAP_PROP_POS_FRAMES);
            cap.read(img);
            //std::vector<std::vector<double> > lines;
            vector<Vec4f> lines;

            Mat img_gray;

            int startTime = getTickCount();
            cvtColor(img, img_gray, COLOR_BGR2GRAY);

            // GenerateWhiteNoise
            /** @brief Creates a smart pointer to a LineSegmentDetector object and initializes it.

            The LineSegmentDetector algorithm is defined using the standard values. Only advanced users may want
            to edit those, as to tailor it for their own application.

            @param _refine The way found lines will be refined, see cv::LineSegmentDetectorModes
            @param _scale The scale of the image that will be used to find the lines. Range (0..1].
            @param _sigma_scale Sigma for Gaussian filter. It is computed as sigma = _sigma_scale/_scale.
            @param _quant Bound to the quantization error on the gradient norm.
            @param _ang_th Gradient angle tolerance in degrees.
            @param _log_eps Detection threshold: -log10(NFA) \> log_eps. Used only when advancent refinement
            is chosen.
            @param _density_th Minimal density of aligned region points in the enclosing rectangle.
            @param _n_bins Number of bins in pseudo-ordering of gradient modulus.
            */
            //Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_NONE,0.8,0.6,0.5,10,0,0.9,1024);
            Ptr<LineSegmentDetector> detector = createLineSegmentDetector(LSD_REFINE_NONE);// , 0.5, 10, 0, 0.9, 1024);
            detector->detect(img_gray, lines);
            int endTime = getTickCount();
            double ticksOneSec = getTickFrequency();

            printf("width * height = %d X %d , (endTime - startTime) / ticksOneSec = %f \n",
                img_gray.cols, img_gray.rows, (endTime - startTime) / ticksOneSec);

            //draw
            for (i = 0; i < lines.size(); ++i)
            {
                if (dist2Point(lines[i][0], lines[i][1], lines[i][2], lines[i][3]) > 50)
                    cv::line(img, cv::Point(lines[i][0], lines[i][1]), cv::Point(lines[i][2], lines[i][3]), cv::Scalar(0, 0, 255), 1, CV_AA);
            }
            cout << "linesSize = " << lines.size() << endl;
            imshow("lsd", img);
            waitKey(1);
            //imwrite("C:\\test.bmp", imgShow);
        }
    }
    return 0;
}
  • 使用效果截图
    LSD直线检测_第1张图片

  • 注意问题
    1、ACCESS_MASK冲突
    opencv300\sources\modules\core\include\opencv2\core\mat.hpp中定义代码:

enum { ACCESS_READ=1<<24, ACCESS_WRITE=1<<25, ACCESS_RW=3<<24, ACCESS_MASK=ACCESS_RW, ACCESS_FAST=1<<26 };

C:\Program Files (x86)\Windows Kits\8.1\Include\um\oledb.h和c:\Program Files (x86)\Windows Kits\8.1\Include\um\winnt.h均有定义代码:

typedef DWORD ACCESS_MASK;

从而导致冲突。冲突有两种解决方式。第一种是不用using namespace cv,第二种是重编opencv3.0的代码,改变ACCESS_MASK的命名,例如CV_ACCESS_MASK。

  • 2、如果要使用视频,除了需要包含opencv的库之外,还需要包含Vfw32.lib。否则编译无误,但是链接时,提示ICOpen、AVIFileInit等AVIFile前缀的函数找不到。

你可能感兴趣的:(源代码,编译问题,LSD直线检测)