-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
397 lines (355 loc) · 11.7 KB
/
main.cpp
File metadata and controls
397 lines (355 loc) · 11.7 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Filter Data Set Kit
* yinsua -- 20170609
*/
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <stdio.h>
#include <assert.h>
#include <fstream>
#include <math.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/contrib/contrib.hpp>
#include "main.hpp"
using namespace std;
using namespace cv;
typedef struct ImageData{
const int eachReadNum = 600;
vector<Mat> currentGroupImage;
Mat cellImage;
int colsNum = 30;
int gap = 5;
int read_Offset = 0;
}_imageData;
_imageData GImageData;
typedef struct ImagesFileName{
vector<string> currentImageName;
map<string,Mat> tempBuf;
vector<string> displayContent;
}_imagesFileName;
_imagesFileName GImageName;
typedef struct SaveItem{
vector<int> countEachNum;
int currentIterNum;
}_saveItem;
_saveItem GSaveItem;
int
getOrderByMousePoint(int x,int y){
assert(x>=0 && y>=0);
int returnValue = -1;
int row=0,col=0;
row = y%(GImageData.cellImage.rows+GImageData.gap) <= GImageData.cellImage.rows
? y/(GImageData.cellImage.rows+GImageData.gap)
: -1
;
col = x%(GImageData.cellImage.cols+GImageData.gap) <= GImageData.cellImage.cols
? x/(GImageData.cellImage.cols+GImageData.gap)
: -1
;
if(row >= 0 && col >= 0){
returnValue = GImageData.colsNum*row+col;
#if 0
cout<< "row : " <<row<< " \t"
<< "col : " <<col<< " \t"
<< "Order : "<<returnValue<<endl;
#endif
}
else cout<<"Error Mouse Point,Get No Image!"<<endl;
return returnValue;
}
void
on_mouse(int event,int x,int y,int flags,void *ustc)
{
static Point pre_pt ;//初始坐标
static Point cur_pt ;//实时坐标
char temp[16];
int win = *(int*)ustc;
if (event == CV_EVENT_LBUTTONDOWN)
{
// Currently only handles win == 1
if(win != 1) return;
sprintf(temp,"(%d,%d)",x,y);
pre_pt = Point(x,y);
int order = getOrderByMousePoint(x,y);
int maxNum = win==1 ? GImageData.currentGroupImage.size() : GImageName.tempBuf.size();
if(order>=0 && order< maxNum){
string fileName_Order = GImageName.currentImageName.at(order);
if(GImageName.tempBuf.find(fileName_Order) != GImageName.tempBuf.end()){
return;
}
Mat &imgData_Order = GImageData.currentGroupImage.at(order);
GImageName.tempBuf[fileName_Order] = imgData_Order.clone();
#if 0
vector<Mat>::iterator pos = GImageData.currentGroupImage.begin()+order;
GImageData.currentGroupImage.erase(pos);
#else
imgData_Order = Mat(imgData_Order.size(),imgData_Order.depth(),Scalar::all(255));
#endif
}
}
else if (event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
{
sprintf(temp,"(%d,%d)",x,y);
cur_pt = Point(x,y);
//cout<<"X: "<<x<<"\t Y: "<<y<<endl;
}
else if (event == CV_EVENT_RBUTTONDOWN){
sprintf(temp,"(%d,%d)",x,y);
pre_pt = Point(x,y);
int order = getOrderByMousePoint(x,y);
int maxNum = win==1 ? GImageData.currentGroupImage.size() : GImageName.tempBuf.size();
if(order>=0 && order< maxNum){
if(win == 1){
string fileName_Order = GImageName.currentImageName.at(order);
if(GImageName.tempBuf.find(fileName_Order) != GImageName.tempBuf.end()){
// resume image from selected
GImageData.currentGroupImage.at(order) = GImageName.tempBuf[fileName_Order].clone();
GImageName.tempBuf.erase(fileName_Order);
}
}
else if(win == 2){
string selected = GImageName.displayContent.at(order);
vector<string>::iterator it = find(
GImageName.currentImageName.begin(),
GImageName.currentImageName.end(),
selected
);
int order1 = it-GImageName.currentImageName.begin();
if(order1 >=0 && order1<GImageName.currentImageName.size()){
GImageData.currentGroupImage.at(order1) = GImageName.tempBuf[selected];
GImageName.tempBuf.erase(selected);
}
else cerr<<order1<<endl;
}
}
}
}
Mat
groupEachImage(string win_name,vector<Mat> src){
if(src.empty()) {
//cerr<<"Image Group Empty!"<<endl;
destroyWindow(win_name);
return Mat();
}
// TODO: Only support Gray Image
assert(src.back().channels()==1);
int gap = GImageData.gap, colsNum = GImageData.colsNum;
Mat totalImage = Mat(
(src.size()/colsNum+1)*(src.back().rows+gap)-gap,
colsNum*(src.back().cols+gap)-gap,
CV_8UC1,
Scalar::all(255)
);
int currentCols=0;
int currentRows=0;
int num = 0;
for( auto i : src){
Rect roi(num*(i.cols+gap),currentRows*(i.rows+gap),i.cols,i.rows);
Mat roiImage = totalImage(roi);
i.copyTo(roiImage);
num++;
if(num%colsNum==0){
currentRows++;
num=0;
}
}
imshow(win_name,totalImage);
return totalImage;
}
vector<Mat>
getData4Map(map<string,Mat> data){
vector<Mat> temp;
vector<string> temp1;
for( auto i : data ){
temp.push_back(i.second);
temp1.push_back(i.first);
}
GImageName.displayContent=temp1;
return temp;
}
vector<string>
read_Filename(string path){
assert(path.size()>2);
Directory dir;
// TODO : if the file of in <path> dir don't change, so the vector of files is same
vector<string> filenames = dir.GetListFiles(path);
return filenames;
}
vector<Mat>
read_Data(string path,vector<string> filenames){
assert(!filenames.empty());
GImageName.currentImageName = filenames;
vector<Mat> imgData;
if(path[path.size()-2] != '/'){
path += "/";
}
for ( auto i : filenames ){
Mat temp=imread(path+i,CV_LOAD_IMAGE_GRAYSCALE);
imgData.push_back(temp);
}
assert(!imgData.back().empty());
return imgData;
}
void
write_saveItem(int num,vector<int> countNum,string filename = _FILE_NAME){
FileStorage fs(filename, FileStorage::WRITE);
fs << "IterNum" << num;
fs << "CountNum" << countNum;
fs.release();
}
void
read_saveItem(string filename = _FILE_NAME){
int iterNum=0;
vector<int> countNum(10,0);
FileStorage fs;
fs.open(filename, FileStorage::READ);
if(fs.isOpened()){
fs["IterNum"] >> iterNum;
vector<int> temp;
fs["CountNum"] >> temp;
if(temp.empty()){
write_saveItem(iterNum,countNum);
}
else{
countNum = temp;
}
fs.release();
}
else{
write_saveItem(iterNum,countNum);
}
GSaveItem.currentIterNum = iterNum;
GSaveItem.countEachNum = countNum;
}
void
keyValueHandle(string path,int key){
// combine two file shell (cover dst file) :
// cat src1.txt src2.txt > dst.txt
// combine two file shell (add to end of dst file) :
// cat src1.txt src2.txt >> dst.txt
if(key == -1) return;
static vector<string> tempSave;
map<int,string> numArray = {
{1114032,"0"},{1114033,"1"},
{1114034,"2"},{1114035,"3"},
{1114036,"4"},{1114037,"5"},
{1114038,"6"},{1114039,"7"},
{1114040,"8"},{1114041,"9"}
};
if(numArray.find(key) != numArray.end()){
//cout<<"Key Value : "<<numArray[key]<<endl;
for( auto i : GImageName.tempBuf){
string temp = i.first + " " + numArray[key] + "\n";
tempSave.push_back(temp);
}
int num = (int)(numArray[key][0])-48;
GSaveItem.countEachNum.at(num) += GImageName.tempBuf.size();
GImageName.tempBuf.clear();
// print
{
vector<int> x = GSaveItem.countEachNum;
printf("Count : \n\t0: %d\n\t1: %d\n\t2: %d\n\t3: %d\n\t4: %d\n\t5: %d\n\t6: %d\n\t7: %d\n\t8: %d\n\t9: %d\n",
x.at(0),x.at(1),x.at(2),x.at(3),
x.at(4),x.at(5),x.at(6),x.at(7),
x.at(8),x.at(9)
);
}
}
else if(key == _KEY_VALUE_ESC/*<ESC>*/){
// 1. save image name and the num of image what is called
ofstream out(path,ios::out | ios::app);
if(out.is_open()){
for( auto i : tempSave){
out << i;
}
tempSave.clear();
out.close();
}
else{
cerr<<"Output TXT Open Failed!"<<endl;
exit(1);
}
// 2. save count num and iter num
GSaveItem.currentIterNum++;
write_saveItem(GSaveItem.currentIterNum,GSaveItem.countEachNum);
}
else cerr<<"Unknow Key!"<<endl;
}
int
main(int argc,char** argv){
if (argc != 3) {
cerr << "Usage: " << argv[0]
<< " <image/data/set/path>"
<< " <output/txt/file>" << std::endl;
return 1;
}
string usageInfo = "Key Map : \n"
"\t<ESC> : Next Page \n"
"\t<q> : Exit \n"
"\t[0~9] : Selected Image what is called \n"
;
cout<<usageInfo<<endl;
string imgsPath = argv[1];
string outputTxt = argv[2];
vector<string> filenames = read_Filename(imgsPath);
read_saveItem();
string win_name = "imgVector (" +
to_string(GSaveItem.currentIterNum+1) +
"/" +
to_string(filenames.size() / GImageData.eachReadNum + 1) +
")";
string selectedImageWinName = "Selected Image";
const int a=1,b=2;
vector<string>::iterator it;
it = filenames.begin();
it += GSaveItem.currentIterNum*GImageData.eachReadNum;
while(1)
{
vector<string> temp;
if(filenames.end()-it <= 0){
cout<<"IterNum : "<<GSaveItem.currentIterNum<<endl;
cout<<"Finish!"<<endl;
break;
}
else if(filenames.end()-it<GImageData.eachReadNum){
temp.assign(it,filenames.end());
it = filenames.end();
}
else{
temp.assign(it,it+GImageData.eachReadNum);
it += GImageData.eachReadNum;
}
vector<Mat> imgVector = read_Data(imgsPath,temp);
assert(!imgVector.empty());
GImageData.currentGroupImage=imgVector;
GImageData.cellImage=imgVector.back();
win_name = "imgVector (" +
to_string(GSaveItem.currentIterNum+1) +
"/" +
to_string(filenames.size() / GImageData.eachReadNum + 1) +
")";
destroyAllWindows();
while (1){
setMouseCallback(win_name,on_mouse,(void*)&a);
setMouseCallback(selectedImageWinName,on_mouse,(void*)&b);
groupEachImage(win_name,GImageData.currentGroupImage);
groupEachImage(selectedImageWinName,getData4Map(GImageName.tempBuf));
int k = waitKey(33);
if(k == _KEY_VALUE_q/*q*/) exit(1);
else{
keyValueHandle(outputTxt,k);
if(k == _KEY_VALUE_ESC/*<ESC>*/) break;
}
}
}
// clean up
{
vector<int> temp(10,0);
write_saveItem(0,temp);
}
return 0;
}