首先用函数HoughLinesP()检测到直线;而HoughLines()的检测效果不好,很多时候都检测不到直线,所以不选用。
-
CV_EXPORTS_W void HoughLinesP( InputArray image, OutputArray lines,
-
double rho, double theta, int threshold,
-
double minLineLength= 0, double maxLineGap= 0 );
-
float CalculateLineAngle( Point p1, Point p2 )
-
{
-
float xDis,yDis;
-
xDis = p2.x - p1.x;
-
yDis = p2.y - p1.y;
-
-
float angle = atan2(yDis, xDis);
-
angle = angle / PI * 180;
-
return angle;
-
}
采用atan2()函数,详细请见http://zh.wikipedia.org/wiki/Atan2
最后根据角度,用仿射变换对图像进行旋转:
-
Point center = Point(srcImg.cols/ 2, srcImg.rows/ 2);
-
Mat rotateM = getRotationMatrix2D(center, angle, 1.0);
-
warpAffine(srcImg, dstImg, rotateM, srcImg.size());
如需转载,请注明文章出处:http://blog.csdn.net/wsbeibei