OpenCV利用霍夫變換進行直線檢測
本文實例為大家分享了OpenCV利用霍夫變換進行直線檢測的具體代碼,供大家參考,具體內(nèi)容如下
1.最簡單的霍夫變換是在圖像中識別直線。在平面直角坐標系(x-y)中,一條直線可以用下式表示:y=kx+b。
這表示參數(shù)平面(k-b)中的一條直線。因此,圖像中的一個點對應(yīng)參數(shù)平面中的一條直線,圖像中的一條直線對應(yīng)參數(shù)平面中的一個點。對圖像上所有的點作霍夫變換,最終所要檢測的直線對應(yīng)的一定是參數(shù)平面中直線相交最多的那個點。這樣就在圖像中檢測出了直線。在實際應(yīng)用中,直線通常采用參數(shù)方程:p=x\cos\theta+y\sin\theta。
Opencv里有以下函數(shù)檢測直線(最基本的霍夫變換):
void HoughLines(InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn=0, double stn=0 )
具體用法看代碼就知道了:(現(xiàn)在版本的OpenCV使用函數(shù)cvHoughLines2)
#include "opencv2/opencv.hpp"
#define PI 3.1415926
int main(int argc, char *argv[])
{
cv::Mat image = cv::imread ("road.jpg");
cv::Mat result;
cv::cvtColor (image,result,CV_BGRA2GRAY);
cv::Mat contours;
cv::Canny (result,contours,125,350); //邊緣檢測
std::vector<cv::Vec2f> lines;
/*霍夫變換,獲得一組極坐標參數(shù)(rho,theta),每一對對應(yīng)一條直線,保存到lines
第3,4個參數(shù)表示在(rho,theta)坐標系里橫縱坐標的最小單位,即步長*/
cv::HoughLines (contours,lines,1,PI/180,80);
std::vector<cv::Vec2f>::const_iterator iter = lines.begin ();
std::cout<<lines.size ()<<std::endl;
while(iter != lines.end())
{
float rho = (*iter)[0];
float theta = (*iter)[1];
if(theta<PI/4.||theta>3.*PI/4)
{ //畫交點在上下兩邊的直線
cv::Point pt1(rho/cos(theta),0);
cv::Point pt2((rho-result.rows*sin(theta))/cos(theta),result.rows);
cv::line(image,pt1,pt2,cv::Scalar(255),1);
}
else
{ //畫交點在左右兩邊的直線
cv::Point pt1(0,rho/sin(theta));
cv::Point pt2(result.cols,(rho-result.cols*cos(theta)/sin(theta)));
cv::line(image,pt1,pt2,cv::Scalar(255),1);
}
++iter;
}
cv::namedWindow ("hough");
cv::imshow("hough",image);
cv::waitKey (0);
}
測試結(jié)果如下:

