#include "opencv2/highgui/highgui.hpp" Mat img = imread("C:/rect1.png"); cvtColor(img, img_gray, CV_BGR2GRAY); Mat thresh_img(img.size(), CV_8UC1); threshold(img_gray, thresh_img, 230, 255, THRESH_BINARY_INV); imshow("thresh", thresh_img); vector<vector<Point>> contours; findContours(thresh_img, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); RotatedRect mr = minAreaRect(Mat(contours[0])); Mat DrawImg(img.size(), img.type(), Scalar(255, 255, 255)); for (int i = 0; i<4; i++) line(DrawImg, vectpoint[i], vectpoint[(i + 1) % 4], Scalar(255, 0, 0), 2); imshow("drawimg", DrawImg); if (mr.size.width <= mr.size.height) Mat rotmat = getRotationMatrix2D(mr.center, angle, 1); warpAffine(img, deal_img, rotmat, img.size(), CV_INTER_CUBIC); imshow("deal_img", deal_img); getRectSubPix(deal_img, si, mr.center, rRect); imshow("截取的矩形区域", rRect);
|