-
Notifications
You must be signed in to change notification settings - Fork 14
/
ShapeRecognize.cpp
183 lines (163 loc) · 5.03 KB
/
ShapeRecognize.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "traffic.h"
#include "math_utils.h"
#define MINIST_SIZE 200//the threshol of the bonudingbox size,if the size of the bounding box is smaller than it , make it invalid
//Helper function to find a cosine of angle between vectors from pt0->pt1 and pt0->pt2
static double angle(cv::Point pt1, cv::Point pt2, cv::Point pt0)
{
double dx1 = pt1.x - pt0.x;
double dy1 = pt1.y - pt0.y;
double dx2 = pt2.x - pt0.x;
double dy2 = pt2.y - pt0.y;
return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}
//识别nhls图像中矩形框内的颜色
int RecColorInBox(Mat img)
{
int redCount=0;
int blueCount=0;
int yellowCount=0;
assert(img.channels() == 1);
for (int i = 0; i < img.rows; ++i)
{
const uchar *img_data = img.ptr<uchar> (i);
for (int j = 0; j < img.cols; ++j)
{
uchar pixelVal=*img_data++;
if(pixelVal==R_VALUE)
redCount++;
else if(pixelVal==Y_VALUE)
yellowCount++;
else if(pixelVal==B_VALUE)
blueCount++;
else
continue;
}
}
//find max value
int finalVal=0;
int tmpCount=0;
if (redCount>=yellowCount)
{
tmpCount=redCount;
finalVal=R_VALUE;
}else{
tmpCount=yellowCount;
finalVal=Y_VALUE;
}
if (blueCount>tmpCount)
finalVal=B_VALUE;
return finalVal;
}
// Helper function to display text in the center of a contour
void setLabel(cv::Mat& im, const std::string label, Rect r)
{
int fontface = cv::FONT_HERSHEY_SIMPLEX;
double scale = 0.4;
int thickness = 1;
int baseline = 0;
cv::Size text = cv::getTextSize(label, fontface, scale, thickness, &baseline);
cv::Point pt(r.x + ((r.width - text.width) / 2), r.y + ((r.height + text.height) / 2));
cv::rectangle(im, pt + cv::Point(0, baseline), pt + cv::Point(text.width, -text.height), CV_RGB(255,255,255), CV_FILLED);
cv::putText(im, label, pt, fontface, scale, CV_RGB(0,0,0), thickness, 8);
}
Mat ShapeRecognize(Mat src,vector<ShapeRecResult>&shapeResult)
{
ShapeRecResult tmp;
if (src.channels()!=1)
{
cout<<"The input image for shape recognition must be binary"<<endl;
}
else
{
// Convert to binary image using Canny
Mat edge;
Canny(src,edge,0,50,5);
#if ISDEBUG_TS
imshow("edge",edge);
waitKey(5);
#endif
//Find contours
vector<vector<Point>> contours;
findContours(edge.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
// The array for storing the approximation curve
std::vector<cv::Point> approx;
// We'll put the labels in this destination image
cv::Mat dst = src.clone();
for (int i = 0; i < contours.size(); i++)
{
Rect rect=boundingRect(contours[i]);
//constraints of width-height ratio
bool RatioConstraint=(abs(1-(float)(rect.width)/((float)(rect.height)))<0.2)||(((float)rect.height/(float)rect.width<2)&&((float)rect.height/(float)rect.width>1));
if(!RatioConstraint)continue;
// Approximate contour with accuracy proportional to the contour perimeter
cv::approxPolyDP(cv::Mat(contours[i]), approx, cv::arcLength(cv::Mat(contours[i]), true) * 0.02, true);
// Skip small or non-convex objects
if (std::fabs(cv::contourArea(contours[i])) < MINIST_SIZE || !cv::isContourConvex(approx))
continue;
// Number of vertices of polygonal curve
int vtc = approx.size();
if (vtc == 3)
{
//TODO:需要进一步跟进三个角的角度值判断,标志牌一般为正三角形
// Get the cosines of all corners
std::vector<double> cos;
int checkpoint=2;
for (checkpoint = 2; checkpoint < vtc+1; checkpoint++)
{
double ang=angle(approx[checkpoint%vtc], approx[checkpoint-2], approx[checkpoint-1]);
if (ang<0||ang>0.7)break;
}
if (checkpoint==4)
{
setLabel(dst, "TRI", rect); // Triangles
tmp.box=rect;
tmp.shape=TRIANGLE;//三角形形状为1
Mat cutMat=src(rect);
tmp.color=RecColorInBox(cutMat);
shapeResult.push_back(tmp);
}
}
else if (vtc >= 4 && vtc <= 6)
{
// Get the cosines of all corners
std::vector<double> cos;
for (int j = 2; j < vtc+1; j++)
cos.push_back(angle(approx[j%vtc], approx[j-2], approx[j-1]));
// Sort ascending the cosine values
std::sort(cos.begin(), cos.end());
// Get the lowest and the highest cosine
double mincos = cos.front();
double maxcos = cos.back();
// Use the degrees obtained above and the number of vertices
// to determine the shape of the contour
if (vtc == 4 && mincos >= -0.1 && maxcos <= 0.3)
{
setLabel(dst, "RECT", rect);
tmp.box=rect;
tmp.shape=RECTANGLE;//2表示矩形
Mat cutMat=src(rect);
tmp.color=RecColorInBox(cutMat);
shapeResult.push_back(tmp);
}
}
else
{
// Detect and label circles
double area = cv::contourArea(contours[i]);
cv::Rect r = cv::boundingRect(contours[i]);
float radius = (float)(r.width)/ 2;
if (std::abs(1 - ((double)r.width / r.height)) <= 0.2 &&
std::abs(1 - (area / (CV_PI * std::pow(radius, 2)))) <= 0.2)
{
setLabel(dst, "CIR", rect);
tmp.box=rect;
tmp.shape=CIRCLE;
Mat cutMat=src(rect);
tmp.color=RecColorInBox(cutMat);
shapeResult.push_back(tmp);
}
}
}
return dst;
}
}