【blog算法原理】Opencv中直線的表示方法
一、問題的提出:
在實際項目編寫過程中,需要對直線(Line)進行特定的處理。在以前的項目設計實現中,直線(Line)多是用來繪圖使用的,而不是用來進行分析的。
經過較為仔細地研究Opencv提供的相關內容,感覺這個問題很有搞頭,所以分離出來研究。先看refman



可以看到,opencv自己提供的3種直線尋找的函數,最后得到的表示line的數據結構和表示內容的表示都是不一樣的。出現這個原因,可能是因為編寫這幾個函數的不是一個開發者;也可能是認為地選擇了最適合特定函數使用的數據結構。那么這3個函數效果到底怎么樣?如何靈活使用到自己的項目中?最后我將提出結合自己實際編寫的函數。
二、函數測試
由於后2個函數都能夠直接以圖像(MAT)作為參數,所以用一副標准圖像進行測試;而后針對Linefit能夠以點集作為輸入的情況單獨測試(如果要使用同樣的圖像需要先細化再找邊緣,比較麻煩),並且提出自己編寫的方法。
測試圖像為

編寫代碼
#
include
"stdafx.h"
# include <iostream >
# include "opencv2/core/core.hpp"
# include "opencv2/highgui/highgui.hpp"
# include "opencv2/imgproc/imgproc.hpp"
//【blog算法原理】Opencv中直線的表示方法
// jsxyhelu 2016年1月20日
using namespace std;
using namespace cv;
void main()
{
Mat src;
Mat board; //用於將識別出來直線繪制出來
Mat board2;
vector <Vec4i > lines;
vector <Vec2f > linesf;
///////////////////////主要流程///////////////////////////////////////
src = imread( "PureLine.jpg", 0);
board = Mat : :zeros(src.size(),src.type());
board2 = Mat : :zeros(src.size(),src.type());
////HoughLineP測試
HoughLinesP(src, lines, 1, CV_PI / 180, 50, 50, 10 );
////HoughLine測試
HoughLines(src, linesf, 1, CV_PI / 180, 100, 0, 0 );
///////////////////////顯示結果///////////////////////////////////////
for( size_t i = 0; i < lines.size(); i ++ ){
Vec4i l = lines[i];
line( board, Point(l[ 0], l[ 1]), Point(l[ 2], l[ 3]), Scalar( 255), 1, CV_AA);
}
for( size_t i = 0; i < linesf.size(); i ++ ){
float rho = linesf[i][ 0], theta = lines[i][ 1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a *rho, y0 = b *rho;
pt1.x = cvRound(x0 + 1000 *( -b));
pt1.y = cvRound(y0 + 1000 *(a));
pt2.x = cvRound(x0 - 1000 *( -b));
pt2.y = cvRound(y0 - 1000 *(a));
line( board2, pt1, pt2, Scalar( 255), 1, CV_AA);
}
imshow( "src",src);
waitKey();
}
# include <iostream >
# include "opencv2/core/core.hpp"
# include "opencv2/highgui/highgui.hpp"
# include "opencv2/imgproc/imgproc.hpp"
//【blog算法原理】Opencv中直線的表示方法
// jsxyhelu 2016年1月20日
using namespace std;
using namespace cv;
void main()
{
Mat src;
Mat board; //用於將識別出來直線繪制出來
Mat board2;
vector <Vec4i > lines;
vector <Vec2f > linesf;
///////////////////////主要流程///////////////////////////////////////
src = imread( "PureLine.jpg", 0);
board = Mat : :zeros(src.size(),src.type());
board2 = Mat : :zeros(src.size(),src.type());
////HoughLineP測試
HoughLinesP(src, lines, 1, CV_PI / 180, 50, 50, 10 );
////HoughLine測試
HoughLines(src, linesf, 1, CV_PI / 180, 100, 0, 0 );
///////////////////////顯示結果///////////////////////////////////////
for( size_t i = 0; i < lines.size(); i ++ ){
Vec4i l = lines[i];
line( board, Point(l[ 0], l[ 1]), Point(l[ 2], l[ 3]), Scalar( 255), 1, CV_AA);
}
for( size_t i = 0; i < linesf.size(); i ++ ){
float rho = linesf[i][ 0], theta = lines[i][ 1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a *rho, y0 = b *rho;
pt1.x = cvRound(x0 + 1000 *( -b));
pt1.y = cvRound(y0 + 1000 *(a));
pt2.x = cvRound(x0 - 1000 *( -b));
pt2.y = cvRound(y0 - 1000 *(a));
line( board2, pt1, pt2, Scalar( 255), 1, CV_AA);
}
imshow( "src",src);
waitKey();
}
結果

(HoughLineP)

(HoughLine)
從結果上來看,HoughlineP識別的要更准確。這兩個函數一個是識別出來圖形上的點,一個是識別出來角度和圖像上的點。兩個函數的結果都是自己編寫代碼打印出來的。
三、結合實際
實際上我需要的是識別出直線,並且對圖像進行旋轉。這部分的代碼是這樣的
Point pt1;
Point pt2;
float t = ( float)(src.cols +src.rows);
pt1.x = cvRound(Line1[ 2] - Line1[ 0] *t);
pt1.y =cvRound(Line1[ 3] -Line1[ 1] *t);
pt2.x = cvRound(Line1[ 2] +Line1[ 0] *t);
pt2.y = cvRound(Line1[ 3] +Line1[ 1] *t);
line( src, pt1, pt2, Scalar( 255), 1, CV_AA);
//對結果圖像進行旋轉
Point center = Point( src.cols / 2, src.rows / 2 ); //以圖像中心為中心
double angle =atan(Line1[ 1] /Line1[ 0]);
angle = Rad2Deg(angle); //由弧度轉換為角度
/// 通過上面的旋轉細節信息求得旋轉矩陣
Mat rot_mat = getRotationMatrix2D( center, angle, 1 );
/// 旋轉已扭曲圖像
warpAffine( src, dst, rot_mat, src.size() );
float t = ( float)(src.cols +src.rows);
pt1.x = cvRound(Line1[ 2] - Line1[ 0] *t);
pt1.y =cvRound(Line1[ 3] -Line1[ 1] *t);
pt2.x = cvRound(Line1[ 2] +Line1[ 0] *t);
pt2.y = cvRound(Line1[ 3] +Line1[ 1] *t);
line( src, pt1, pt2, Scalar( 255), 1, CV_AA);
//對結果圖像進行旋轉
Point center = Point( src.cols / 2, src.rows / 2 ); //以圖像中心為中心
double angle =atan(Line1[ 1] /Line1[ 0]);
angle = Rad2Deg(angle); //由弧度轉換為角度
/// 通過上面的旋轉細節信息求得旋轉矩陣
Mat rot_mat = getRotationMatrix2D( center, angle, 1 );
/// 旋轉已扭曲圖像
warpAffine( src, dst, rot_mat, src.size() );