Hough变换

先上代码,c++

1.hough检测线

//LineFinder.h
#include"opencv2/imgproc/imgproc.hpp"
#include"opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include
//#include 
//#include 
using namespace std;
using namespace cv;
#define PI 3.141592
class LineFinder{
private:
    Mat img;
    vectorlines;
    double deltaRho;
    double deltaTheta;
    int minVote;
    double minLength;
    double maxGap;
public:
    LineFinder():deltaRho(1),deltaTheta(PI/180),minVote(10),minLength(0.),maxGap(0.){}
    void setAccResolution(double dRho,double dTheta)
    {
        deltaRho=dRho;
        deltaTheta=dTheta;
    }
    void setMinVote (int minv)
    {
        minVote=minv;
    }
    void setLineLengAndGap(double length,double gap)
    {
        minLength=length;
        maxGap=gap;
    }
    vector findLines(Mat &binary)
    {
        lines.clear();
        HoughLinesP(binary,lines,deltaRho,deltaTheta,minVote,minLength,maxGap);
        return lines;
    }
 
    void drawDetectedLines(Mat &image,Scalar color=Scalar(255,255,255))
    {
        vector::const_iterator it2=lines.begin();
        while (it2!=lines.end())
        {
            Point pt1((*it2)[0],(*it2)[1]);
            Point pt2((*it2)[2],(*it2)[3]);
            line(image,pt1,pt2,color);
            ++it2;
        }
 
    }
 
};

//main
#include "LineFinder.h"
void main()
{
    Mat img=imread("C:\\Users\\Administrator\\Desktop\\工作\\testp\\road.jpg",0);
   Mat out1;
    Canny(img,out1,125,350);
    Mat img1;
    img1=img.clone();
    vector lines;
    HoughLines(out1,lines,1,PI/180,60);
    vector::const_iterator it=lines.begin();
    while (it!=lines.end())
    {
        float rho=(*it)[0];
        float theta=(*it)[1];
        if (theta3.*PI/4.)
        {
            Point pt1(rho/cos(theta),0);
            Point pt2((rho-out1.rows*sin(theta))/cos(theta),out1.rows);
            line(img1,pt1,pt2,Scalar(255),1);
        }
        else
        {
            Point pt1(0,rho/sin(theta));
            Point pt2(out1.cols,(rho-out1.cols*cos(theta))/sin(theta));
            line(img1,pt1,pt2,Scalar(255),1);
        }
        ++it;
    }
 
    LineFinder finder;
    finder.setLineLengAndGap(100,20);
    finder.setMinVote(60);
    finder.findLines(out1);
    finder.drawDetectedLines(img);
 
 
 
 
    imshow("houghlinep",img);
    imshow("original",img1);
    imshow("out",out1);
    waitKey(0);
 
 
 
} 

2.Hough检测⚪

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
void main()
{
    Mat img=imread("C:\\Users\\Administrator\\Desktop\\工作\\testp\\chariot.jpg",0);
    Mat img1;
    GaussianBlur(img,img1,Size(5,5),1.5);
vectorcircles;
HoughCircles(img1,circles,CV_HOUGH_GRADIENT,2,50,200,100,25,100);
vector::const_iterator itc=circles.begin();
while(itc!=circles.end())
{
    circle(img1,Point((*itc)[0],(*itc)[1]),(*itc)[2],Scalar(255),5);
    ++itc;
}
 
 
 
    imshow("original",img);
    imshow("img1",img1);
    waitKey(0);
 
 
 
 
}

3.点集的直线拟合

#include "LineFinder.h"
void main()
{
    Mat img=imread("C:\\Users\\Administrator\\Desktop\\工作\\testp\\road.jpg",0);
   Mat out1;
    Canny(img,out1,125,350);
    Mat img1;
    img1=img.clone();
    vector lines;
    HoughLines(out1,lines,1,PI/180,60);
    vector::const_iterator it=lines.begin();
    while (it!=lines.end())
    {
        float rho=(*it)[0];
        float theta=(*it)[1];
        if (theta3.*PI/4.)
        {
            Point pt1(rho/cos(theta),0);
            Point pt2((rho-out1.rows*sin(theta))/cos(theta),out1.rows);
            line(img1,pt1,pt2,Scalar(255),1);
        }
        else
        {
            Point pt1(0,rho/sin(theta));
            Point pt2(out1.cols,(rho-out1.cols*cos(theta))/sin(theta));
            line(img1,pt1,pt2,Scalar(255),1);
        }
        ++it;
    }
 
    LineFinder finder;
    finder.setLineLengAndGap(100,20);
    finder.setMinVote(60);
    finder.findLines(out1);
    finder.drawDetectedLines(img);
 
 
    vectorlines111=finder.findLines(out1);
    int n=0;
    Mat oneline(out1.size(),CV_8U,Scalar(0));
    line(oneline,Point(lines111[n][0],lines111[n][1]),Point(lines111[n][2],lines111[n][3]),Scalar(255),3);
    bitwise_and(out1,oneline,oneline);
    //threshold(oneline,oneline,100,255,THRESH_BINARY_INV);
    vectorpoints;
    for (int y=0;y(y);
        for (int x=0;x

你可能感兴趣的:(Hough变换)