2.可以看出,上面的直線檢測存在以下問題:
1)只能檢測出線段所在的直線,而不知道具體線段位置,也不知道線段長度;
2)同一直線可能檢測出多條直線;
3)偶然地也可能誤判段直線。
針對這些問題,opencv有那么一個函數(shù):(現(xiàn)在版本的OpenCV使用同一個函數(shù)cvHoughLines2)
void HoughLinesP(InputArray image, OutputArray lines, double rho, double theta, int threshold, dou-
ble minLineLength=0, double maxLineGap=0)
這個方法是通過概率霍夫變換實現(xiàn)的:
1)隨機獲取邊緣圖片上的前景點,映射到級坐標系畫曲線;
2)當(dāng)極坐標系里面有交點達到最小投票數(shù),將該點對應(yīng)x-y坐標系的直線L找出來;
3)搜索邊緣圖片上前景點,在直線L上的點(且點與點之間距離小于maxLineGap的)連成線段,然后這些點全部刪除,并且記錄該線段的參數(shù),就是起始點和終止點。(當(dāng)然這里線段長度要滿足最小長度的,否則就不用記錄了)
4)重復(fù)1),2),3)
其使用方法見代碼:
#include "opencv2/opencv.hpp"
#define PI 3.1415926
class LineFinder
{
private:
std::vector<cv::Vec4i> lines; // 直線對應(yīng)的點參數(shù)向量
double deltaRho; //步長
double deltaTheta;
int minVote; // 判斷是直線的最小投票數(shù)
double minLength; // 判斷是直線的最小長度
double maxGap; // 同一條直線上點之間的距離容忍度
public:
LineFinder() : deltaRho(1), deltaTheta(PI/180),
minVote(10), minLength(0.), maxGap(0.) {} //初始化
void setAccResolution(double dRho, double dTheta) // 設(shè)置步長
{
deltaRho= dRho;
deltaTheta= dTheta;
}
void setMinVote(int minv) // 設(shè)置最小投票數(shù)
{
minVote= minv;
}
void setLineLengthAndGap(double length, double gap) // 設(shè)置最小線段長度和線段間距容忍度
{
minLength= length;
maxGap= gap;
}
std::vector<cv::Vec4i> findLines(cv::Mat& binary) //尋找線段
{
lines.clear();
cv::HoughLinesP(binary,lines, deltaRho, deltaTheta, minVote,minLength, maxGap);
return lines;
}
void drawDetectedLines(cv::Mat &image, cv::Scalar color=cv::Scalar(255,255,255)) // 畫線段
{
std::vector<cv::Vec4i>::const_iterator it2=lines.begin();
while (it2!=lines.end())
{
cv::Point pt1((*it2)[0],(*it2)[1]);
cv::Point pt2((*it2)[2],(*it2)[3]);
cv::line( image, pt1, pt2, color);
++it2;
}
}
};
int main(int argc, char *argv[])
{
cv::Mat image = cv::imread ("road.jpg");
cv::Mat result;
cv::cvtColor (image,result,CV_BGRA2GRAY);
cv::Mat contours;
cv::Canny (result,contours,125,350); //邊緣檢測
LineFinder finder;
finder.setMinVote (80);
finder.setLineLengthAndGap (100,20);
finder.findLines (contours);
finder.drawDetectedLines (image);
std::vector<cv::Vec2f> lines;
cv::HoughLines (contours,lines,1,PI/180,80);
std::vector<cv::Vec2f>::const_iterator iter = lines.begin ();
std::cout<<lines.size ()<<std::endl;
while(iter != lines.end())
{
float rho = (*iter)[0];
float theta = (*iter)[1];
if(theta<PI/4.||theta>3.*PI/4)
{ //畫交點在上下兩邊的直線
cv::Point pt1(rho/cos(theta),0);
cv::Point pt2((rho-result.rows*sin(theta))/cos(theta),result.rows);
cv::line(image,pt1,pt2,cv::Scalar(255),1);
}
else
{ //畫交點在左右兩邊的直線
cv::Point pt1(0,rho/sin(theta));
cv::Point pt2(result.cols,(rho-result.cols*cos(theta)/sin(theta)));
cv::line(image,pt1,pt2,cv::Scalar(255),1);
}
++iter;
}
cv::namedWindow ("hough");
cv::imshow("hough",image);
cv::waitKey (0);
}
測試結(jié)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
數(shù)據(jù)結(jié)構(gòu) 雙機調(diào)度問題的實例詳解
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu) 雙機調(diào)度問題的實例詳解的相關(guān)資料,雙機調(diào)度問題,又稱獨立任務(wù)最優(yōu)調(diào)度:用兩臺處理機A和B處理n個作業(yè)的實例,需要的朋友可以參考下2017-08-08
C/C++中CJSON的使用(創(chuàng)建與解析JSON數(shù)據(jù))
cJSON是一個超輕巧的JSON解析器,本文主要介紹了C/C++中CJSON的使用(創(chuàng)建與解析JSON數(shù)據(jù)),具有一定的參考價值,感興趣的可以了解一下2021-09-09
C語言中g(shù)etchar和putchar的使用方法詳解
我們知道scanf函數(shù)可以從鍵盤輸入信息,而printf則可以輸出信息,同樣地,getchar和putchar也有同樣的功能,下面我來給大家介紹putchar和getchar的使用方法,需要的朋友可以參考下2023-08-08
用C語言判斷一個二叉樹是否為另一個的子結(jié)構(gòu)
這篇文章主要介紹了用C語言判斷一個二叉樹是否為另一個的子結(jié)構(gòu),是數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)當(dāng)中的基礎(chǔ)知識,需要的朋友可以參考下2015-08-08

