文章目录
轮廓发现是基于图像边缘提取的基础寻找对象轮廓的方法。 所以边缘提取的阈值选定会影响最终轮廓发现结果
轮廓查找步骤:
- 输入图像转为灰度图像cvtColor
- 使用Canny进行边缘提取或者threshold阈值操作,得到二值图像
- 使用findContours寻找轮廓
- 使用drawContours绘制轮廓
findContours发现轮廓
在二值图像上发现轮廓使用
cv::findContours(InputOutputArray binImg, 输入图像,非0的像素被看成1,0的像素值保持不变,8-bitOutputArrayOfArrays contours, 全部发现的轮廓对象OutputArray, hierachy 图该的拓扑结构 std::vector<cv::Vec4i>,可选,该轮廓发现算法正是基于图像拓扑结构实现。它的元素与轮廓的数量一样多。对于每个第 i 个轮廓轮廓[i],元素hierarchy[i][0]、hierarchy[i][1]int mode, 轮廓返回的模式int method, 发现方法Point offset=Point() 轮廓像素的位移,默认(0, 0)没有位移)
drawContours绘制轮廓
在二值图像上发现轮廓cv::findContours之后对发现的轮廓数据进行绘制显示
drawContours(InputOutputArray binImg, 输出图像OutputArrayOfArrays contours, 全部发现的轮廓对象Int contourIdx 轮廓索引号const Scalar & color, 绘制颜色int thickness,/ 绘制线宽int lineType , 线的类型LINE_8InputArray hierarchy, 拓扑结构图int maxlevel, 最大层数, 0只绘制当前的,1表示绘制绘制当前及其内嵌的轮廓Point offset=Point() 轮廓位移,可选
代码
//轮廓发现:通过cv::fingContoursAPI查找轮廓,通过cv::drawContours绘制轮廓#include #include #include using namespace cv;using namespace std;int threshold_value = 100;int threshold_max = 255;RNG rng;const char* output_win = "Demo_Contour";void Demo_Contours(int, void*);Mat src,dst;int main(int argc, char** argv) {src = imread("D:/photos/45.png");if (src.empty()) {printf("could not load image...\n");return -1;}namedWindow("input image", CV_WINDOW_AUTOSIZE);namedWindow(output_win, CV_WINDOW_AUTOSIZE);imshow("input image", src);cvtColor(src, src, CV_BGR2GRAY);//灰度化图像,为Canny边缘检测做准备const char* trackbar_title = "threshold_value";createTrackbar(trackbar_title, output_win, &threshold_value, threshold_max, Demo_Contours);//动态调整Canny边缘检测的阈值Demo_Contours(0, 0);//使程序刚开始就有结果,与createTrackbar无关waitKey(0);return 0;}void Demo_Contours(int, void*) {Mat canny_output;vector<vector<Point>> contours;vector<Vec4i> hierachy;Canny(src, canny_output, threshold_value, threshold_value * 2, 3, false);//Canny边缘检测,3代表算子尺寸imshow("canny image", canny_output);findContours(canny_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));//contours储存轮廓的点集,轮廓提取方式为RETR_TREE,轮廓表达为:CHAIN_APPROX_SIMPLEdst = Mat::zeros(src.size(), CV_8UC3);RNG rng(12345);for (size_t i = 0; i < contours.size(); i++) {//逐条绘制轮廓Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));drawContours(dst, contours, i, color, 2, 8, hierachy, 0, Point(0, 0));}imshow(output_win, dst);}
凸包概念
什么是凸包(Convex Hull),在一个多变形边缘或者内部任意两个点的连线都包含在多边形边界或者内部。
**正式定义:**包含点集合S中所有点的最小凸多边形称为凸包
凸包扫描算法介绍——Graham扫描算法
- 首先选择Y方向最低的点作为起始点p0。
- 从p0开始极坐标扫描,依次添加p1….pn(排序顺序是根据极坐标的角度大小,逆时针方向)。
- 对每个点pi来说,如果添加pi点到凸包中导致一个左转向(逆时针方法)则添加该点到凸包,
反之如果导致一个右转向(顺时针方向)删除该点从凸包中。
convexHull(InputArray points,// 输入候选点,来自findContoursOutputArray hull,// 凸包bool clockwise,// default true, 顺时针方向bool returnPoints)// true 表示返回点个数,如果第二个参数是vector则自动忽略 }
凸包逼近实现步骤:
-
首先把图像从RGB转为灰度。
-
然后再转为二值图像。
-
在通过发现轮廓得到候选点。
-
凸包API调用。
-
绘制显示。
#include #include #include using namespace cv;using namespace std;int threshold_value = 100;int threshold_max = 255;RNG rng(12345);const char* output_win = "Demo_convex hull";void threshold_callback(int, void*);Mat src, dst,dst2,gray_src;int main(int argc, char** argv) {src = imread("D:/photos/45.png");if (src.empty()) {printf("could not load image...\n");return -1;}namedWindow("input image", CV_WINDOW_AUTOSIZE);namedWindow(output_win, CV_WINDOW_AUTOSIZE);const char* trackbar_label = "threshold:";imshow("input image", src);cvtColor(src, gray_src, CV_BGR2GRAY);blur(gray_src, gray_src, Size(3, 3), Point(-1, -1), BORDER_DEFAULT);//均值模糊进行降噪处理imshow("src_gray", gray_src);createTrackbar(trackbar_label, output_win, &threshold_value, threshold_max, threshold_callback);threshold_callback(0, 0);waitKey(0);return 0;}void threshold_callback(int, void*) {Mat bin_output;vector<vector<Point>> contours;vector<Vec4i> hierachy;threshold(gray_src, bin_output, threshold_value, threshold_max, THRESH_BINARY);findContours(bin_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));vector<vector<Point>> convexs(contours.size());dst = Mat::zeros(src.size(), CV_8UC3);dst2 = Mat::zeros(src.size(), CV_8UC3);for (size_t i = 0; i < contours.size(); i++) {Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));convexHull(contours[i], convexs[i], false, true);//drawContours(dst, contours, i, color, 2, 8, hierachy, 0, Point(0, 0));}vector<Vec4i> empty(0);for (size_t k = 0; k < contours.size(); k++) {Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));drawContours(dst2, contours, k, color, 2, LINE_8, hierachy,1, Point(0, 0));drawContours(dst, convexs, k, color, 2, LINE_8, empty, 0, Point(0, 0));//注意此时hieracgy选项填Mat()}imshow(output_win, dst);imshow("contours_Demo", dst2);return;}
相关理论介绍
轮廓周围绘制矩形 -API
approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed)
基于RDP算法实现,目的是减少多边形轮廓点数。
cv::minEnclosingCircle(InputArray points, //得到最小区域圆形
Point2f& center, // 圆心位置
float& radius)// 圆的半径
cv::fitEllipse(InputArray points)得到最小椭圆
绘制步骤
首先将图像变为二值图像。
发现轮廓,找到图像轮廓。
通过相关API在轮廓点上找到最小包含矩形和圆,旋转矩形与椭圆。
绘制它们。
程序实例
#include #include #include using namespace std;using namespace cv;Mat src, gray_src, drawImg;int threshold_v = 170;int threshold_max = 255;const char* output_win = "rectangle-demo";RNG rng(12345);void Contours_Callback(int, void*);int main(int argc, char** argv) {src = imread("D:/photos/45.png");if (!src.data) {printf("could not load image...\n");return -1;}cvtColor(src, gray_src, CV_BGR2GRAY);blur(gray_src, gray_src, Size(3, 3), Point(-1, -1));const char* source_win = "input image";namedWindow(source_win, CV_WINDOW_AUTOSIZE);namedWindow(output_win, CV_WINDOW_AUTOSIZE);imshow(source_win, src);createTrackbar("Threshold Value:", output_win, &threshold_v, threshold_max, Contours_Callback);Contours_Callback(0, 0);waitKey(0);return 0;}void Contours_Callback(int, void*) {Mat binary_output;vector<vector<Point>> contours;vector<Vec4i> hierachy;threshold(gray_src, binary_output, threshold_v, threshold_max, THRESH_BINARY);//imshow("binary image", binary_output);findContours(binary_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(-1, -1));vector<vector<Point>> contours_ploy(contours.size());vector<Rect> ploy_rects(contours.size());vector<Point2f> ccs(contours.size());vector<float> radius(contours.size());vector<RotatedRect> minRects(contours.size());vector<RotatedRect> myellipse(contours.size());for (size_t i = 0; i < contours.size(); i++) {approxPolyDP(Mat(contours[i]), contours_ploy[i], 3, true);ploy_rects[i] = boundingRect(contours_ploy[i]);minEnclosingCircle(contours_ploy[i], ccs[i], radius[i]);if (contours_ploy[i].size() > 5) {myellipse[i] = fitEllipse(contours_ploy[i]);minRects[i] = minAreaRect(contours_ploy[i]);}}// draw itdrawImg = Mat::zeros(src.size(), src.type());Point2f pts[4];for (size_t t = 0; t < contours.size(); t++) {Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));//rectangle(drawImg, ploy_rects[t], color, 2, 8);//circle(drawImg, ccs[t], radius[t], color, 2, 8);if (contours_ploy[t].size() > 5) {ellipse(drawImg, myellipse[t], color, 1, 8);minRects[t].points(pts);for (int r = 0; r < 4; r++) {line(drawImg, pts[r], pts[(r + 1) % 4], color, 1, 8);}}}imshow(output_win, drawImg);return;}
运行效果:
1、相关理论
2、API介绍
计算矩cv::moments
moments(InputArray array,//输入数据bool binaryImage=false // 是否为二值图像)
API介绍与使用 – cv::moments 计算生成数据
计算轮廓面积cv::contourArea
contourArea(InputArray contour,//输入轮廓数据bool oriented// 默认false、返回绝对值)}
.计算轮廓长度cv::arcLength
arcLength(InputArray curve,//输入曲线数据bool closed// 是否是封闭曲线)}
实现步骤:
提取图像边缘。
发现轮廓。
计算每个轮廓对象的矩。
计算每个对象的中心、弧长、面积
例程
#include #include #include using namespace std;using namespace cv;Mat src, gray_src;int threshold_value = 80;int threshold_max = 255;const char* output_win = "image moents demo";RNG rng(12345);void Demo_Moments(int, void*);int main(int argc, char** argv) {src = imread("D:/photos/45.png");if (!src.data) {printf("could not load image...\n");return -1;}cvtColor(src, gray_src, CV_BGR2GRAY);GaussianBlur(gray_src, gray_src, Size(3, 3), 0, 0);char input_win[] = "input image";namedWindow(input_win, CV_WINDOW_AUTOSIZE);namedWindow(output_win, CV_WINDOW_AUTOSIZE);imshow(input_win, src);createTrackbar("Threshold Value : ", output_win, &threshold_value, threshold_max, Demo_Moments);Demo_Moments(0, 0);waitKey(0);return 0;}void Demo_Moments(int, void*) {Mat canny_output;vector<vector<Point>> contours;vector<Vec4i> hierachy;Canny(gray_src, canny_output, threshold_value, threshold_value * 2, 3, false);findContours(canny_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));vector<Moments> contours_moments(contours.size());vector<Point2f> ccs(contours.size());for (size_t i = 0; i < contours.size(); i++) {contours_moments[i] = moments(contours[i]);ccs[i] = Point(static_cast<float>(contours_moments[i].m10 / contours_moments[i].m00), static_cast<float>(contours_moments[i].m01 / contours_moments[i].m00));}Mat drawImg;// = Mat::zeros(src.size(), CV_8UC3);src.copyTo(drawImg);for (size_t i = 0; i < contours.size(); i++) {if (contours[i].size() < 100) {continue;}Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));printf("center point x : %.2f y : %.2f\n", ccs[i].x, ccs[i].y);printf("contours %d area : %.2f arc length : %.2f\n", i, contourArea(contours[i]), arcLength(contours[i], true));drawContours(drawImg, contours, i, color, 2, 8, hierachy, 0, Point(0, 0));circle(drawImg, ccs[i], 2, color,2, 8);}imshow(output_win, drawImg);return;}
点多边形测试 : 测试一个点是否在给定的多边形内部,边缘或者外部。
2.相关API介绍
cv::pointPolygonTestpointPolygonTest(InputArray contour,// 输入的轮廓Point2f pt, // 测试点bool measureDist // 是否返回距离值,如果是false,1表示在内面,0表示在边界上,-1表示在外部,true返回实际距离)返回数据是double类型
程序示例
#include #include #include using namespace std;using namespace cv;int main(int argc, char** argv) {const int r = 100;Mat src = Mat::zeros(r * 4, r * 4, CV_8UC1);vector<Point2f> vert(6);vert[0] = Point(3 * r / 2, static_cast<int>(1.34*r)); vert[1] = Point(1 * r, 2 * r);vert[2] = Point(3 * r / 2, static_cast<int>(2.866*r)); vert[3] = Point(5 * r / 2, static_cast<int>(2.866*r));vert[4] = Point(3 * r, 2 * r); vert[5] = Point(5 * r / 2, static_cast<int>(1.34*r));for (int i = 0; i < 6; i++) {line(src, vert[i], vert[(i + 1) % 6], Scalar(255), 3, 8, 0);}vector<vector<Point>> contours;vector<Vec4i> hierachy;Mat csrc;src.copyTo(csrc);findContours(csrc, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));Mat raw_dist = Mat::zeros(csrc.size(), CV_32FC1);for (int row = 0; row < raw_dist.rows; row++) {for (int col = 0; col < raw_dist.cols; col++) {double dist = pointPolygonTest(contours[0], Point2f(static_cast<float>(col), static_cast<float>(row)), true);raw_dist.at<float>(row, col) = static_cast<float>(dist);}}double minValue, maxValue;minMaxLoc(raw_dist, &minValue, &maxValue, 0, 0, Mat());Mat drawImg = Mat::zeros(src.size(), CV_8UC3);for (int row = 0; row < drawImg.rows; row++) {for (int col = 0; col < drawImg.cols; col++) {float dist = raw_dist.at<float>(row, col);if (dist > 0) {drawImg.at<Vec3b>(row, col)[0] = (uchar)(abs(1.0 - (dist / maxValue)) * 255);}else if (dist < 0) {drawImg.at<Vec3b>(row, col)[2] = (uchar)(abs(1.0 - (dist / minValue)) * 255);} else {drawImg.at<Vec3b>(row, col)[0] = (uchar)(abs(255 - dist));drawImg.at<Vec3b>(row, col)[1] = (uchar)(abs(255 - dist));drawImg.at<Vec3b>(row, col)[2] = (uchar)(abs(255 - dist));}}}const char* output_win = "point polygon test demo";char input_win[] = "input image";namedWindow(input_win, CV_WINDOW_AUTOSIZE);namedWindow(output_win, CV_WINDOW_AUTOSIZE);imshow(input_win, src);imshow(output_win, drawImg);waitKey(0);return 0;}
来源地址:https://blog.csdn.net/jiyanghao19/article/details/132453249