opencv3寻找最小包围矩形在图像中的应用-滚动条

时间:2022-09-21 17:39:51
 #include<opencv2/opencv.hpp>
#include<iostream>
#include<vector> using namespace cv;
using namespace std; int g_nMinThred = , g_nMaxThred = ;
int g_nThick = ; int main()
{
Mat srcImage = imread("group.jpg"); namedWindow("【原图】", );
imshow("【原图】", srcImage); createTrackbar("minthred", "【原图】", &g_nMinThred, , );
createTrackbar("maxthred", "【原图】", &g_nMaxThred, , );
createTrackbar("thick", "【原图】", &g_nThick, , ); //先对图像进行空间的转换(为了之后要提取二值图像)
Mat grayImage;
cvtColor(srcImage, grayImage, CV_BGR2GRAY);
//对图像进行滤波,达到较好的效果
GaussianBlur(grayImage, grayImage, Size(, ), , );
imshow("【滤波后的图像】", grayImage); char key;
while ()
{
//用边缘检测的方式获取二值图像
Mat cannyImage;
Canny(grayImage, cannyImage, g_nMinThred, g_nMaxThred, ); //在二值图像中提取轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(cannyImage, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(, )); //对每个轮廓的点集 找逼近多边形
vector<vector<Point>> approxPoint(contours.size());
for (int i = ; i < (int)contours.size(); i++)
{
//rectPoint变量中得到了矩形的四个顶点坐标
RotatedRect rectPoint = minAreaRect(contours[i]);
//定义一个存储以上四个点的坐标的变量
Point2f fourPoint2f[];
//将rectPoint变量中存储的坐标值放到 fourPoint的数组中
rectPoint.points(fourPoint2f); //根据得到的四个点的坐标 绘制矩形
for (int i = ; i < ; i++)
{
line(cannyImage, fourPoint2f[i], fourPoint2f[i + ]
, Scalar(, , ), g_nThick);
}
line(cannyImage, fourPoint2f[], fourPoint2f[]
, Scalar(, , ), g_nThick);
} imshow("【绘制后的图像】", cannyImage); key = waitKey();
if (key == )
break;
} return ;
}
opencv3寻找最小包围矩形在图像中的应用-滚动条
 
0