-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftSaliencyDetection2.cpp
330 lines (261 loc) · 9.71 KB
/
ftSaliencyDetection2.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include "opencv2/opencv.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
#include <sstream>
#include <iomanip>
#include <string.h>
#include <fstream>
#include <iostream>
#include <sys/time.h>
#include "classification.hpp"
using namespace cv;
using namespace std;
std::vector<cv::Rect> fineMinAreaRect(Mat &threshold_output,Mat &org_image)
{
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
std::vector<cv::Rect> detectedRect;
//寻找轮廓
findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
//对每个找到的轮廓创建可倾斜的边界框
vector<RotatedRect> minRect(contours.size());
vector<RotatedRect> orgRect(contours.size());
float response[contours.size()];
int num=contours.size();
int ROI_x_min[num],ROI_y_min[num],ROI_width[num],ROI_height[num];
for (int i = 0; i < contours.size(); i++)
{
minRect[i] = minAreaRect(Mat(contours[i]));//Input vector of 2D points, stored in:
//std::vector<> or Mat (C++ interface)
orgRect[i].center.x= minRect[i].center.x*4;
orgRect[i].center.y = minRect[i].center.y*4;
orgRect[i].size.height = minRect[i].size.height*8;
orgRect[i].size.width = minRect[i].size.width*8;
orgRect[i].angle = minRect[i].angle*0;
// cout<<"contours["<<i<<"].size() "<<contours[i].size()<<" minRect[i].center.x "<<minRect[i].center.x<<endl;
ROI_x_min[i] = orgRect[i].center.x - orgRect[i].size.width/2 ;
ROI_y_min[i] = orgRect[i].center.y - orgRect[i].size.width/2 ;
ROI_x_min[i] = (ROI_x_min[i] > 0 ? ROI_x_min[i]:0);
ROI_y_min[i] = (ROI_y_min[i] > 0 ? ROI_y_min[i]:0);
ROI_width[i] = orgRect[i].size.width;
ROI_height[i] = orgRect[i].size.height;
ROI_width[i] = (ROI_width[i]+ROI_x_min[i] > (org_image.cols-1) ? (org_image.cols-1) - ROI_x_min[i]:ROI_width[i]);
ROI_height[i] = (ROI_height[i]+ROI_y_min[i] > (org_image.rows-1)? (org_image.rows-1) - ROI_y_min[i]:ROI_height[i]);
cout<<ROI_x_min[i]<<" "<<ROI_y_min[i] <<" "<<ROI_width[i]<<" "<<ROI_height[i]<<endl;
if(ROI_width[i]<200&&ROI_height[i]<200&&ROI_width[i]>4&&ROI_height[i]>4&&ROI_x_min[i]>281)
{ cv::Rect roiFound;
roiFound.x = ROI_x_min[i];
roiFound.y = ROI_y_min[i];
roiFound.width = ROI_width[i];
roiFound.height = ROI_height[i];
detectedRect.push_back(roiFound);
}
}
return detectedRect;
}
void SaliencyProcess(Mat &I,Mat &invDFTcvt)
{
if(I.empty())
return;
if(I.channels()==3)
cvtColor(I,I,CV_RGB2GRAY);
Mat planes[] = { Mat_<float>(I), Mat::zeros(I.size(), CV_32F) };
Mat complexI; //复数矩阵
merge(planes, 2, complexI); //把单通道矩阵组合成复数形式的双通道矩阵
dft(complexI, complexI); // 使用离散傅立叶变换
//对复数矩阵进行处理,方法为谱残差
Mat mag,pha,mag_mean;
Mat Re,Im;
split(complexI,planes); //分离复数到实部和虚部
Re=planes[0]; //实部
Im=planes[1]; //74
magnitude(Re,Im,mag); //计算幅值
phase(Re,Im,pha); //计算相角
float *pre,*pim,*pm,*pp;
//对幅值进行对数化
for(int i=0;i<mag.rows;i++)
{
pm=mag.ptr<float>(i);
for(int j=0;j<mag.cols;j++)
{
*pm=log(*pm);
pm++;
}
}
blur(mag, mag_mean, Size(5, 5)); //对数谱的均值滤波
mag = mag - mag_mean; //求取对数频谱残差
//把对数谱残差的幅值和相角划归到复数形式
for(int i=0;i<mag.rows;i++)
{
pre=Re.ptr<float>(i);
pim=Im.ptr<float>(i);
pm=mag.ptr<float>(i);
pp=pha.ptr<float>(i);
for(int j=0;j<mag.cols;j++)
{
*pm=exp(*pm);
*pre=*pm * cos(*pp);
*pim=*pm * sin(*pp);
pre++;
pim++;
pm++;
pp++;
}
}
Mat planes1[] = { Mat_<float>(Re),Mat_<float>(Im) };
merge(planes1, 2, complexI); //重新整合实部和虚部组成双通道形式的复数矩阵
idft(complexI, complexI, DFT_SCALE); // 傅立叶反变换
split(complexI, planes); //分离复数到实部和虚部
Re=planes[0];
Im=planes[1];
magnitude(Re,Im,mag); //计算幅值和相角
for(int i=0;i<mag.rows;i++)
{
pm=mag.ptr<float>(i);
for(int j=0;j<mag.cols;j++)
{
*pm=(*pm) * (*pm);
pm++;
}
}
GaussianBlur(mag,mag,Size(7,7),2.5,2.5);
Mat invDFT;
normalize(mag,invDFT,0,255,NORM_MINMAX); //归一化到[0,255]供显示
invDFT.convertTo(invDFTcvt, CV_8U); //转化成CV_8U型
}
string model_caffe = "./params/cifar10_quick.prototxt";
string para_caffe = "./params/hy_model_iter_320000.caffemodel";
string mean_caffe = "./params/cifar10_mean.binaryproto";
string label_caffe = "./params/label.txt";
int main(int argc,char *argv[])
{
const char *videoname = (argc >= 2 ? argv[1] : "GOPR0591_jiequ.avi");
::google::InitGoogleLogging(argv[0]);
const string& model_file = model_caffe;
const string& trained_file = para_caffe;
const string& mean_file = mean_caffe;
const string& label_file = label_caffe;
std::cout<<"##########################"<<std::endl;
std::cout<<"model_file is "<<model_file<<std::endl;
std::cout<<"trained_file is "<<trained_file<<std::endl;
std::cout<<"##########################"<<std::endl;
Classifier classifier(model_file, trained_file, mean_file, label_file);
VideoCapture capture(videoname);
capture.set(CV_CAP_PROP_POS_FRAMES,10);/// play from 10
if (!capture.isOpened())
{
std::cout<< "No Input Image"<<std::endl;
return 1;
}
namedWindow("org_image",CV_WINDOW_NORMAL);
char Tex[20];
char car1Tex[40];
char car0Tex[40];
VideoWriter writer("save1.avi", CV_FOURCC('M', 'J', 'P', 'G'), 20.0, Size(1280, 720));
int N=0;
while(1)
{
N=N+1;
Mat I_org; // 当前视频帧
if (!capture.read(I_org))
break;
Mat I,imOrg;
I_org.copyTo(imOrg);
struct timeval tv0,tv1;
gettimeofday(&tv0,NULL);
int startTime = int(tv0.tv_sec * 1000 + tv0.tv_usec / 1000);
resize(I_org,I,Size(I_org.cols/4,I_org.rows/4));
cout<<"I_org.cols "<<I_org.cols<<" I_org.rows"<<I_org.rows<<endl;
Mat invDFTcvt,invDFTcvtSave;
SaliencyProcess(I,invDFTcvt);
threshold(invDFTcvt, invDFTcvt, 80, 255, CV_THRESH_BINARY);
invDFTcvt.copyTo(invDFTcvtSave);
std::vector<cv::Rect> gDetectedRect = fineMinAreaRect(invDFTcvt,imOrg);/// 最小外接矩形
string CAR0 = "0";
string CAR1 = "1";
float prob0 = 0;
float prob1 = 0;
int car0 = 10000;
int car1 = 10000;
for(int i = 0;i<gDetectedRect.size();i++)
{
Mat imageToClassify = imOrg(gDetectedRect[i]);
std::cout<<"imageToClassify.cols "<<imageToClassify.cols<<" imageToClassify.rows "<<imageToClassify.rows<<std::endl;
cv::imshow("imageToClassify",imageToClassify);
cv::waitKey(10);
CHECK(!imageToClassify.empty()) << "Unable to decode image ";
std::vector<Prediction> predictions = classifier.Classify(imageToClassify);//// how????
/// Print the top N predictions. ///
/*
for (size_t i = 0; i < predictions.size(); ++i)
{
Prediction p = predictions[i];
std::cout << std::fixed << std::setprecision(4) << p.second << " - \""
<< p.first << "\"" << std::endl;
if((p.first == CAR0)&&(p.second>prob0))
{
car0 = i;
prob0 = p.second;
}
if((p.first == CAR1)&&(p.second>prob1))
{
car1 = i;
prob1 = p.second;
}
}
*/
Prediction p = predictions[0];
if((p.first == CAR0)&&(p.second>prob0))
{
car0 = i;
prob0 = p.second;
}
if((p.first == CAR1)&&(p.second>prob1))
{
car1 = i;
prob1 = p.second;
}
rectangle(imOrg,gDetectedRect[i],Scalar(255,155,255),1,8,0);
}
if(prob0>0.2&&car0<10000)
{
if(car0==car1)
{
}
else //if(prob0>prob1)
{
sprintf(car0Tex,"car 0: %f",prob0);
rectangle(imOrg,gDetectedRect[car0],Scalar(0,0,255),3,8,0);
putText(imOrg,car0Tex,Point(gDetectedRect[car0].x,gDetectedRect[car0].y),FONT_HERSHEY_SIMPLEX,1,Scalar(0,0,255),4,8);//在图片上写文字
}
//else
//{}
}
if(prob1>0.2&&car1<10000)
{
if(car0==car1)
{
}
else //if(prob1>prob0)
{
sprintf(car1Tex,"car 1: %f",prob1);
rectangle(imOrg,gDetectedRect[car1],Scalar(255,0,0),3,8,0);
putText(imOrg,car1Tex,Point(gDetectedRect[car1].x,gDetectedRect[car1].y),FONT_HERSHEY_SIMPLEX,1,Scalar(255,0,0),4,8);//在图片上写文字
}
//else
//{}
}
gettimeofday(&tv1,NULL);
int endTime = int(tv1.tv_sec * 1000 + tv1.tv_usec / 1000);
std::cout<<"endTime - startTime: "<<endTime - startTime<<" ms"<<std::endl;
sprintf(Tex,"Fps: %d",int(1000/(endTime - startTime +1)));
putText(imOrg,Tex,Point(50,50),FONT_HERSHEY_SIMPLEX,1,Scalar(255,23,0),4,8);//在图片上写文字
writer.write(imOrg);
cout<<imOrg.cols<<" "<<imOrg.rows<<endl;
imshow("org_image",imOrg);
waitKey(3);
}
return 0;
}