该系列文章前面几篇介绍了轮廓以及其矩特征、几何特征等等。
本文会介绍轮廓更多的属性,它们可用于识别和分类物体、测量形状和分析图像。
1. 长宽比
长宽比是轮廓的宽度与高度的比值。它可以用于识别物体的形状。
<math xmlns="http://www.w3.org/1998/Math/MathML"> a s p e c t r a d i o = w h aspect radio = \frac{w}{h} </math>aspect radio = hw
2. 矩形度
矩形度是轮廓区域面积与其最小外接矩形区域面积的比值。它是衡量轮廓与矩形相似程度的一个参数。
<math xmlns="http://www.w3.org/1998/Math/MathML"> r e c t a n g u l a r i t y = c o n t o u r A r e a m i n A r e a R e c t A r e a rectangularity = \frac{contourArea}{minAreaRect Area} </math>rectangularity = minAreaRect AreacontourArea
3. 范围
范围是轮廓区域面积与其外接矩形区域面积的比值。它可以用于衡量轮廓的大小。
<math xmlns="http://www.w3.org/1998/Math/MathML"> e x t e n t = c o n t o u r A r e a b o u n d i n g R e c t A r e a extent = \frac{contourArea}{boundingRect Area} </math>extent = boundingRect AreacontourArea
下面的例子是之前使用过的,分别画了一个正方形、三角形、五边形和圆。通过轮廓查找找到它们的轮廓后,计算轮廓的长宽比、矩形度、范围。
cpp
#include <iostream>
#include <opencv2/opencv.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace std;
using namespace cv;
bool ascendSort(vector<Point> a,vector<Point> b)
{
return contourArea(a) > contourArea(b);
}
int main(int argc, char **argv) {
Mat image(1600, 1600, CV_8UC3, Scalar(0, 0, 0));
// 画一个正方形
rectangle(image, Point(100, 100), Point(400, 400), Scalar(255, 255, 0), -1);
// 画一个三角形
Point trianglePoints[3] = {Point(500, 800), Point(250, 1300), Point(800, 1100)};
fillConvexPoly(image, trianglePoints, 3, Scalar(255, 255, 0));
// 画一个五边形
Point pentagonPoints[5];
for (int i = 0; i < 5; i++) {
pentagonPoints[i] = Point(600 + 200 * cos(2 * CV_PI * i / 5), 600 + 200 * sin(2 * CV_PI * i / 5));
}
fillConvexPoly(image, pentagonPoints, 5, Scalar(255, 255, 0));
// 画一个圆形
circle(image, Point(1200, 1200), 300, Scalar(255, 255, 0), -1);
Mat gray,thresh;
cvtColor(image, gray, cv::COLOR_BGR2GRAY);
threshold(gray,thresh,0,255,THRESH_BINARY | THRESH_OTSU);
imshow("thresh", thresh);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(thresh, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
sort(contours.begin(), contours.end(), ascendSort);//ascending sort
for (size_t i = 0; i< contours.size(); i++) {
double area = contourArea(contours[i]);
Rect rect = boundingRect(contours[i]);
RotatedRect rrt = minAreaRect(contours[i]);
float aspect_radio = (float)rect.width/rect.height;
double rectangularity = area / (rrt.size.width*rrt.size.height);
double extent = area / (rect.width*rect.height);
printf("aspect_radio = %f, rectangularity = %f, extent = %f \n",aspect_radio,rectangularity,extent);
}
imshow("result", image);
waitKey(0);
return 0;
}
执行结果:
ini
aspect_radio = 1.000000, rectangularity = 0.784830, extent = 0.780308
aspect_radio = 1.099800, rectangularity = 0.500556, extent = 0.407986
aspect_radio = 0.950262, rectangularity = 0.691453, extent = 0.686282
aspect_radio = 1.000000, rectangularity = 1.000000, extent = 0.993367
之前,在该系列文章第十六篇中使用 approxPolyDP() 函数 把圆近似成16边形。现在通过计算矩形度 还是很容易区分出圆的,圆的矩形度 : <math xmlns="http://www.w3.org/1998/Math/MathML"> π R 2 4 R 2 = π 4 ≈ 0.785 \frac{\pi R^2}{4R^2} = \frac{\pi}{4} \approx 0.785 </math>4R2πR2 = 4π ≈ 0.785
4. 极点
极点是指轮廓的最顶部,最底部,最右侧和最左侧的点。
下面的代码,读取一张树叶的图,进行二值化,再通过轮廓查找找到最大的轮廓,计算出其极点并在原图中标记出来。
cpp
#include <iostream>
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace std;
using namespace cv;
bool ascendSort(vector<Point> a,vector<Point> b)
{
return contourArea(a) > contourArea(b);
}
int main(int argc, char **argv) {
Mat src = imread(".../leaf.png");
imshow("src", src);
Mat gray,thresh;
cvtColor(src, gray, cv::COLOR_BGR2GRAY);
threshold(gray,thresh,0,255,THRESH_BINARY_INV | THRESH_OTSU);
imshow("thresh", thresh);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(thresh, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
sort(contours.begin(), contours.end(), ascendSort);//ascending sort
cout << "contours.size() = " << contours.size() << endl;
//计算轮廓极值点
Point left = *min_element(contours[0].begin(), contours[0].end(),
[](const Point& lhs, const Point& rhs) {
return lhs.x < rhs.x;
});
Point right = *max_element(contours[0].begin(), contours[0].end(),
[](const Point& lhs, const Point& rhs) {
return lhs.x < rhs.x;
});
Point top = *min_element(contours[0].begin(), contours[0].end(),
[](const Point& lhs, const Point& rhs) {
return lhs.y < rhs.y;
});
Point bottom = *max_element(contours[0].begin(), contours[0].end(),
[](const Point& lhs, const Point& rhs) {
return lhs.y < rhs.y;
});
circle(src,left,2,Scalar(255,0,0),8);
circle(src,right,2,Scalar(255,0,0),8);
circle(src,top,2,Scalar(255,0,0),8);
circle(src,bottom,2,Scalar(255,0,0),8);
int fontFace = FONT_HERSHEY_PLAIN;
double fontScale = 2;
int thickness = 6;
putText(src,"left",left,fontFace,fontScale,Scalar(255, 0, 0),thickness);
putText(src,"right",right,fontFace,fontScale,Scalar(255, 0, 0),thickness);
putText(src,"top",top,fontFace,fontScale,Scalar(255, 0, 0),thickness);
putText(src,"bottom",bottom,fontFace,fontScale,Scalar(255, 0, 0),thickness);
imshow("result", src);
waitKey(0);
return 0;
}
5. 方向
方向是是轮廓的倾斜角度。方向可以用于识别物体的方向。
下面的代码,通过将轮廓进行椭圆拟合来获取轮廓的方向。
ini
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace std;
using namespace cv;
bool ascendSort(vector<Point> a,vector<Point> b)
{
return contourArea(a) > contourArea(b);
}
int main(int argc, char **argv) {
Mat src = imread(".../spoon.jpg");
imshow("src", src);
Mat gray,thresh;
cvtColor(src, gray, cv::COLOR_BGR2GRAY);
threshold(gray,thresh,0,255,THRESH_BINARY_INV | THRESH_OTSU);
imshow("thresh", thresh);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(thresh, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
sort(contours.begin(), contours.end(), ascendSort);//ascending sort
for (size_t i = 0; i< contours.size(); i++) {
double area = contourArea(contours[i]);
if (area < 1000) {
continue;
}
RotatedRect rrt = fitEllipse(contours[i]);
Point2f center = rrt.center;
float angle = rrt.angle;
ellipse(src,rrt, Scalar(0, 0, 255), 8, 8);
std::string text = "angle:" + to_string(angle);
int fontFace = FONT_HERSHEY_PLAIN;
double fontScale = 5;
int thickness = 8;
putText(src,text,center,fontFace,fontScale,Scalar(255, 0, 0),thickness);
}
imshow("result", src);
waitKey(0);
return 0;
}
6. 等效直径
等效直径是面积与轮廓面积相同的圆的直径。它可以用于衡量轮廓的大小和形状。
<math xmlns="http://www.w3.org/1998/Math/MathML"> e q u i v a l e n t d i a m e t e r = 4 ∗ c o n t o u r A r e a π equivalent diameter = \sqrt{\frac{4*contourArea}{\pi}} </math>equivalent diameter = π4∗contourArea
7. 坚实度
坚实度是轮廓区域面积与其凸包区域面积的比值。它可以用于衡量轮廓的密实程度。
<math xmlns="http://www.w3.org/1998/Math/MathML"> s o l i d i t y = c o n t o u r A r e a c o n v e x H u l l A r e a solidity = \frac{contourArea}{convexHull Area} </math>solidity = convexHull AreacontourArea
8. 总结
本文介绍了很多轮廓的属性,这些属性可以根据特定的应用需求进行选择。例如,在物体识别中,可以使用轮廓的长宽比、矩形度、范围和坚实度来识别物体的形状和大小。在形状分析中,可以使用轮廓的等效直径和方向来分析形状的几何特性。