/
mainTextureFilters.cpp
1077 lines (905 loc) · 32.1 KB
/
mainTextureFilters.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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <dirent.h>
#include <unistd.h> // For sleep function
#include <iostream>
#include <fstream>
#include "set"
#include "stack"
#include "stdio.h"
#include "string"
#include <vector>
#include "queue"
#include "map"
#include "math.h"
#include "time.h"
#include <algorithm>
using namespace std;
#include "opencv2/opencv.hpp"
#include <typeinfo>
#include <sstream>
#include <chrono> // time measurement
#include <thread> // time measurement
#include <boost/math/special_functions/round.hpp>
#include <boost/filesystem.hpp>
#include <boost/range/algorithm.hpp>
using namespace boost::filesystem;
using namespace cv;
typedef unsigned int uint;
const float PI = 3.1415;
typedef vector<float> m1;
typedef vector<m1> m2;
typedef vector<m2> m3;
typedef vector<Mat> mH1;
typedef vector<mH1> mH2;
//response = np.exp(-x ** 2 / (2. * sigma ** 2))
void func1(float *response, float *lengths, float sigma, int size) {
for(int i = 0; i < size; i++)
response[i] = exp(- lengths[i] * lengths[i] / (2 * sigma * sigma));
}
//response = -response * x
void func2(float *response, float *lengths, int size) {
for(int i = 0; i < size; i++)
response[i] = -response[i] * lengths[i];
}
//response = response * (x ** 2 - sigma ** 2)
void func3(float *response, float *lengths, float sigma, int size) {
for(int i = 0; i < size; i++)
response[i] = response[i] * (lengths[i] * lengths[i] - sigma * sigma);
}
// response /= np.abs(response).sum()
void normalize(float *response, int size) {
float summ = 0;
for(int i = 0; i < size; i++)
summ += std::abs(response[i]);
for(int i = 0; i < size; i++)
response[i] /= summ;
}
void make_gaussian_filter(float *response, float *lengths, float sigma, int size, int order=0) {
assert(order <= 2);//, "Only orders up to 2 are supported"
// compute unnormalized Gaussian response
func1(response, lengths, sigma, size);
if (order == 1)
func2(response, lengths, size);
else if (order == 2)
func3(response, lengths, sigma, size);
normalize(response, size);
}
void getX(float *xCoords, Point2f* pts, int size) {
for(int i = 0; i < size; i++)
xCoords[i] = pts[i].x;
}
void getY(float *yCoords, Point2f* pts, int size) {
for(int i = 0; i < size; i++)
yCoords[i] = pts[i].y;
}
void multiplyArrays(float *gx, float *gy, float *response, int size) {
for(int i = 0; i < size; i++)
response[i] = gx[i] * gy[i];
}
void makeFilter(float scale, int phasey, Point2f* pts, float *response, int size) {
float xCoords[size];
float yCoords[size];
getX(xCoords, pts, size);
getY(yCoords, pts, size);
float gx[size];
float gy[size];
make_gaussian_filter(gx, xCoords, 3 * scale, size);
make_gaussian_filter(gy, yCoords, scale, size, phasey);
multiplyArrays(gx, gy, response, size);
normalize(response, size);
}
void createPointsArray(Point2f *pointsArray, int radius) {
int index = 0;
for(int x = -radius; x <= radius; x++)
for(int y = -radius; y <= radius; y++)
{
pointsArray[index] = Point2f(x,y);
index++;
}
}
void rotatePoints(float s, float c, Point2f *pointsArray, Point2f *rotatedPointsArray, int size) {
for(int i = 0; i < size; i++)
{
rotatedPointsArray[i].x = c * pointsArray[i].x - s * pointsArray[i].y;
rotatedPointsArray[i].y = s * pointsArray[i].x - c * pointsArray[i].y;
}
}
void computeLength(Point2f *pointsArray, float *length, int size) {
for(int i = 0; i < size; i++)
length[i] = sqrt(pointsArray[i].x * pointsArray[i].x + pointsArray[i].y * pointsArray[i].y);
}
void toMat(float *edgeThis, Mat &edgeThisMat, int support) {
edgeThisMat = Mat::zeros(support, support, CV_32FC1);
float* nextPts = (float*)edgeThisMat.data;
for(int i = 0; i < support * support; i++)
{
nextPts[i] = edgeThis[i];
}
}
void makeRFSfilters(vector<Mat>& edge, vector<Mat >& bar, vector<Mat>& rot, vector<float> &sigmas, int n_orientations=6, int radius=24) {
int support = 2 * radius + 1;
int size = support * support;
Point2f orgpts[size];
createPointsArray(orgpts, radius);
for(uint sigmaIndex = 0; sigmaIndex < sigmas.size(); sigmaIndex++)
for(int orient = 0; orient < n_orientations; orient++)
{
float sigma = sigmas[sigmaIndex];
//Not 2pi as filters have symmetry
float angle = PI * orient / n_orientations;
float c = cos(angle);
float s = sin(angle);
Point2f rotpts[size];
rotatePoints(s, c, orgpts, rotpts, size);
float edgeThis[size];
makeFilter(sigma, 1, rotpts, edgeThis, size);
float barThis[size];
makeFilter(sigma, 2, rotpts, barThis, size);
Mat edgeThisMat;
Mat barThisMat;
toMat(edgeThis, edgeThisMat, support);
toMat(barThis, barThisMat, support);
edge.push_back(edgeThisMat);
bar.push_back(barThisMat);
}
float length[size];
computeLength(orgpts, length, size);
float rotThis1[size];
float rotThis2[size];
make_gaussian_filter(rotThis1, length, 10, size);
make_gaussian_filter(rotThis2, length, 10, size, 2);
Mat rotThis1Mat;
Mat rotThis2Mat;
toMat(rotThis1, rotThis1Mat, support);
toMat(rotThis2, rotThis2Mat, support);
rot.push_back(rotThis1Mat);
rot.push_back(rotThis2Mat);
}
void apply_filterbank(Mat &img, vector<vector<Mat> >filterbank, vector<vector<Mat> > &response, int n_sigmas, int n_orientations) {
response.resize(3);
vector<Mat>& edges = filterbank[0];
vector<Mat>& bar = filterbank[1];
vector<Mat>& rot = filterbank[2];
// Apply Edge Filters //
int i = 0;
for(int sigmaIndex = 0; sigmaIndex < n_sigmas; sigmaIndex++)
{
Mat newMat = Mat::zeros(img.rows, img.cols, img.type());
for(int orient = 0; orient < n_orientations; orient++)
{
Mat dst;
filter2D(img, dst, -1, edges[i], Point( -1, -1 ), 0, BORDER_DEFAULT );
newMat = cv::max(dst, newMat);
i++;
}
Mat newMatUchar;
newMat = cv::abs(newMat);
newMat.convertTo(newMatUchar, CV_8UC1);
response[0].push_back(newMatUchar);
}
// Apply Bar Filters //
i = 0;
for(int sigmaIndex = 0; sigmaIndex < n_sigmas; sigmaIndex++)
{
Mat newMat = Mat::zeros(img.rows, img.cols, img.type());
for(int orient = 0; orient < n_orientations; orient++)
{
Mat dst;
filter2D(img, dst, -1 , bar[i], Point( -1, -1 ), 0, BORDER_DEFAULT );
newMat = max(dst, newMat);
i++;
}
Mat newMatUchar;
newMat = cv::abs(newMat);
newMat.convertTo(newMatUchar, CV_8UC1);
response[1].push_back(newMatUchar);
}
// Apply Gaussian and LoG Filters //
for(uint i = 0; i < 2; i++)
{
Mat newMat = Mat::zeros(img.rows, img.cols, img.type());
Mat dst;
filter2D(img, dst, -1 , rot[i], Point( -1, -1 ), 0, BORDER_DEFAULT );
newMat = max(dst, newMat);
Mat newMatUchar;
newMat = cv::abs(newMat);
newMat.convertTo(newMatUchar, CV_8UC1);
response[2].push_back(newMatUchar);
}
cout <<"leaving apply filterbank" << endl;
}
// Aggregate Images
void aggregateImg(uint num, double alpha, Mat &aggImg, Mat input) {
double beta = 1.0 - alpha;
if (num == 0) {
// cout << "initialising aggImg" << endl;
input.copyTo(aggImg);
} else {
cout << "Adding new image, num = " << num << endl;
addWeighted(aggImg, beta , input, alpha, 0.0, aggImg, -1);
}
}
// Apply kmeans, rtn centers
Mat applyKmeans(Mat samples, int clusterCount){
// Mat labels = Mat(20,1,CV_32S);
// vector<int> inputLabels = {0,0,10,10,20,20,30,30,40,40,50,50,60,60,70,70,80,80,90,90,99,99};
//
// for(int k =0;k<20;k++){
// labels.at<float>(k,0) = inputLabels[k];
// cout << "this is the labels: "<< k << " : " << labels.at<float>(k,0) << endl;
// }
Mat labels;
int attempts = 5;
Mat centers;
// Apply KMeans
kmeans(samples, clusterCount, labels, TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 10000, 0.0001), attempts, KMEANS_PP_CENTERS, centers);
return centers;
}
// convert 2d Mat to 1Col float mat, Pass to kmeans
Mat createSamples(Mat input, int cluster){
input.convertTo(input, CV_32F);
Mat samples(input.rows * input.cols, 1, CV_32F);
// Copy across input image
for (int y = 0; y < input.rows; y++) {
for (int x = 0; x < input.cols; x++) {
samples.at<float>(y, x) = input.at<float>(y, x);
}
}
return applyKmeans(samples, cluster);
}
// Print Texton Dictionary
void printTexDict(vector<float> textonDict){
int classesSize = textonDict.size();
cout << "This is the full texton Dictionary size:" << classesSize << endl;
for(int i = 0;i < classesSize;i++){
cout << i << ":" << textonDict.at(i)<< endl;
}
}
void printModelsInner(vector<float> v, int count){
if(v.size()!=0){
cout << "Model:" << count << "\nIt's size is: " << v.size() << endl;
for(int i = 0; i < v.size(); i++){
cout << v[i] << " ";
}
cout << "\n";
}
}
void printModels(vector <vector<float> > v){
cout << "Below are the model cluster centers, vector size: " << v.size() << endl;
for(int b =0;b<v.size();b++)
printModelsInner(v[b], b);
}
// Check that file in dir is an accepted img type
bool hasEnding(std::string const &fullString, std::string const &ending) {
if (fullString.length() >= ending.length()) {
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
} else {
return false;
}
}
// Load and Equalise Imgs from path, rtn img float
bool loadImg(Mat& img){
if(!img.data){
return false;
}
equalizeHist(img, img);
img.convertTo(img, CV_32FC1);
return true;
}
// Round to 3dp
//inner loop
void roundModelInner(vector<float>& v){
for(int j=0;j<v.size();j++){
v[j] = floorf(v[j]*1000)/1000;
}
}
// outerloop
void roundModel(vector<vector<float> >& v){
for(int i=0;i<v.size();i++){
roundModelInner(v[i]);
}
}
// converts input 1d Mat to vector
void matToVec(vector<float> &textonDict, Mat centers){
for(int j = 0;j < centers.rows;j++){
textonDict.push_back(centers.at<float>(0, j));
}
}
void drawingResponceInner(vector<Mat>& response, vector<vector<float> > &models, int &counter, int flag, Mat &aggImg, uint type, double& alpha){
for(uint imageIndex = 0; imageIndex < response.size(); imageIndex++){
if(flag){
// Aggregate for Texton Dictionary
aggregateImg(counter, alpha, aggImg, response[imageIndex]);
alpha *= 0.5;
}else {
cout << "\ndrawing Response model:" << type << " : " << imageIndex << endl;
// cluster and save to models
Mat clusters = createSamples(response[imageIndex], 10);
// multiply type*3 then add imageIndex to access all 8 vector locations
// matToVec(models[(type*3)+imageIndex], clusters);
matToVec(models[counter], clusters);
}
}
}
// produce Agg image from responses
void drawingResponce(vector<vector<Mat> > &response, vector<vector<float> > &models, int &counter, int flag, Mat &aggImg){
double alpha = 0.5;
for(uint type = 0; type < response.size(); type++){
drawingResponceInner(response[type], models, counter, flag, aggImg, type, alpha);
}
}
void testImgModel(vector<vector<Mat> > &response, vector<float> &model){
int numOfClusters = 10;
for(int i = 0; i < response.size(); i++){
for(int j = 0; j < response[i].size(); j++){
Mat clusters = createSamples(response[i][j], numOfClusters);
matToVec(model, clusters);
}
}
cout << "This is sthe size produced: " << model.size() << endl;
boost::sort(model);
cout << "This is sthe size produced: " << model.size() << endl;
cout << "This is the model: \n";
for(int q =0;q<model.size();q++){
cout << model[q] << " ,";
}
cout << "\n\n";
}
// Generate models from training images
void createModels(vector<vector<Mat> >& response, vector<vector<float> >& models, int counter){
Mat aggImg;
drawingResponce(response, models, counter, 0, aggImg);
}
// Generate Texton Dictionary from all imgs in sub dirs
void createTexDic(mH2 filterbank, vector<string> classes , m3& models, int n_sigmas, int n_orientations, vector<float>& textonDict, string type){
string extTypes[] = {".jpg", ".png", ".bmp"};
int classesSize = classes.size();
int doCount = 0;
do{
string classnme = classes.at(doCount);
stringstream dss;
string dirtmp = "../../../TEST_IMAGES/kth-tips/";
dss << dirtmp;
dss << classnme;
dss << "/";
dss << type;
string dirNme = dss.str();
cout << "this is the dir name: " << dirNme << endl;
DIR *dir;
dir = opendir(dirNme.c_str());
string imgName;
struct dirent *ent;
int counter = 0;
if(dir != NULL){
Mat aggImg;
vector<vector<Mat> > response;
// Will count the number of images in each class
int imagecounter =0;
while ((ent = readdir(dir)) != NULL) {
imgName = ent->d_name;
if (hasEnding(imgName, ".png")) {
cout << "correct extension: " << imgName << endl;
// Sort out string Stream
std::stringstream ss;
ss << dirNme << imgName;
std::string imgpath = ss.str();
// Load image
Mat img = imread(imgpath, CV_LOAD_IMAGE_GRAYSCALE);
if(loadImg(img)){
cout << "This is imgpath:" << imgpath << " img.size()" << img.size() << endl;
// Apply and store in response
apply_filterbank(img, filterbank, response, n_sigmas, n_orientations);
// If type is test cluster each image
if(type.compare("test/")==0){
createModels(response, models[doCount], imagecounter);
response.clear();
}
}
imagecounter++;
} else{
cout << "incorrect extension: " << imgName << endl;
}
}
// If type is train aggregate and generate clusters
if(type.compare("train/")==0){
// Aggregate fitler responses from the same classes
drawingResponce(response, models[doCount], counter, 1,aggImg);
Mat centers = createSamples(aggImg, 10);
//cluster aggregated response
cout << "These are the clusters: " << centers << endl;
// Store kmeans cluster centers(textons) in vector referenced from main//
// the number of values already in array
matToVec(textonDict, centers);
}
}
doCount ++;
}while(doCount < classesSize);
}
void roundTex(vector<float>& tex){
for(int i=0;i<tex.size();i++){
tex[i] = floorf((tex[i])*1000)/1000;
//tex[i] = round(tex[i]);
}
}
void removeDups(vector<float>& tex){
set<float> v;
unsigned size = tex.size();
for(unsigned i = 0;i<size;i++)
v.insert(tex[i]);
// Clear tex vector
tex.clear();
// Insert sorted values
tex.assign(v.begin(), v.end());
}
int maxHistVal(Mat in){
int maxVal = 0;
// Started at 1 to avoid first very high value..
for(int i = 1; i < in.rows;i++){
for(int j = 0; j < in.cols;j++){
if(in.at<float>(i,j)>maxVal){
maxVal = in.at<float>(i,j);
}
}
}
return maxVal;
}
// Calculate and return histogram image
Mat showHist(Mat& inputHist, int histBins){
// Create variables
int hist_w = 512; int hist_h = 400;
int bin_w = cvRound( (double) hist_w/histBins );
int maxVal = maxHistVal(inputHist);
// Calibrate the maximum histogram value at 80% of window height
double scaleFactor = ((hist_h*0.8)/maxVal);
Mat m( hist_h, hist_w, CV_8UC1, Scalar( 0) );
int countHist = 0;
cout << "\n\n";
for( int i = 1; i < histBins; i++ ){
// Count sum of histogram values
countHist += inputHist.at<float>(i);
cout << "This is value: " << i << " value: " << inputHist.at<float>(i) << endl;
// Draw rectangle representative of hist values in output image
rectangle( m, Point( bin_w*(i), hist_h) ,
Point( (bin_w*(i))+bin_w, hist_h - (inputHist.at<float>(i) * scaleFactor)),
Scalar( 255, 255, 255),-1, 8);
}
cout << "\n\n";
return m;
}
void createHist(Mat& in, Mat& out, int histSize, const float* histRange, bool uniform){
bool accumulate = false;
// Compute the histograms:
cout << "CreateHist: This is input sze: " << in.size() << " And out size: " << out.size() << endl;
calcHist( &in, 1, 0, Mat(), out, 1, &histSize, &histRange, uniform, accumulate );
}
// Takes in Vector<float> and converts to Mat<float>
void textToMat(Mat& tmp, vector<float>& texDict){
cout << "\n\n\n\nThis is textToMAt.size(): " << texDict.size() << "\n";
cout << "This is MAt.size(): " << tmp.size() << "\n\n\n";
for(int i = 0; i < texDict.size();i++){
tmp.at<float>(i,0) = texDict[i];
}
}
void textonFind(vector<float> txtDict, float& m){
float distance = 0.0, nearest = 0.0;
distance = abs(txtDict[0] - m);
nearest = txtDict[0];
for(int i = 0; i < txtDict.size(); i++){
if(abs(txtDict[i] - m) < distance){
nearest = txtDict[i];
distance = abs(txtDict[i] - m);
}
}
m = nearest;
}
void textonModel(vector<float> txtDict, vector<float>& models){
for(int j = 0; j < models.size();j++){
textonFind(txtDict, models[j]);
}
}
// Create bins for each textonDictionary Value
void binLimits(vector<float> texDict, float* bins, int size){
cout << "inside binLimits" << endl;
bins[0] = 0;
for(int i = 1;i <= size;i++){
bins[i] = (texDict[i-1] + 0.001);
cout << "texDict: " << i << " "<< texDict[i-1] << " becomes: " << bins[i] << endl;
}
bins[size+1] = 255;
}
void savetxtDict(vector<float> dict, float* binArray, int binNum){
FileStorage fs("txtDict.xml", FileStorage::WRITE);
// Save texton Dictionary
cout << "saving texton dictionary" << endl;
fs << "TextonDictionary" << "[";
for(int i =0;i<dict.size();i++)
fs << dict[i];
fs << "]";
// Save Corresponding Bin Array
cout << "saving Bin limits" << endl;
fs << "binArray" << "[";
for(int j=0;j<binNum;j++){
fs << binArray[j];
}
fs << "]";
fs.release();
}
void loadTex(vector<float>& out){
FileStorage fs("txtDict.xml", FileStorage::READ);
FileNode n = fs["TextonDictionary"];
if(n.type() != FileNode::SEQ){
cout << "incorrect filetype: " << n.type() << endl;
fs.release();
return;
}
FileNodeIterator it = n.begin(), it_end = n.end();
int cnt =0;
for(;it != it_end;++it){
out.push_back((float)*it);
cnt++;
}
cout << "finished reading Textons..\n\n";
fs.release();
}
void loadBins(float* out){
FileStorage fs("txtDict.xml", FileStorage::READ);
FileNode n = fs["binArray"];
if(n.type() != FileNode::SEQ){
cout << "incorrect filetype: " << n.type() << endl;
fs.release();
return;
}
FileNodeIterator it = n.begin(), it_end = n.end();
int cnt =0;
for(;it != it_end;++it){
out[cnt] = (float)*it;
cnt++;
}
cout << "finished reading Bins..\n\n";
fs.release();
}
void loadHist(mH2& hist){
FileStorage fs("test123.xml", FileStorage::READ);
FileNode n = fs["ModelHistograms"];
// Loop through Classes
for(int i=0;i<n.size();i++){
stringstream ss;
ss << "Class_";
ss << i;
string a = ss.str();
FileNode n1 = n[a];
// Loop through Each classes Models
for(int j = 0; j < n1.size(); j++){
stringstream ss1;
ss1 << "Model_";
ss1 << j;
string b = ss1.str();
FileNode n2 = n1[b];
// Save stored Mat to mask
FileNodeIterator it = n2.begin(), it_end = n2.end();
for(;it != it_end;++it){
Mat mask;
(*it) >> hist[i][j];
}
}
}
fs.release();
}
void saveHist(mH2 hist){
cout << "saving histogram." << endl;
FileStorage fs("test123.xml", FileStorage::WRITE);
int size = hist.size();
fs << "ModelHistograms" << "{";
for(int i=0;i<size;i++){
stringstream ss;
string a, b;
a = "Class_";
ss << i;
b = ss.str();
a +=b;
fs << a << "{";
for(int j =0;j<hist[i].size();j++){
stringstream ss1;
string c;
ss1 << "Model_" << j;
c = ss1.str();
fs << c << "[";
fs << hist[i][j];
fs << "]";
}
fs << "}";
}
fs << "}";
fs.release();
}
void makeTexDictionary(mH2 filterbank, vector<string> classes, int n_sigmas, int n_orientations, string type){
vector<float> texDict;
m3 models(4, m2(8, m1(0)));
// Create texton dict and store
createTexDic(filterbank, classes, models, n_sigmas, n_orientations, texDict, type);
// Sort texton Dict and round to 2dp
sort(texDict.begin(), texDict.end());
roundTex(texDict);
removeDups(texDict);
int texDictSize = texDict.size();
float binArray[texDictSize];
binLimits(texDict, binArray, texDictSize);
// Store in local dir, texDict+2 to account for starting 0 and finishing 255
savetxtDict(texDict, binArray, texDictSize+2);
}
void displayTexDict(vector<float> texDict){
// Convert array to Mat
int listLen = texDict.size();
Mat tmp(listLen,1,CV_32FC1);
textToMat(tmp, texDict);
const string windowname1 = "texton Distribution";
// Display Texton Histogram
int histSize = 20;
float range[] = { 0, 255 };
bool uniform = true;
Mat histImage;
createHist(tmp, histImage, histSize ,range, uniform);
Mat histImg = showHist(histImage, histSize);
namedWindow(windowname1, CV_WINDOW_AUTOSIZE);
imshow(windowname1,histImg);
waitKey(0);
destroyWindow(windowname1);
cout << "Leaving DisplayTexDict. " << endl;
}
void generateModels(mH2 filterbank, vector<float> textonDictionary, const float* binArray, vector<string> classes, int n_sigmas, int n_orientations, string type){
m3 models(4, m2(8, m1(0)));
mH2 modelHist(10, mH1(10, Mat::zeros(80,1,CV_32FC1)));
// Return clusters(in models) from filter responses to images in test dirs
createTexDic(filterbank, classes, models, n_sigmas, n_orientations, textonDictionary, type);
// loop through different classes
for(int a = 0; a < models.size(); a++){
// loop through different models
for(int b = 0; b < models[a].size(); b++){
if(models[a][b].size()!=0){
cout << "starting this loop: " << a << " mini loop number: " << b << endl;
cout << "this is the texton dictionary size: " << textonDictionary.size() << endl;
cout << "TYhis is the model[a][b].size(): " << models[a][b].size() << "\n\n";
textonModel(textonDictionary, models[a][b]);
// Convert array to Mat, large tmp.rows prevents overrun
Mat tmp = Mat::zeros(500, 1, CV_32FC1);
textToMat(tmp, models[a][b]);
// Generate texton histogram and return Mat image and display
bool uniform = false;
createHist(tmp, modelHist[a][b],textonDictionary.size(), binArray, uniform);
cout << "This is the modelSize after: " << modelHist[a][b].size() << endl;
// Mat histImg = showHist(modelHist[a][b], texDictSize);
}
}
}
saveHist(modelHist);
}
void createFilterbank(mH2 &filterbank, int &n_sigmas, int &n_orientations){
vector<float> sigmas;
sigmas.push_back(1);
sigmas.push_back(2);
sigmas.push_back(4);
n_sigmas = sigmas.size();
n_orientations = 6;
vector<Mat > edge, bar, rot;
makeRFSfilters(edge, bar, rot, sigmas, n_orientations);
// Store created filters in fitlerbank 2d vector<Mat>
filterbank.push_back(edge);
filterbank.push_back(bar);
filterbank.push_back(rot);
}
void getImages(path p){
try{
vector<string> files;
// Check path exists
if(exists(p)){
if(is_regular_file(p)){
cout << p << " is a regular file, with a size of: " << file_size(p) << endl;
cout << "and it's extension is: " << p.extension().string() << endl;
}else if(is_directory(p)){
int count = 0;
directory_iterator end_itr;
for(directory_iterator itr(p); itr != end_itr; ++itr){
files.push_back(itr -> path().filename().string());
cout << "This is the directory status: " << files[count] << endl;
count ++;
}
}else{
cout << "The path is valid but this is not a regular file." << endl;
}
}else{
cout << "This path is not valid." << endl;
}
}
catch(const filesystem_error& ex){
}
}
void segmentImg(Mat in, vector<Mat>& segImg){
for(int c=0; c < 10; c++){
for(int r=0 ;r < 10; r++){
// Select ROI and copy to tmp Matrix
Mat tmp;
in(Rect(r,c,20,20)).copyTo(tmp);
// Reshape to 1D vector and push to array
segImg.push_back(tmp.reshape(400,1));
}
}
}
int main(){
// Start the window thread(essential for deleting windows)
cvStartWindowThread();
string basePath = "../../../TEST_IMAGES/kth-tips/bread/train/";
path p(basePath);
// getImages(p);
stringstream ss;
ss << basePath;
ss << "52a-scale_2_im_1_col.png";
Mat input1 = imread(ss.str());
vector<Mat> segImgArr(1000,Mat::zeros(1,1300,CV_32FC2));
segmentImg(input1, segImgArr);
cout << "This is the size: " << segImgArr[0].size() << endl;
Mat labels, centers;
int attempts = 5, clustercnt = 10;
kmeans(segImgArr[0], clustercnt, labels, TermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS, 10000, 0.0001), attempts, KMEANS_PP_CENTERS, centers);
cout << "This is the size of the centers: " << centers.size() << endl;
// dirs holding texton and model generating images
string textonclasses[] = {"wood", "cotton", "cork", "bread"};
vector<string> classes (textonclasses, textonclasses + sizeof(textonclasses)/sizeof(textonclasses[0]));
int height = 10;
int width = 10;
int modelH = 4, modelW = 8, modelD =0;
bool modelsGenerated = false;
// Declare resources
const string type[] = {"train/", "test/", "novel/"};
// --------------------- Texton Dictionary Creation ---------------------- //
// If statement to reduce const histRange scope, allowing it to be redeclared
if(false){
int n_sigmas, n_orientations;
mH2 filterbank;
createFilterbank(filterbank, n_sigmas, n_orientations);
cout << "creating texton dictionary" << endl;
// Check for saved Dictionary xml file, generate and save new one if not found
ifstream savedDict("txtDict.xml");
if(!savedDict){
cout << "\n\n\n\nCAlculating Texton Dicationary..\n\n\n";
makeTexDictionary(filterbank, classes, n_sigmas, n_orientations, type[0]);
}
vector<float> textonDictionary;
loadTex(textonDictionary);
ifstream savedMod("test123.xml");
if(!savedMod){
cout << "\n\n\nCAlculating models...\n\n";
float binArray[textonDictionary.size()];
loadBins(binArray);
generateModels(filterbank, textonDictionary, binArray, classes, n_sigmas, n_orientations, type[1]);
}
printTexDict(textonDictionary);
displayTexDict(textonDictionary);
}
bool cont = false;
do{
cout << "\nMenu: Please enter the chosen options number \n"<< endl;
cout << "1: ReCalculate Texton Dictionary" << endl;
cout << "2: ReCalculate Models" << endl;
cout << "3: model testing" << endl;
cout << "4: exit\n" << endl;
string tmp;
cin >> tmp;
if(tmp == "1"){
int n_sigmas, n_orientations;
mH2 filterbank;
createFilterbank(filterbank, n_sigmas, n_orientations);
cout << "\n\n-------------------Regenerating TextonDictionary and Bins--------------------\n\n";
cout << "ReCalculating Texton Dictionary and saving\n\n";
vector<float> textonDictionary;
makeTexDictionary(filterbank, classes, n_sigmas, n_orientations, type[0]);
cont = true;
}
else if(tmp == "2"){
int n_sigmas, n_orientations;
mH2 filterbank;
createFilterbank(filterbank, n_sigmas, n_orientations);
// Measure start time
auto t1 = std::chrono::high_resolution_clock::now();
cout << "\n\n-------------------Regenerating models--------------------\n\n";
// Check for saved Dictionary xml file, generate and save new one if not found
ifstream savedDict("txtDict.xml");
if(!savedDict)
makeTexDictionary(filterbank, classes, n_sigmas, n_orientations, type[1]);
vector<float> textonDictionary;
loadTex(textonDictionary);
float binArray[textonDictionary.size()];
loadBins(binArray);
generateModels(filterbank, textonDictionary, binArray, classes, n_sigmas, n_orientations, type[1]);
// Measure time efficiency
auto t2 = std::chrono::high_resolution_clock::now();
std::cout << "f() took "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count()
<< " milliseconds\n";
cont = true;
modelsGenerated = true;
}
else if(tmp == "3"){
cout << "\n\n-------------------Testing Image Against Models--------------------\n\n";
// Measure start time
// auto t3 = std::chrono::high_resolution_clock::now();
vector<float> textonDictionary;
loadTex(textonDictionary);
float binArray[textonDictionary.size()];
loadBins(binArray);
m3 models(4, m2(8, m1(0)));
mH2 modelHist(10, mH1(10, Mat::zeros(80,1,CV_32FC1)));
loadHist(modelHist);
cout << "\nFirst.. \n\n";
int n_sigmas, n_orientations;
mH2 filterbank;
createFilterbank(filterbank, n_sigmas, n_orientations);
vector<float> testModel;
Mat inputImg = Mat::zeros(200,200,CV_8UC1);
inputImg = imread("../../../TEST_IMAGES/testImage/16a-scale_2_im_8_col.png", CV_LOAD_IMAGE_GRAYSCALE);
if(!inputImg.data){
cout << "unable to load image.." << endl;
return -1;
}
vector<vector<Mat> > response;