opencv关于直线检测,基于opencv

98 篇文章 5 订阅
48 篇文章 4 订阅

linefinder.h文件

#ifndef LINEFINDER_H
#define LINEFINDER_H
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include<cmath>
class LineFinder
{
private:
    cv::Mat img; // original image
    std::vector<cv::Vec4i> lines;
    double deltaRho;
    double deltaTheta;
    int minVote;

    double minLength; // min length for a line
    double maxGap; // max allowed gap along the line
public:
    // Default accumulator resolution is 1 pixel by 1 degree
    // no gap, no mimimum length
    LineFinder() : deltaRho(1),
        deltaTheta(PI/180),
        minVote(10),
        minLength(0.),
        maxGap(0.) {}
    // Set the resolution of the accumulator
    void setAccResolution(double dRho, double dTheta)
    {
        deltaRho= dRho;
        deltaTheta= dTheta;
    }
    // Set the minimum number of votes
    void setMinVote(int minv)
    {
        minVote= minv;
    }
    // Set line length and gap
    void setLineLengthAndGap(double length, double gap)
    {
        minLength= length;
        maxGap= gap;
    }
    // Apply probabilistic Hough Transform
    std::vector<cv::Vec4i> findLines(cv::Mat& binary)
    {
        lines.clear();
        cv::HoughLinesP(binary, lines, deltaRho, deltaTheta, minVote, minLength, maxGap);
        return lines;
    }
    // Draw the detected lines on an image
    void drawDetectedLines(cv::Mat &image, cv::Scalar color = cv::Scalar(255, 255, 255))
    {
        // Draw the lines
        int x = 0;
        int y = 0;
        std::vector<cv::Vec4i>::const_iterator it2 = lines.begin();
        while (it2 != lines.end())
        {
           x = (*it2)[0] - (*it2)[2];
           y = (*it2)[1] - (*it2)[3];
           if(abs(x) > 50 || abs(y) > 50)//这里过滤长于多少的线段
           {
               cv::Point pt1((*it2)[0],(*it2)[1]);
               cv::Point pt2((*it2)[2],(*it2)[3]);
               cv::line( image, pt1, pt2, color, 2);
               ++it2;
           }
//           cv::Point pt1((*it2)[0],(*it2)[1]);
//           cv::Point pt2((*it2)[2],(*it2)[3]);
//           cv::line( image, pt1, pt2, color, 2);
//           ++it2;
            //

        }
    }
};

#endif // LINEFINDER_H

main.cpp文件

#include <math.h>
#define PI 3.14159265358979
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <cv.h>
#include <highgui.h>
#include <string>
#include "stdlib.h"
#include "linefinder.h"
#include <QDebug>
using namespace std;

int main(int argc, char *argv[])
{
    cv::Mat image = cv::imread("C:\\Users\\Administrator\\Desktop\\avi\\testline.jpg");
    cv::Mat contours;
    cv::cvtColor(image, contours, cv::COLOR_BGR2GRAY);
    cv::bitwise_not(contours, contours);
    //cv::Canny(image, contours, 155, 350);
    LineFinder finder;
    // Set probabilistic Hough parameters
    finder.setLineLengthAndGap(100, 20);
    finder.setMinVote(80);
    // Detect lines and draw them
    std::vector<cv::Vec4i> lines = finder.findLines(contours);
    finder.drawDetectedLines(image, cv::Scalar(0, 0, 255));
    cv::namedWindow("Detected Lines with HoughP");
    cv::imshow("Detected Lines with HoughP",image);
    imwrite("C:\\Users\\Administrator\\Desktop\\avi\\image1.jpg", image);//保存保存一帧图片
    getchar();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vqt5_qt6

你的鼓励是我们创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值