1、图像细化
需要安装扩展包
//图像细化
int test1()
{//中文字进行细化Mat img = imread("LearnCV_black.png",IMREAD_ANYCOLOR); if (img.empty()){cout << "请确认图像文件名称是否正确" << endl;return -1;}//英文字 + 实心圆和圆环细化Mat words = Mat::zeros(100,200,CV_8UC1);//创建一个黑色的背景图片putText(words,"Learn",Point(30,30),2,1,Scalar(255),2);//添加英文putText(words,"OpenCV 4",Point(30,60),2,1,Scalar(255),2);circle(words,Point(80,75),10,Scalar(255), - 1);//添加实心圆circle(words,Point(130,75),10,Scalar(255),3);//添加圆环//进行细化Mat thin1, thin2;ximgproc::thinning(img, thin1, 0);//注意类名ximgproc::thinning(words,thin2, 0);//显示处理结果imshow("thinl", thin1); imshow("img", img);namedWindow("thin2",WINDOW_NORMAL);imshow ("thin2", thin2);namedWindow("words",WINDOW_NORMAL);imshow("words", words);waitKey(0);return 0;
}
2、轮廓检测
//轮廓检测
int test2()
{system("color F0");//更改输出界面颜色Mat img = imread("F:/testMap/key.png");if (img.empty()){cout << "请确认图像文件名称是否正确" << endl;return -1;}imshow("原图", img);Mat gray, binary;cvtColor(img, gray, COLOR_BGR2GRAY);//转化成灰度图GaussianBlur(gray, gray, Size(13, 13), 4, 4);//平滑滤波threshold(gray, binary, 170, 255, THRESH_BINARY | THRESH_OTSU);//自适应二值化//轮廓发现与绘制vector<vector<Point>> contours;//轮廓vector<Vec4i> hierarchy; //存放轮廓结构变量findContours(binary, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point());//绘制轮廓for (int i = 0; i < hierarchy.size(); i++){cout << hierarchy[i] << endl;}for (int t = 0; t < contours.size(); t++){drawContours(img, contours, t, Scalar(0, 0, 255), 2, 8);imshow("轮廓检测结果", img);waitKey(0);}
}
3、轮廓信息统计
//轮廓信息统计
int test3()
{system("color F0");//更改输出界面颜色//用四个点表示三角形轮廓vector<Point> contour;contour.push_back(Point2f(0, 0));contour.push_back(Point2f(10, 0));contour.push_back(Point2f(10, 10));contour.push_back(Point2f(5, 5));double area = contourArea(contour);cout << "area =" << area << endl;double length0 = arcLength(contour, true);double length1 = arcLength(contour, false);cout << "length0 =" << length0 << endl;cout << "lengthl =" << length1 << endl;cout << "图像轮廓面积" << endl;waitKey(0);Mat img = imread("F:/testMap/key.png");if (img.empty()){cout << "请确认图像文件名称是否正确" << endl;return -1;}Mat gray, binary;cvtColor(img, gray, COLOR_BGR2GRAY); //转化成灰度图GaussianBlur(gray, gray, Size(9, 9), 2, 2);//平滑滤波threshold(gray, binary, 170, 255, THRESH_BINARY | THRESH_OTSU);//自适应二值化//轮廓检测vector<vector<Point>> contours;//轮廓vector<Vec4i> hierarchy;//存放轮廓结构变量findContours(binary, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point());//输出轮廓面积for (int t = 0; t < contours.size(); t++){double area1 = contourArea(contours[t]);cout << "第" << t << "轮廓面积 = " << area1 << endl;}//输出轮廓长度for (int t = 0; t < contours.size(); t++){double length2 = arcLength(contours[t],true);cout << "第" << t << "轮廓长度 = " << length2 << endl;}return 0;
}
4、轮廓外接多边形
int test4()
{Mat img = imread("F:/testMap/stuff.png");if (img.empty()){cout << "请确认图像文件名称是否正确" << endl;return -1;}Mat img1, img2;img.copyTo(img1); //深拷贝用来绘制最大外接矩形img.copyTo(img2);//深拷贝用来绘制最小外接矩形imshow("img", img);//去噪声与二值化Mat canny;Canny(img, canny, 80, 160, 3, false);imshow("", canny);//膨胀运算,将细小缝隙填补上Mat kernel = getStructuringElement(0, Size(3, 3)); dilate(canny, canny, kernel);//轮廓发现与绘制vector<vector<Point>> contours;vector<Vec4i> hierarchy;findContours(canny, contours, hierarchy, 0, 2, Point());//寻找轮廓的外接矩形for (int n = 0; n < contours.size(); n++){// 最大外接矩形Rect rect = boundingRect(contours[n]);rectangle(img1, rect, Scalar(0, 0, 255), 2, 8, 0);//最小外接矩形RotatedRect rrect = minAreaRect(contours[n]); Point2f points[4];rrect.points(points);//读取最小外接矩形的四个顶点Point2f cpt = rrect.center;//最小外接矩形的中心//绘制旋转矩形与中心位置for (int i = 0; i < 4; i++){if (i == 3){line(img2, points[i], points[0], Scalar(0, 255, 0), 2, 8, 0);break;}line(img2, points[i], points[i + 1], Scalar(0, 255, 0), 2, 8, 0);}//绘制矩形的中心circle(img2, cpt, 4, Scalar(255, 0, 0), -8, 0);}//输出绘制外接矩形的结果imshow("max", img1);imshow("min", img2);cout << "下面是多边形拟合" << endl;waitKey(0);Mat approx = imread("F:/testMap/approx.png");if (approx.empty()){cout << "请确认图像文件名称是否正确" << endl;return -1;}//边缘检测Mat canny2;Canny(approx, canny2, 80, 160, 3, false);//膨胀运算Mat kernel2 = getStructuringElement(0, Size(3, 3));dilate(canny2, canny2, kernel2);//轮廓发现与绘制vector<vector<Point>> contours2;vector<Vec4i> hierarchy2;findContours(canny2, contours2, hierarchy2, 0, 2, Point());//绘制多边形for (int t = 0; t < contours2.size(); t++){//用最小外接矩形求取轮廓中心RotatedRect rrect = minAreaRect(contours2[t]);Point2f center = rrect.center;circle(approx, center, 2, Scalar(0, 255, 0), 2, 8, 0);Mat result;approxPolyDP(contours2[t], result, 4, true); //多边形拟合drawapp(result, approx);cout << "corners : " << result.rows << endl;//判断形状和绘制轮廓if (result.rows == 3){putText(approx, "triangle", center, 0, 1, Scalar(0, 255, 0), 1, 8);}if (result.rows == 4){putText(approx, "rectangle", center, 0, 1, Scalar(0, 255, 0), 1, 8);}if (result.rows == 8){putText(approx, "poly-8", center, 0, 1, Scalar(0, 255, 0), 1, 8);}if (result.rows > 12){putText(approx, "circle", center, 0, 1, Scalar(0, 255, 0), 1, 8);}}imshow("result", approx);//输出绘制外接矩形的结果imshow(" max", img1);imshow("min", img2);waitKey(0);
}