部分代码实现:
Point2f center = minRect.center;//外接矩形中心点坐标
double angle = twoPointsAngel(vertices[0], vertices[1]);
Mat rot_mat = getRotationMatrix2D(center, 90-angle, 1.0);//求旋转矩阵
Mat rot_image;
Size dst_sz(im_bgr.size());
warpAffine(im_bgr, rot_image, rot_mat, dst_sz);//原图像旋转
//imshow("rot_image", rot_image);
//cv::waitKey();
int x = (center.x - (minRect.size.height / 2)) * w_scale;
int y = (center.y - (minRect.size.width / 2))* h_scale;
int h = minRect.size.width * w_scale;
int w = minRect.size.height* h_scale;
cv::Point points_0 = Point(vertices[0].x* w_scale, vertices[0].y* h_scale);
cv::Point points_1 = Point(vertices[1].x* w_scale, vertices[1].y* h_scale);
cv::Point points_2 = Point(vertices[2].x* w_scale, vertices[2].y* h_scale);
cv::Point points_3 = Point(vertices[3].x* w_scale, vertices[3].y* h_scale);
cv::line(im_bgr, points_0, points_1, color1, 2);
cv::line(im_bgr, points_1, points_2, color2, 2);
cv::line(im_bgr, points_2, points_3, color3, 2);
cv::line(im_bgr, points_3, points_0, color4, 2);
Mat result1 = rot_image(Rect(x, y, w, h));//提取ROI
//imshow("result", result1);
//waitKey();
char c[50];
_itoa(i++, c, 10);
strcat(c, ".jpg");
cv::imwrite(c, result1);
psenet身份证定位效果
cv::Point points[4];
for(int k=0;k<4;k++)
points[k] = Point(vertices[k].x* w_scale, vertices[k].y* h_scale);
cv::line(im_bgr, points[0], points[1], color1, 2);
cv::line(im_bgr, points[1], points[2], color2, 2);
cv::line(im_bgr, points[2], points[3], color3, 2);
cv::line(im_bgr, points[3], points[0], color4, 2);
红、绿、蓝、黑
width:逆时针旋转与水平线夹角的第一条边;height:另一条边
angle:width边与水平线夹角,大小范围[0,-90]
cv::Point points_0 = Point(vertices[0].x* w_scale, vertices[0].y* h_scale);
cv::Point points_1 = Point(vertices[1].x* w_scale, vertices[1].y* h_scale);
cv::Point points_2 = Point(vertices[2].x* w_scale, vertices[2].y* h_scale);
cv::Point points_3 = Point(vertices[3].x* w_scale, vertices[3].y* h_scale);
cv::line(im_bgr, points_0, points_1, color1, 2);
cv::line(im_bgr, points_1, points_2, color2, 2);
cv::line(im_bgr, points_2, points_3, color3, 2);
cv::line(im_bgr, points_3, points_0, color4, 2);
Mat result1 = rot_image(Rect(x, y, w, h));//提取ROI