赞
踩
#include <opencv2/imgcodecs.hpp> #include <opencv2/highgui.hpp> #include <opencv2/imgproc.hpp> #include <iostream> using namespace cv; using namespace std; /// Color Detection // void getContours(Mat imgDil, Mat img) { vector<vector<Point>> contours; vector<Vec4i> hierarchy; findContours(imgDil, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);//找轮廓 //drawContours(img, contours, -1, Scalar(255, 0, 255), 2); vector<vector<Point>> conPoly(contours.size());//找轮廓点 vector<Rect> boundRect(contours.size());//画外框 for (int i = 0; i < contours.size(); i++) { int area = contourArea(contours[i]); cout << area << endl; string objectType; if (area > 1000)//因为我们做实验的时候画了一个很小的圆圈 避免干扰 不做去噪声处理 就做判断 { float peri = arcLength(contours[i], true);//计算闭合轮廓的周长 approxPolyDP(contours[i], conPoly[i], 0.02 * peri, true);//找到轮廓点 cout << conPoly[i].size() << endl;//计算每一个轮廓点的个数 boundRect[i] = boundingRect(conPoly[i]); int objCor = (int)conPoly[i].size();//判断形状 矩形 圆形 正方形 三角形 if (objCor == 3) { objectType = "Tri"; }//三角形 else if (objCor == 4)//正方形 { float aspRatio = (float)boundRect[i].width / (float)boundRect[i].height; cout << aspRatio << endl; if (aspRatio > 0.95 && aspRatio < 1.05) { objectType = "Square"; } else { objectType = "Rect"; } } else if (objCor > 4) { objectType = "Circle"; }//圆形 drawContours(img, conPoly, i, Scalar(255, 0, 255), 2);// rectangle(img, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 255, 0), 5);//画外框 putText(img, objectType, { boundRect[i].x,boundRect[i].y – 5 }, FONT_HERSHEY_PLAIN, 1, Scalar(0, 69, 255), 2);//放判定的字 } } } void main() { string path = "Resources / shapes.png"; Mat img = imread(path); Mat imgGray, imgBlur, imgCanny, imgDil, imgErode; // Preprocessing cvtColor(img, imgGray, COLOR_BGR2GRAY); GaussianBlur(imgGray, imgBlur, Size(3, 3), 3, 0); Canny(imgBlur, imgCanny, 25, 75); Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3)); dilate(imgCanny, imgDil, kernel); getContours(imgDil, img); imshow("Image", img); //imshow("Image Gray", imgGray); //imshow("Image Blur", imgBlur); //imshow("Image Canny", imgCanny); //imshow("Image Dil", imgDil); waitKey(0); }
drawContours//绘制轮廓,用于绘制找到的图像轮廓:
InputOutputArray image,//要绘制轮廓的图像
InputArrayOfArrays contours,//所有输入的轮廓,每个轮廓被保存成一个point向量
int contourIdx,//指定要绘制轮廓的编号,如果是负数,则绘制所有的轮廓
const Scalar& color,//绘制轮廓所用的颜色
int thickness = 1, //绘制轮廓的线的粗细,如果是负数,则轮廓内部被填充
int lineType = 8, /绘制轮廓的线的连通性
InputArray hierarchy = noArray(),//关于层级的可选参数,只有绘制部分轮廓时才会用到
int maxLevel = INT_MAX,//绘制轮廓的最高级别,这个参数只有hierarchy有效的时候才有效
//maxLevel=0,绘制与输入轮廓属于同一等级的所有轮廓即输入轮廓和与其相邻的轮廓
//maxLevel=1, 绘制与输入轮廓同一等级的所有轮廓与其子节点。
//maxLevel=2,绘制与输入轮廓同一等级的所有轮廓与其子节点以及子节点的子节点
注意:findContours()运行的时候,这个图像会被直接涂改,因此如果是将来还有用的图像,应该复制之后再传给findContours()。
findContours//提取轮廓,用于提取图像的轮廓:
InputOutputArray image,//输入图像,必须是8位单通道图像,并且应该转化成二值的
OutputArrayOfArrays contours,//检测到的轮廓,每个轮廓被表示成一个point向量
OutputArray hierarchy,//可选的输出向量,包含图像的拓扑信息。其中元素的个数和检测到的轮廓的数量相等
int mode,//说明需要的轮廓类型和希望的返回值方式
int method,//轮廓近似方法
Point offset = Point()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。