#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
#define window "【原始图】"
#define windowx "【结果图1】"
#define windowy "[结果图2]"
//将随机数量的用最小旋转矩形包围。
void on_thresh(int,void*);
int main()
{
Mat image(Size(600,600),CV_8UC3);
RNG rng = theRNG();
while (1)
{
int count = rng.uniform(3, 103);
vector<Point> points;
int x, y;
for (int i = 0; i < count; ++i)
{
x = rng.uniform(100, 500);
y = rng.uniform(100, 500);
points.push_back(Point(x, y));
}
RotatedRect rect = minAreaRect(points);//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx最小旋转矩形。
Point2f po[4];
rect.points(po);//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx将rect的四个定点提取出来。
Point pre = po[3];
Mat result(image.size(), image.type(),Scalar(0));
for (int i = 0; i < count; ++i)
{
circle(result, points[i], 3, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), -1);
}
for (int i = 0; i < 4; ++i)
{
line(result, pre, po[i], Scalar(rng.uniform(0, 255),rng.uniform(0,255),rng.uniform(0,255)));
pre = po[i];
}
imshow(windowx, result);
int ch = waitKey(0);
if (ch == 27) break;
}
waitKey(0);
return 0;
}
将随机数量的用最小旋转矩形包围
最新推荐文章于 2022-09-06 16:05:52 发布