opencv實現圖像傾斜校正
本文實例為大傢分享瞭opencv實現圖像傾斜校正的具體代碼,供大傢參考,具體內容如下
今天的任務是如圖這兩種情況,我現在的情況是和如圖一樣的,左圖左邊傾斜一點兒,那麼我需要把左邊壓低傾斜校正。右圖,右邊傾斜我需要把右邊下壓到水平位置傾斜校正。我的場景不會有大角度的傾斜,就這麼點可能的不會超過45°的小角度。
標準的傳統圖像處理方法。首先就是灰度,二值化,取輪廓,篩選得到目標(面積最大的那個),得到傾斜角度,轉正。
具體過程就不詳細說瞭,都在代碼裡面一看就懂。效果圖如下:
#include <iostream> #include"opencv2/opencv.hpp" using namespace std; using namespace cv; bool cmp_x(cv::Point pt1, cv::Point pt2) { return (pt1.x < pt2.x); } cv::Point2f get_mid_pt(cv::Point2f pt1, cv::Point2f pt2) { return cv::Point2f((pt1.x + pt2.x)/2.0,(pt1.y + pt2.y) / 2.0); } double get_point_angle(cv::Point2f pointO,cv::Point2f pointA) { double angle = 0; cv::Point2f point; double temp; point = cv::Point2f((pointA.x - pointO.x), (pointA.y - pointO.y)); if ((0==point.x) && (0==point.y)) { return 0; } if (0==point.x) { angle = 90; return angle; } if (0==point.y) { angle = 0; return angle; } temp = fabsf(float(point.y)/float(point.x)); temp = atan(temp); temp = temp*180/CV_PI ; if ((0<point.x)&&(0<point.y)) { angle = 360 - temp; return angle; } if ((0>point.x)&&(0<point.y)) { angle = 360 - (180 - temp); return angle; } if ((0<point.x)&&(0>point.y)) { angle = temp; return angle; } if ((0>point.x)&&(0>point.y)) { angle = 180 - temp; return angle; } printf("sceneDrawing :: getAngle error!"); return -1; } int RotateImage(const cv::Mat &src, double angle, cv::Mat &dst, cv::Mat &rot_matrix, bool crop = true, int flags = cv::INTER_NEAREST, int borderMode = cv::BORDER_CONSTANT, const cv::Scalar &borderValue = cv::Scalar()) { if(0 == src.cols * src.rows) { return 0;} cv::Point2f center(src.cols / 2.0f, src.rows / 2.0f); rot_matrix = cv::getRotationMatrix2D(center, angle, 1.0); if (crop) { if (dst.data == NULL) { dst = cv::Mat(src.rows, src.cols, src.type()); } } else { cv::Rect bbox = cv::RotatedRect(center, cv::Size2f(src.cols, src.rows), angle).boundingRect(); double *p = (double *) rot_matrix.data; p[2] += bbox.width / 2.0 - center.x; p[5] += bbox.height / 2.0 - center.y; if (dst.rows != bbox.height || dst.cols != bbox.width) { dst = cv::Mat(bbox.height, bbox.width, src.type()); } } cv::warpAffine(src, dst, rot_matrix, dst.size(), flags, borderMode, borderValue); return 0; } int main(int argc, char *argv[]) { cv::Mat img = cv::imread("/data_1/everyday/0325/13.jpeg"); cv::Mat m_gray,m_bi; cv::cvtColor(img,m_gray,CV_BGR2GRAY); cv::threshold(m_gray,m_bi,100,255,THRESH_BINARY_INV); vector<vector<Point>> contours; vector<Vec4i> hierarchy; findContours(m_bi,contours,hierarchy,RETR_TREE,CHAIN_APPROX_SIMPLE,Point()); RotatedRect rt_rot_max,rt_tmp; int max_szie = -1; for(int i=0;i<contours.size();i++) { rt_tmp = minAreaRect(Mat(contours[i])); if(rt_tmp.size.area() > max_szie) { max_szie = rt_tmp.size.area(); rt_rot_max = rt_tmp; } } std::vector<cv::Point2f> v_pt(4); rt_rot_max.points(v_pt.data()); std::sort(v_pt.begin(),v_pt.end(),cmp_x); cv::Point2f pt_left = get_mid_pt(v_pt[0], v_pt[1]); cv::Point2f pt_right = get_mid_pt(v_pt[2], v_pt[3]); double ang = get_point_angle(pt_left,pt_right); std::cout<<"ang="<<ang<<std::endl; cv::circle(img,v_pt[0],6,Scalar(50,12,189),3); cv::circle(img,v_pt[1],6,Scalar(10,255,255),3); cv::circle(img,v_pt[2],6,Scalar(150,120,19),3); cv::circle(img,v_pt[3],6,Scalar(0,0,0),3); cv::Mat m_rot,rot_matrix; RotateImage(img, -ang, m_rot, rot_matrix, false); cv::imshow("m_rot",m_rot); cv::imshow("m_bi",m_bi); cv::imshow("m_gray",m_gray); cv::imshow("img",img); cv::waitKey(0); return 0; }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。