1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > opencv 获取最小外接矩形

opencv 获取最小外接矩形

时间:2024-06-20 17:19:43

相关推荐

opencv 获取最小外接矩形

#include "iostream";#include "opencv.hpp"using namespace std;using namespace cv;int main(){Mat grayImage, dstImage, resultImage;Mat Image = imread("test.png");resultImage = Image.clone();imshow("原图", Image);cvtColor(Image, grayImage, COLOR_RGB2GRAY);threshold(grayImage, dstImage, 100, 255, THRESH_BINARY);//imshow("二值化", dstImage);vector<vector<Point>> contours;vector<Vec4i> hierarcy;findContours(dstImage, contours, hierarcy, RETR_EXTERNAL, CHAIN_APPROX_NONE);vector<RotatedRect> box(contours.size()); //最小外接矩形Point2f rect[4];float width = 0;//外接矩形的宽和高float height = 0;float ratio = 0; //存储长宽比=width/heigthfor (int i = 0; i < contours.size(); i++){box[i] = minAreaRect(Mat(contours[i]));box[i].points(rect);//最小外接矩形的4个端点width = box[i].size.width; height = box[i].size.height;if (height >= width){float x = 0;x = height;height = width;width = x;}ratio = width / height;cout << "宽" << width << " " << "高" << height << "长宽比"<<ratio<<endl;for (int j = 0; j < 4; j++){line(resultImage, rect[j], rect[(j + 1) % 4], Scalar(0, 0, 255), 1, 8);//绘制最小外接矩形的每条边}}namedWindow("结果图", 1);imshow("结果图", resultImage);waitKey(0);return 0;}

运行结果:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。