Opencv中直线的表示方法
# 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();
}
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() );