-
Notifications
You must be signed in to change notification settings - Fork 18
/
utils.cpp
211 lines (174 loc) · 6.5 KB
/
utils.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "utils.hpp"
float getNorm2(float x,float y)
{
return sqrt(x*x+y*y);
}
cv::Mat getTransForm(cv::Mat &src_img, cv::Point2f order_rect[4]) //透视变换
{cv::Point2f w1=order_rect[0]-order_rect[1];
cv::Point2f w2=order_rect[2]-order_rect[3];
auto width1 = getNorm2(w1.x,w1.y);
auto width2 = getNorm2(w2.x,w2.y);
auto maxWidth = std::max(width1,width2);
cv::Point2f h1=order_rect[0]-order_rect[3];
cv::Point2f h2=order_rect[1]-order_rect[2];
auto height1 = getNorm2(h1.x,h1.y);
auto height2 = getNorm2(h2.x,h2.y);
auto maxHeight = std::max(height1,height2);
// 透视变换
std::vector<cv::Point2f> pts_ori(4);
std::vector<cv::Point2f> pts_std(4);
pts_ori[0]=order_rect[0];
pts_ori[1]=order_rect[1];
pts_ori[2]=order_rect[2];
pts_ori[3]=order_rect[3];
pts_std[0]=cv::Point2f(0,0);
pts_std[1]=cv::Point2f(maxWidth,0);
pts_std[2]=cv::Point2f(maxWidth,maxHeight);
pts_std[3]=cv::Point2f(0,maxHeight);
cv::Mat M = cv::getPerspectiveTransform(pts_ori,pts_std);
cv:: Mat dstimg;
cv::warpPerspective(src_img,dstimg,M,cv::Size(maxWidth,maxHeight));
return dstimg;
}
cv::Mat get_split_merge(cv::Mat &img) //双层车牌 分割 拼接
{
cv::Rect upper_rect_area = cv::Rect(0,0,img.cols,int(5.0/12*img.rows));
cv::Rect lower_rect_area = cv::Rect(0,int(1.0/3*img.rows),img.cols,img.rows-int(1.0/3*img.rows));
cv::Mat img_upper = img(upper_rect_area);
cv::Mat img_lower =img(lower_rect_area);
cv::resize(img_upper,img_upper,img_lower.size());
cv::Mat out(img_lower.rows,img_lower.cols+img_upper.cols, CV_8UC3, cv::Scalar(114, 114, 114));
img_upper.copyTo(out(cv::Rect(0,0,img_upper.cols,img_upper.rows)));
img_lower.copyTo(out(cv::Rect(img_upper.cols,0,img_lower.cols,img_lower.rows)));
return out;
}
std::string getHouZhui(std::string fileName)
{
// std::string fileName="/home/xiaolei/23.jpg";
int pos=fileName.find_last_of(std::string("."));
std::string houZui=fileName.substr(pos+1);
return houZui;
}
int readFileList(char *basePath,std::vector<std::string> &fileList,std::vector<std::string> fileType)
{
DIR *dir;
struct dirent *ptr;
char base[1000];
if ((dir=opendir(basePath)) == NULL)
{
perror("Open dir error...");
exit(1);
}
while ((ptr=readdir(dir)) != NULL)
{
if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0) ///current dir OR parrent dir
continue;
else if(ptr->d_type == 8)
{ ///file
if (fileType.size())
{
std::string houZui=getHouZhui(std::string(ptr->d_name));
for (auto &s:fileType)
{
if (houZui==s)
{
fileList.push_back(basePath+std::string("/")+std::string(ptr->d_name));
break;
}
}
}
else
{
fileList.push_back(basePath+std::string("/")+std::string(ptr->d_name));
}
}
else if(ptr->d_type == 10) ///link file
printf("d_name:%s/%s\n",basePath,ptr->d_name);
else if(ptr->d_type == 4) ///dir
{
memset(base,'\0',sizeof(base));
strcpy(base,basePath);
strcat(base,"/");
strcat(base,ptr->d_name);
readFileList(base,fileList,fileType);
}
}
closedir(dir);
return 1;
}
void draw_rect(const cv::Mat& image, const std::vector<boundingBox>bboxes,const char* class_names[])
{
// static const char* class_names[] = {
// "head", "leg", "hand", "back", "nostd", "body", "plate", "logo"};
// cv::Mat image = bgr.clone();
for (size_t i = 0; i < bboxes.size(); i++)
{
// const Object& obj = objects[i];
const boundingBox &obj= bboxes[i];
// fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob,
// obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height);
cv::Scalar color = cv::Scalar(color_list1[obj.label][0], color_list1[obj.label][1], color_list1[obj.label][2]);
float c_mean = cv::mean(color)[0];
cv::Scalar txt_color;
if (c_mean > 0.5){
txt_color = cv::Scalar(0, 0, 0);
}else{
txt_color = cv::Scalar(255, 255, 255);
}
cv::Rect myRect(obj.x,obj.y,obj.w,obj.h);
cv::rectangle(image,myRect, color * 255, 2);
char text[256];
sprintf(text, "%s %.1f%%", class_names[obj.label], obj.score * 100);
int baseLine = 0;
cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.4, 1, &baseLine);
cv::Scalar txt_bk_color = color * 0.7 * 255;
int x = obj.x;
int y = obj.y + 1;
//int y = obj.rect.y - label_size.height - baseLine;
if (y > image.rows)
y = image.rows;
//if (x + label_size.width > image.cols)
//x = image.cols - label_size.width;
cv::rectangle(image, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)),
txt_bk_color,-1);
cv::putText(image, text, cv::Point(x, y + label_size.height),
cv::FONT_HERSHEY_SIMPLEX, 0.4, txt_color, 1);
}
}
bool cmpBox(boundingBox b1, boundingBox b2)
{
return b1.score > b2.score;
}
float getIou(boundingBox b1,boundingBox b2) //计算IOU
{
int xl1 = b1.x; //左
int xr1 = b1.w+b1.x; // 右
int yt1=b1.y; //顶
int yb1 = b1.y+b1.h; //底
int xl2 = b2.x; //左
int xr2 = b2.w+b2.x; // 右
int yt2=b2.y; //顶
int yb2 = b2.y+b2.h; //底
int x11 =std::max(xl1,xl2);
int y11 = std::max(yt1,yt2);
int x22 = std::min(xr1,xr2);
int y22 = std::min(yb1,yb2);
float intersectionArea= (x22-x11)*(y22-y11); //交集
float unionArea = (xr1-xl1)*(yb1-yt1)+(xr2-xl2)*(yb2-yt2)-intersectionArea; //并集
return 1.0f*intersectionArea/unionArea;
}
void myNms(std::vector<boundingBox>&bboxes,float score)
{
std::sort(bboxes.begin(),bboxes.end(),cmpBox);
for(int i = 0; i<bboxes.size()-1; i++)
{
for(int j = i+1;j<bboxes.size(); j++)
{
if(getIou(bboxes[i],bboxes[j])>score)
{
bboxes.erase(bboxes.begin()+j);
j--;
}
}
}
}