- 接上一篇,现在需要绕两中轴旋转,并且给定角度旋转。
- 思路:运用几何关系通过角度算出四点变换;再运用opencv四点得出3*3矩阵;最后运用透视变换函数进行变换。
下面视图为手稿:
代码如下:
cv::Mat angleTOmatbyMID(cv::Mat src,int angle1,int angle2)
{
float anx,any;
cout << "anx="<<angle1<<" any="<<angle2<<endl;
anx = 3.14*angle1/180; //x边动,y中轴不变
any = 3.14*angle2/180;
vector<cv::Point2f> corners(4),corners_trans(4);
corners[0] = cv::Point2f(0,0);
corners[1] = cv::Point2f(src.cols-1,0);
corners[2] = cv::Point2f(src.cols-1,src.rows-1);
corners[3] = cv::Point2f(0,src.rows-1);
int dis_x,dis_y,dis_xx1,dis_yy1,dis_xx2,dis_yy2;
dis_x = 0.5*src.rows*cos(any)*sin(any);
dis_y = 0.5*src.rows*cos(any)*cos(any);
dis_xx1 = 0.5*(src.cols + 2*dis_x)*cos(anx)*cos(anx);
dis_yy1 = 0.5*(src.cols + 2*dis_x)*cos(anx)*sin(anx);
dis_xx2 = 0.5*(src.cols - 2*dis_x)*cos(anx)*cos(anx);
dis_yy2 = 0.5*(src.cols - 2*dis_x)*cos(anx)*sin(anx);
corners_trans[0].x = 0.5*src.cols - dis_xx2;
corners_trans[0].y = (0.5*src.rows - dis_y) - dis_yy2;
corners_trans[1].x =0.5*src.cols + dis_xx2;
corners_trans[1].y = (0.5*src.rows - dis_y) + dis_yy2;
corners_trans[2].x = 0.5*src.cols + dis_xx1;;
corners_trans[2].y = src.rows - (dis_yy1 + (0.5*src.rows - dis_y));
corners_trans[3].x = 0.5*src.cols - dis_xx1;
corners_trans[3].y = src.rows + (dis_yy1 - (0.5*src.rows - dis_y));
//求外切矩形 左x比小、右x比大;上y比小,下y比大。
cv::Point2f change_point = cv::Point2f(0,0);
if(corners_trans[0].y == corners_trans[1].y) // anx = 0 上下平行
{
out_height = corners_trans[3].y - corners_trans[0].y;
if(corners_trans[0].x > corners_trans[3].x){
cout << "下大"<<endl;
out_width = corners_trans[2].x - corners_trans[3].x;
change_point.x = 0 - corners_trans[3].x;
change_point.y = out_height - corners_trans[3].y;
}
else{
cout << "上大"<<endl;
out_width = corners_trans[1].x - corners_trans[0].x;
change_point.x = 0 - corners_trans[0].x;
change_point.y = 0 - corners_trans[0].y;
}
corners_trans[0] += change_point;
corners_trans[1] += change_point;
corners_trans[2] +=change_point;
corners_trans[3] += change_point;
}
else if(corners_trans[0].x