-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyApplication.cpp
1006 lines (945 loc) · 36.7 KB
/
MyApplication.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 "Utilities.h"
#include <iostream>
#include <fstream>
#include <list>
#include <experimental/filesystem> // C++-standard header file name
#include <filesystem> // Microsoft-specific implementation header file name
#include <math.h>
using namespace std::experimental::filesystem::v1;
using namespace std;
using namespace cv;
// Sign must be at least 100x100
#define MINIMUM_SIGN_SIDE 100
#define MINIMUM_SIGN_AREA 10000
#define MINIMUM_SIGN_BOUNDARY_LENGTH 400
#define STANDARD_SIGN_WIDTH_AND_HEIGHT 200
// Best match must be 10% better than second best match
#define REQUIRED_RATIO_OF_BEST_TO_SECOND_BEST 1.1
// Located shape must overlap the ground truth by 80% to be considered a match
#define REQUIRED_OVERLAP 0.8
class ObjectAndLocation
{
public:
ObjectAndLocation(string object_name, Point top_left, Point top_right, Point bottom_right, Point bottom_left, Mat object_image);
ObjectAndLocation(FileNode& node);
void write(FileStorage& fs);
void read(FileNode& node);
Mat& getImage();
string getName();
void setName(string new_name);
string getVerticesString();
void DrawObject(Mat* display_image, Scalar& colour);
double getMinimumSideLength();
double getArea();
void getVertice(int index, int& x, int& y);
void setImage(Mat image); // *** Student should add any initialisation (of their images or features; see private data below) they wish into this method.
double compareObjects(ObjectAndLocation* otherObject); // *** Student should write code to compare objects using chosen method.
bool OverlapsWith(ObjectAndLocation* other_object);
private:
string object_name;
Mat image;
vector<Point2i> vertices;
// *** Student can add whatever images or features they need to describe the object.
int No_of_contours;
};
class AnnotatedImages;
class ImageWithObjects
{
friend class AnnotatedImages;
public:
ImageWithObjects(string passed_filename);
ImageWithObjects(FileNode& node);
virtual void LocateAndAddAllObjects(AnnotatedImages& training_images) = 0;
ObjectAndLocation* addObject(string object_name, int top_left_column, int top_left_row, int top_right_column, int top_right_row,
int bottom_right_column, int bottom_right_row, int bottom_left_column, int bottom_left_row, Mat& image);
void write(FileStorage& fs);
void read(FileNode& node);
ObjectAndLocation* getObject(int index);
void extractAndSetObjectImage(ObjectAndLocation *new_object);
string ExtractObjectName(string filenamestr);
void FindBestMatch(ObjectAndLocation* new_object, string& object_name, double& match_value);
protected:
string filename;
Mat image;
vector<ObjectAndLocation> objects;
};
class ImageWithBlueSignObjects : public ImageWithObjects
{
public:
ImageWithBlueSignObjects(string passed_filename);
ImageWithBlueSignObjects(FileNode& node);
void LocateAndAddAllObjects(AnnotatedImages& training_images); // *** Student needs to develop this routine and add in objects using the addObject method
};
class ConfusionMatrix;
class AnnotatedImages
{
public:
AnnotatedImages(string directory_name);
AnnotatedImages();
void addAnnotatedImage(ImageWithObjects &annotated_image);
void write(FileStorage& fs);
void read(FileStorage& fs);
void read(FileNode& node);
void read(string filename);
void LocateAndAddAllObjects(AnnotatedImages& training_images);
void FindBestMatch(ObjectAndLocation* new_object);
Mat getImageOfAllObjects(int break_after = 7);
void CompareObjectsWithGroundTruth(AnnotatedImages& training_images, AnnotatedImages& ground_truth, ConfusionMatrix& results);
ImageWithObjects* getAnnotatedImage(int index);
ImageWithObjects* FindAnnotatedImage(string filename_to_find);
public:
string name;
vector<ImageWithObjects*> annotated_images;
};
class ConfusionMatrix
{
public:
ConfusionMatrix(AnnotatedImages training_images);
void AddMatch(string ground_truth, string recognised_as, bool duplicate = false);
void AddFalseNegative(string ground_truth);
void AddFalsePositive(string recognised_as);
void Print();
private:
void AddObjectClass(string object_class_name);
int getObjectClassIndex(string object_class_name);
vector<string> class_names;
int confusion_size;
int** confusion_matrix;
int false_index;
int tp, fp, fn;
};
ObjectAndLocation::ObjectAndLocation(string passed_object_name, Point top_left, Point top_right, Point bottom_right, Point bottom_left, Mat object_image)
{
object_name = passed_object_name;
vertices.push_back(top_left);
vertices.push_back(top_right);
vertices.push_back(bottom_right);
vertices.push_back(bottom_left);
setImage(object_image);
}
ObjectAndLocation::ObjectAndLocation(FileNode& node)
{
read(node);
}
void ObjectAndLocation::write(FileStorage& fs)
{
fs << "{" << "nameStr" << object_name;
fs << "coordinates" << "[";
for (int i = 0; i < vertices.size(); ++i)
{
fs << "[:" << vertices[i].x << vertices[i].y << "]";
}
fs << "]";
fs << "}";
}
void ObjectAndLocation::read(FileNode& node)
{
node["nameStr"] >> object_name;
FileNode data = node["coordinates"];
for (FileNodeIterator itData = data.begin(); itData != data.end(); ++itData)
{
// Read each point
FileNode pt = *itData;
Point2i point;
FileNodeIterator itPt = pt.begin();
point.x = *itPt; ++itPt;
point.y = *itPt;
vertices.push_back(point);
}
}
Mat& ObjectAndLocation::getImage()
{
return image;
}
string ObjectAndLocation::getName()
{
return object_name;
}
void ObjectAndLocation::setName(string new_name)
{
object_name.assign(new_name);
}
string ObjectAndLocation::getVerticesString()
{
string result;
for (int index = 0; (index < vertices.size()); index++)
result.append("(" + to_string(vertices[index].x) + " " + to_string(vertices[index].y) + ") ");
return result;
}
void ObjectAndLocation::DrawObject(Mat* display_image, Scalar& colour)
{
writeText(*display_image, object_name, vertices[0].y - 8, vertices[0].x + 8, colour, 2.0, 4);
polylines(*display_image, vertices, true, colour, 8);
}
double ObjectAndLocation::getMinimumSideLength()
{
double min_distance = DistanceBetweenPoints(vertices[0], vertices[vertices.size() - 1]);
for (int index = 0; (index < vertices.size() - 1); index++)
{
double distance = DistanceBetweenPoints(vertices[index], vertices[index + 1]);
if (distance < min_distance)
min_distance = distance;
}
return min_distance;
}
double ObjectAndLocation::getArea()
{
return contourArea(vertices);
}
void ObjectAndLocation::getVertice(int index, int& x, int& y)
{
if ((vertices.size() < index) || (index < 0))
x = y = -1;
else
{
x = vertices[index].x;
y = vertices[index].y;
}
}
ImageWithObjects::ImageWithObjects(string passed_filename)
{
filename = strdup(passed_filename.c_str());
cout << "Opening " << filename << endl;
image = imread(filename, -1);
}
ImageWithObjects::ImageWithObjects(FileNode& node)
{
read(node);
}
ObjectAndLocation* ImageWithObjects::addObject(string object_name, int top_left_column, int top_left_row, int top_right_column, int top_right_row,
int bottom_right_column, int bottom_right_row, int bottom_left_column, int bottom_left_row, Mat& image)
{
ObjectAndLocation new_object(object_name, Point(top_left_column, top_left_row), Point(top_right_column, top_right_row), Point(bottom_right_column, bottom_right_row), Point(bottom_left_column, bottom_left_row), image);
objects.push_back(new_object);
return &(objects[objects.size() - 1]);
}
void ImageWithObjects::write(FileStorage& fs)
{
fs << "{" << "Filename" << filename << "Objects" << "[";
for (int index = 0; index < objects.size(); index++)
objects[index].write(fs);
fs << "]" << "}";
}
void ImageWithObjects::extractAndSetObjectImage(ObjectAndLocation *new_object)
{
Mat perspective_warped_image = Mat::zeros(STANDARD_SIGN_WIDTH_AND_HEIGHT, STANDARD_SIGN_WIDTH_AND_HEIGHT, image.type());
Mat perspective_matrix(3, 3, CV_32FC1);
int x[4], y[4];
new_object->getVertice(0, x[0], y[0]);
new_object->getVertice(1, x[1], y[1]);
new_object->getVertice(2, x[2], y[2]);
new_object->getVertice(3, x[3], y[3]);
Point2f source_points[4] = { { ((float)x[0]), ((float)y[0]) },{ ((float)x[1]), ((float)y[1]) },{ ((float)x[2]), ((float)y[2]) },{ ((float)x[3]), ((float)y[3]) } };
Point2f destination_points[4] = { { 0.0, 0.0 },{ STANDARD_SIGN_WIDTH_AND_HEIGHT - 1, 0.0 },{ STANDARD_SIGN_WIDTH_AND_HEIGHT - 1, STANDARD_SIGN_WIDTH_AND_HEIGHT - 1 },{ 0.0, STANDARD_SIGN_WIDTH_AND_HEIGHT - 1 } };
perspective_matrix = getPerspectiveTransform(source_points, destination_points);
warpPerspective(image, perspective_warped_image, perspective_matrix, perspective_warped_image.size());
new_object->setImage(perspective_warped_image);
}
void ImageWithObjects::read(FileNode& node)
{
filename = (string) node["Filename"];
image = imread(filename, -1);
FileNode images_node = node["Objects"];
if (images_node.type() == FileNode::SEQ)
{
for (FileNodeIterator it = images_node.begin(); it != images_node.end(); ++it)
{
FileNode current_node = *it;
ObjectAndLocation *new_object = new ObjectAndLocation(current_node);
extractAndSetObjectImage(new_object);
objects.push_back(*new_object);
}
}
}
ObjectAndLocation* ImageWithObjects::getObject(int index)
{
if ((index < 0) || (index >= objects.size()))
return NULL;
else return &(objects[index]);
}
void ImageWithObjects::FindBestMatch(ObjectAndLocation* new_object, string& object_name, double& match_value)
{
for (int index = 0; (index < objects.size()); index++)
{
double temp_match_score = objects[index].compareObjects(new_object);
if ((temp_match_score > 0.0) && ((match_value < 0.0) || (temp_match_score < match_value)))
{
object_name = objects[index].getName();
match_value = temp_match_score;
}
}
}
string ImageWithObjects::ExtractObjectName(string filenamestr)
{
int last_slash = filenamestr.rfind("/");
int start_of_object_name = (last_slash == std::string::npos) ? 0 : last_slash + 1;
int extension = filenamestr.find(".", start_of_object_name);
int end_of_filename = (extension == std::string::npos) ? filenamestr.length() - 1 : extension - 1;
int end_of_object_name = filenamestr.find_last_not_of("1234567890", end_of_filename);
end_of_object_name = (end_of_object_name == std::string::npos) ? end_of_filename : end_of_object_name;
string object_name = filenamestr.substr(start_of_object_name, end_of_object_name - start_of_object_name + 1);
return object_name;
}
ImageWithBlueSignObjects::ImageWithBlueSignObjects(string passed_filename) :
ImageWithObjects(passed_filename)
{
}
ImageWithBlueSignObjects::ImageWithBlueSignObjects(FileNode& node) :
ImageWithObjects(node)
{
}
AnnotatedImages::AnnotatedImages(string directory_name)
{
name = directory_name;
for (std::experimental::filesystem::directory_iterator next(std::experimental::filesystem::path(directory_name.c_str())), end; next != end; ++next)
{
read(next->path().generic_string());
}
}
AnnotatedImages::AnnotatedImages()
{
name = "";
}
void AnnotatedImages::addAnnotatedImage(ImageWithObjects &annotated_image)
{
annotated_images.push_back(&annotated_image);
}
void AnnotatedImages::write(FileStorage& fs)
{
fs << "AnnotatedImages";
fs << "{";
fs << "name" << name << "ImagesAndObjects" << "[";
for (int index = 0; index < annotated_images.size(); index++)
annotated_images[index]->write(fs);
fs << "]" << "}";
}
void AnnotatedImages::read(FileStorage& fs)
{
FileNode node = fs.getFirstTopLevelNode();
read(node);
}
void AnnotatedImages::read(FileNode& node)
{
name = (string)node["name"];
FileNode images_node = node["ImagesAndObjects"];
if (images_node.type() == FileNode::SEQ)
{
for (FileNodeIterator it = images_node.begin(); it != images_node.end(); ++it)
{
FileNode current_node = *it;
ImageWithBlueSignObjects* new_image_with_objects = new ImageWithBlueSignObjects(current_node);
annotated_images.push_back(new_image_with_objects);
}
}
}
void AnnotatedImages::read(string filename)
{
ImageWithBlueSignObjects *new_image_with_objects = new ImageWithBlueSignObjects(filename);
annotated_images.push_back(new_image_with_objects);
}
void AnnotatedImages::LocateAndAddAllObjects(AnnotatedImages& training_images)
{
for (int index = 0; index < annotated_images.size(); index++)
{
annotated_images[index]->LocateAndAddAllObjects(training_images);
}
}
void AnnotatedImages::FindBestMatch(ObjectAndLocation* new_object) //Mat& perspective_warped_image, string& object_name, double& match_value)
{
double match_value = -1.0;
string object_name = "Unknown";
double temp_best_match = 1000000.0;
string temp_best_name;
double temp_second_best_match = 1000000.0;
string temp_second_best_name;
for (int index = 0; index < annotated_images.size(); index++)
{
annotated_images[index]->FindBestMatch(new_object, object_name, match_value);
if (match_value < temp_best_match)
{
if (temp_best_name.compare(object_name) != 0)
{
temp_second_best_match = temp_best_match;
temp_second_best_name = temp_best_name;
}
temp_best_match = match_value;
temp_best_name = object_name;
}
else if ((match_value != temp_best_match) && (match_value < temp_second_best_match) && (temp_best_name.compare(object_name) != 0))
{
temp_second_best_match = match_value;
temp_second_best_name = object_name;
}
}
if (temp_second_best_match / temp_best_match < REQUIRED_RATIO_OF_BEST_TO_SECOND_BEST)
new_object->setName("Unknown");
else new_object->setName(temp_best_name);
}
Mat AnnotatedImages::getImageOfAllObjects(int break_after)
{
Mat all_rows_so_far;
Mat output;
int count = 0;
int object_index = 0;
string blank("");
for (int index = 0; (index < annotated_images.size()); index++)
{
ObjectAndLocation* current_object = NULL;
int object_index = 0;
while ((current_object = (annotated_images[index])->getObject(object_index)) != NULL)
{
if (count == 0)
{
output = JoinSingleImage(current_object->getImage(), current_object->getName());
}
else if (count % break_after == 0)
{
if (count == break_after)
all_rows_so_far = output;
else
{
Mat temp_rows = JoinImagesVertically(all_rows_so_far, blank, output, blank, 0);
all_rows_so_far = temp_rows.clone();
}
output = JoinSingleImage(current_object->getImage(), current_object->getName());
}
else
{
Mat new_output = JoinImagesHorizontally(output, blank, current_object->getImage(), current_object->getName(), 0);
output = new_output.clone();
}
count++;
object_index++;
}
}
if (count == 0)
{
Mat blank_output(1, 1, CV_8UC3, Scalar(0, 0, 0));
return blank_output;
}
else if (count < break_after)
return output;
else {
Mat temp_rows = JoinImagesVertically(all_rows_so_far, blank, output, blank, 0);
all_rows_so_far = temp_rows.clone();
return all_rows_so_far;
}
}
ImageWithObjects* AnnotatedImages::getAnnotatedImage(int index)
{
if ((index >= 0) && (index < annotated_images.size()))
return annotated_images[index];
else return NULL;
}
ImageWithObjects* AnnotatedImages::FindAnnotatedImage(string filename_to_find)
{
for (int index = 0; (index < annotated_images.size()); index++)
{
if (filename_to_find.compare(annotated_images[index]->filename) == 0)
return annotated_images[index];
}
return NULL;
}
void MyApplication()
{
AnnotatedImages trainingImages;
FileStorage training_file("BlueSignsTraining.xml", FileStorage::READ);
if (!training_file.isOpened())
{
cout << "Could not open the file: \"" << "BlueSignsTraining.xml" << "\"" << endl;
}
else
{
trainingImages.read(training_file);
}
training_file.release();
Mat image_of_all_training_objects = trainingImages.getImageOfAllObjects();
imshow("All Training Objects", image_of_all_training_objects);
imwrite("AllTrainingObjectImages.jpg", image_of_all_training_objects);
char ch = cv::waitKey(1);
AnnotatedImages groundTruthImages;
FileStorage ground_truth_file("BlueSignsGroundTruth.xml", FileStorage::READ);
if (!ground_truth_file.isOpened())
{
cout << "Could not open the file: \"" << "BlueSignsGroundTruth.xml" << "\"" << endl;
}
else
{
groundTruthImages.read(ground_truth_file);
}
ground_truth_file.release();
Mat image_of_all_ground_truth_objects = groundTruthImages.getImageOfAllObjects();
imshow("All Ground Truth Objects", image_of_all_ground_truth_objects);
imwrite("AllGroundTruthObjectImages.jpg", image_of_all_ground_truth_objects);
ch = cv::waitKey(1);
AnnotatedImages unknownImages("Blue Signs/Testing");
unknownImages.LocateAndAddAllObjects(trainingImages);
FileStorage unknowns_file("BlueSignsTesting.xml", FileStorage::WRITE);
if (!unknowns_file.isOpened())
{
cout << "Could not open the file: \"" << "BlueSignsTesting.xml" << "\"" << endl;
}
else
{
unknownImages.write(unknowns_file);
}
unknowns_file.release();
Mat image_of_recognised_objects = unknownImages.getImageOfAllObjects();
imshow("All Recognised Objects", image_of_recognised_objects);
imwrite("AllRecognisedObjects.jpg", image_of_recognised_objects);
ConfusionMatrix results(trainingImages);
unknownImages.CompareObjectsWithGroundTruth(trainingImages, groundTruthImages, results);
results.Print();
}
bool PointInPolygon(Point2i point, vector<Point2i> vertices)
{
int i, j, nvert = vertices.size();
bool inside = false;
for (i = 0, j = nvert - 1; i < nvert; j = i++)
{
if ((vertices[i].x == point.x) && (vertices[i].y == point.y))
return true;
if (((vertices[i].y >= point.y) != (vertices[j].y >= point.y)) &&
(point.x <= (vertices[j].x - vertices[i].x) * (point.y - vertices[i].y) / (vertices[j].y - vertices[i].y) + vertices[i].x)
)
inside = !inside;
}
return inside;
}
bool ObjectAndLocation::OverlapsWith(ObjectAndLocation* other_object)
{
double area = contourArea(vertices);
double other_area = contourArea(other_object->vertices);
double overlap_area = 0.0;
int count_points_inside = 0;
for (int index = 0; (index < vertices.size()); index++)
{
if (PointInPolygon(vertices[index], other_object->vertices))
count_points_inside++;
}
int count_other_points_inside = 0;
for (int index = 0; (index < other_object->vertices.size()); index++)
{
if (PointInPolygon(other_object->vertices[index], vertices))
count_other_points_inside++;
}
if (count_points_inside == vertices.size())
overlap_area = area;
else if (count_other_points_inside == other_object->vertices.size())
overlap_area = other_area;
else if ((count_points_inside == 0) && (count_other_points_inside == 0))
overlap_area = 0.0;
else
{ // There is a partial overlap of the polygons.
// Find min & max x & y for the current object
int min_x = vertices[0].x, min_y = vertices[0].y, max_x = vertices[0].x, max_y = vertices[0].y;
for (int index = 0; (index < vertices.size()); index++)
{
if (min_x > vertices[index].x)
min_x = vertices[index].x;
else if (max_x < vertices[index].x)
max_x = vertices[index].x;
if (min_y > vertices[index].y)
min_y = vertices[index].y;
else if (max_y < vertices[index].y)
max_y = vertices[index].y;
}
int min_x2 = other_object->vertices[0].x, min_y2 = other_object->vertices[0].y, max_x2 = other_object->vertices[0].x, max_y2 = other_object->vertices[0].y;
for (int index = 0; (index < other_object->vertices.size()); index++)
{
if (min_x2 > other_object->vertices[index].x)
min_x2 = other_object->vertices[index].x;
else if (max_x2 < other_object->vertices[index].x)
max_x2 = other_object->vertices[index].x;
if (min_y2 > other_object->vertices[index].y)
min_y2 = other_object->vertices[index].y;
else if (max_y2 < other_object->vertices[index].y)
max_y2 = other_object->vertices[index].y;
}
// We only need the maximum overlapping bounding boxes
if (min_x < min_x2) min_x = min_x2;
if (max_x > max_x2) max_x = max_x2;
if (min_y < min_y2) min_y = min_y2;
if (max_y > max_y2) max_y = max_y2;
// For all points
overlap_area = 0;
Point2i current_point;
// Try ever decreasing squares within the overlapping (image aligned) bounding boxes to find the overlapping area.
bool all_points_inside = false;
int distance_from_edge = 0;
for (; ((distance_from_edge < (max_x - min_x + 1) / 2) && (distance_from_edge < (max_y - min_y + 1) / 2) && (!all_points_inside)); distance_from_edge++)
{
all_points_inside = true;
for (current_point.x = min_x + distance_from_edge; (current_point.x <= (max_x - distance_from_edge)); current_point.x++)
for (current_point.y = min_y + distance_from_edge; (current_point.y <= max_y - distance_from_edge); current_point.y += max_y - 2 * distance_from_edge - min_y)
{
if ((PointInPolygon(current_point, vertices)) && (PointInPolygon(current_point, other_object->vertices)))
overlap_area++;
else all_points_inside = false;
}
for (current_point.y = min_y + distance_from_edge + 1; (current_point.y <= (max_y - distance_from_edge - 1)); current_point.y++)
for (current_point.x = min_x + distance_from_edge; (current_point.x <= max_x - distance_from_edge); current_point.x += max_x - 2 * distance_from_edge - min_x)
{
if ((PointInPolygon(current_point, vertices)) && (PointInPolygon(current_point, other_object->vertices)))
overlap_area++;
else all_points_inside = false;
}
}
if (all_points_inside)
overlap_area += (max_x - min_x + 1 - 2 * (distance_from_edge + 1)) * (max_y - min_y + 1 - 2 * (distance_from_edge + 1));
}
double percentage_overlap = (overlap_area*2.0) / (area + other_area);
return (percentage_overlap >= REQUIRED_OVERLAP);
}
void AnnotatedImages::CompareObjectsWithGroundTruth(AnnotatedImages& training_images, AnnotatedImages& ground_truth, ConfusionMatrix& results)
{
// For every annotated image in ground_truth, find the corresponding image in this
for (int ground_truth_image_index = 0; ground_truth_image_index < ground_truth.annotated_images.size(); ground_truth_image_index++)
{
ImageWithObjects* current_annotated_ground_truth_image = ground_truth.annotated_images[ground_truth_image_index];
ImageWithObjects* current_annotated_recognition_image = FindAnnotatedImage(current_annotated_ground_truth_image->filename);
if (current_annotated_recognition_image != NULL)
{
ObjectAndLocation* current_ground_truth_object = NULL;
int ground_truth_object_index = 0;
Mat* display_image = NULL;
if (!current_annotated_recognition_image->image.empty())
{
display_image = &(current_annotated_recognition_image->image);
}
// For each object in ground_truth.annotated_image
while ((current_ground_truth_object = current_annotated_ground_truth_image->getObject(ground_truth_object_index)) != NULL)
{
if ((current_ground_truth_object->getMinimumSideLength() >= MINIMUM_SIGN_SIDE) &&
(current_ground_truth_object->getArea() >= MINIMUM_SIGN_AREA))
{
// Determine the number of overlapping objects (correct & incorrect)
vector<ObjectAndLocation*> overlapping_correct_objects;
vector<ObjectAndLocation*> overlapping_incorrect_objects;
ObjectAndLocation* current_recognised_object = NULL;
int recognised_object_index = 0;
// For each object in this.annotated_image
while ((current_recognised_object = current_annotated_recognition_image->getObject(recognised_object_index)) != NULL)
{
if (current_recognised_object->getName().compare("Unknown") != 0)
if (current_ground_truth_object->OverlapsWith(current_recognised_object))
{
if (current_ground_truth_object->getName().compare(current_recognised_object->getName()) == 0)
overlapping_correct_objects.push_back(current_recognised_object);
else overlapping_incorrect_objects.push_back(current_recognised_object);
}
recognised_object_index++;
}
if ((overlapping_correct_objects.size() == 0) && (overlapping_incorrect_objects.size() == 0))
{
if (display_image != NULL)
{
Scalar colour(0x00, 0x00, 0xFF);
current_ground_truth_object->DrawObject(display_image, colour);
}
results.AddFalseNegative(current_ground_truth_object->getName());
cout << current_annotated_ground_truth_image->filename << ", " << current_ground_truth_object->getName() << ", (False Negative) , " << current_ground_truth_object->getVerticesString() << endl;
}
else {
for (int index = 0; (index < overlapping_correct_objects.size()); index++)
{
Scalar colour(0x00, 0xFF, 0x00);
results.AddMatch(current_ground_truth_object->getName(), overlapping_correct_objects[index]->getName(), (index > 0));
if (index > 0)
{
colour[2] = 0xFF;
cout << current_annotated_ground_truth_image->filename << ", " << current_ground_truth_object->getName() << ", (Duplicate) , " << current_ground_truth_object->getVerticesString() << endl;
}
if (display_image != NULL)
current_ground_truth_object->DrawObject(display_image, colour);
}
for (int index = 0; (index < overlapping_incorrect_objects.size()); index++)
{
if (display_image != NULL)
{
Scalar colour(0xFF, 0x00, 0xFF);
overlapping_incorrect_objects[index]->DrawObject(display_image, colour);
}
results.AddMatch(current_ground_truth_object->getName(), overlapping_incorrect_objects[index]->getName(), (index > 0));
cout << current_annotated_ground_truth_image->filename << ", " << current_ground_truth_object->getName() << ", (Mismatch), " << overlapping_incorrect_objects[index]->getName() << " , " << current_ground_truth_object->getVerticesString() << endl;;
}
}
}
else
cout << current_annotated_ground_truth_image->filename << ", " << current_ground_truth_object->getName() << ", (DROPPED GT) , " << current_ground_truth_object->getVerticesString() << endl;
ground_truth_object_index++;
}
// For each object in this.annotated_image
// For each overlapping object in ground_truth.annotated_image
// Don't do anything (as already done above)
// If no overlapping objects.
// Update the confusion table (with a False Positive)
ObjectAndLocation* current_recognised_object = NULL;
int recognised_object_index = 0;
// For each object in this.annotated_image
while ((current_recognised_object = current_annotated_recognition_image->getObject(recognised_object_index)) != NULL)
{
if ((current_recognised_object->getMinimumSideLength() >= MINIMUM_SIGN_SIDE) &&
(current_recognised_object->getArea() >= MINIMUM_SIGN_AREA))
{
// Determine the number of overlapping objects (correct & incorrect)
vector<ObjectAndLocation*> overlapping_objects;
ObjectAndLocation* current_ground_truth_object = NULL;
int ground_truth_object_index = 0;
// For each object in ground_truth.annotated_image
while ((current_ground_truth_object = current_annotated_ground_truth_image->getObject(ground_truth_object_index)) != NULL)
{
if (current_ground_truth_object->OverlapsWith(current_recognised_object))
overlapping_objects.push_back(current_ground_truth_object);
ground_truth_object_index++;
}
if ((overlapping_objects.size() == 0) && (current_recognised_object->getName().compare("Unknown") != 0))
{
results.AddFalsePositive(current_recognised_object->getName());
if (display_image != NULL)
{
Scalar colour(0x7F, 0x7F, 0xFF);
current_recognised_object->DrawObject(display_image, colour);
}
cout << current_annotated_recognition_image->filename << ", " << current_recognised_object->getName() << ", (False Positive) , " << current_recognised_object->getVerticesString() << endl;
}
}
else
cout << current_annotated_recognition_image->filename << ", " << current_recognised_object->getName() << ", (DROPPED) , " << current_recognised_object->getVerticesString() << endl;
recognised_object_index++;
}
if (display_image != NULL)
{
Mat smaller_image;
resize(*display_image, smaller_image, Size(display_image->cols / 4, display_image->rows / 4));
imshow(current_annotated_recognition_image->filename, smaller_image);
char ch = cv::waitKey(1);
// delete display_image;
}
}
}
}
// Determine object classes from the training_images (vector of strings)
// Create and zero a confusion matrix
ConfusionMatrix::ConfusionMatrix(AnnotatedImages training_images)
{
// Extract object class names
ImageWithObjects* current_annnotated_image = NULL;
int image_index = 0;
while ((current_annnotated_image = training_images.getAnnotatedImage(image_index)) != NULL)
{
ObjectAndLocation* current_object = NULL;
int object_index = 0;
while ((current_object = current_annnotated_image->getObject(object_index)) != NULL)
{
AddObjectClass(current_object->getName());
object_index++;
}
image_index++;
}
// Create and initialise confusion matrix
confusion_size = class_names.size() + 1;
confusion_matrix = new int*[confusion_size];
for (int index = 0; (index < confusion_size); index++)
{
confusion_matrix[index] = new int[confusion_size];
for (int index2 = 0; (index2 < confusion_size); index2++)
confusion_matrix[index][index2] = 0;
}
false_index = confusion_size - 1;
}
void ConfusionMatrix::AddObjectClass(string object_class_name)
{
int index = getObjectClassIndex(object_class_name);
if (index == -1)
class_names.push_back(object_class_name);
tp = fp = fn = 0;
}
int ConfusionMatrix::getObjectClassIndex(string object_class_name)
{
int index = 0;
for (; (index < class_names.size()) && (object_class_name.compare(class_names[index]) != 0); index++)
;
if (index < class_names.size())
return index;
else return -1;
}
void ConfusionMatrix::AddMatch(string ground_truth, string recognised_as, bool duplicate)
{
if ((ground_truth.compare(recognised_as) == 0) && (duplicate))
AddFalsePositive(recognised_as);
else
{
confusion_matrix[getObjectClassIndex(ground_truth)][getObjectClassIndex(recognised_as)]++;
if (ground_truth.compare(recognised_as) == 0)
tp++;
else {
fp++;
fn++;
}
}
}
void ConfusionMatrix::AddFalseNegative(string ground_truth)
{
fn++;
confusion_matrix[getObjectClassIndex(ground_truth)][false_index]++;
}
void ConfusionMatrix::AddFalsePositive(string recognised_as)
{
fp++;
confusion_matrix[false_index][getObjectClassIndex(recognised_as)]++;
}
void ConfusionMatrix::Print()
{
cout << ",,,Recognised as:" << endl << ",,";
for (int recognised_as_index = 0; recognised_as_index < confusion_size; recognised_as_index++)
if (recognised_as_index < confusion_size - 1)
cout << class_names[recognised_as_index] << ",";
else cout << "False Negative,";
cout << endl;
for (int ground_truth_index = 0; (ground_truth_index <= class_names.size()); ground_truth_index++)
{
if (ground_truth_index < confusion_size - 1)
cout << "Ground Truth," << class_names[ground_truth_index] << ",";
else cout << "Ground Truth,False Positive,";
for (int recognised_as_index = 0; recognised_as_index < confusion_size; recognised_as_index++)
cout << confusion_matrix[ground_truth_index][recognised_as_index] << ",";
cout << endl;
}
double precision = ((double)tp) / ((double)(tp + fp));
double recall = ((double)tp) / ((double)(tp + fn));
double f1 = 2.0*precision*recall / (precision + recall);
cout << endl << "Precision = " << precision << endl << "Recall = " << recall << endl << "F1 = " << f1 << endl;
}
bool isSquare(vector<Point> contour)
{
Mat approx;
approxPolyDP(contour, approx, 0.04*arcLength(contour, true), true);
Rect rect = boundingRect(approx);
double area_aspect = (float)contourArea(approx) / (float)rect.area();
double length_aspect = (float)rect.height / (float)rect.width;
if (area_aspect > 0.5&&length_aspect > 0.5&&length_aspect < 1.3)
{
return true;
}
return false;
}
void ObjectAndLocation::setImage(Mat object_image)
{
image = object_image.clone();
// *** Student should add any initialisation (of their images or features; see private data below) they wish into this method.
// The image reduction stuff will happen
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
resize(image,image, Size(STANDARD_SIGN_WIDTH_AND_HEIGHT, STANDARD_SIGN_WIDTH_AND_HEIGHT), 0, 0, INTER_AREA);
cvtColor(image, image, COLOR_BGR2GRAY, 0);
GaussianBlur(image, image, Size(3, 3), 0, 0);
}
float Distance(Point2f first, Point2f second)
{
int x_diff = first.x - second.x;
int y_diff = first.y - second.y;
int sum = pow(x_diff,2) + pow(y_diff,2);
return pow(sum,0.5);
}
void ImageWithBlueSignObjects::LocateAndAddAllObjects(AnnotatedImages& training_images)
{
//***Student needs to develop this routine and add in objects using the addObject method
Mat ORG = this->image.clone();
//first reduce own to the blue areas here on a single image by using the all contour techineque
GaussianBlur(this->image, this->image, Size(5, 5), 0, 0);
Mat org = this->image.clone();
cvtColor(org, org, COLOR_BGR2GRAY);
Canny(org, org, 150, 270, 3);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(org, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
Mat drawing = Mat::zeros(org.size(), CV_8U);
for (int i = 0; i < contours.size(); i++)
{
if (hierarchy[i][2] != -1 && contourArea(contours[i]) > MINIMUM_SIGN_AREA && isSquare(contours[i]))
{
Scalar color = Scalar(255, 255, 255);
drawContours(drawing, contours, i, color, FILLED, 8, hierarchy, 0, Point());
}
}
Mat original(Mat::zeros(org.size(), CV_8U));
bitwise_and(ORG, ORG, original, drawing);
this->image = original.clone();
//The only run the blue and image thing by using the external contour technique to extract the blue sign objects
vector<vector<Point>> contours1;
vector<Vec4i> hierarchy1;
Mat org2 = this->image.clone();
GaussianBlur(org2, org2, Size(5, 5), 0, 0);
cvtColor(org2, org2, COLOR_BGR2GRAY);
findContours(org2, contours1, hierarchy1, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
for (int i = 0; i < contours1.size(); i++)
{
cv::Mat approx;
approxPolyDP(contours1[i], approx, 0.04*arcLength(contours1[i], true), true);
if (approx.rows == 4)
{
approx.reshape(4, 2);
int top_1 = 0; int bottom_1 = 0;
for (int j = 0; j < 4; j++)
{
if (approx.at<int>(j, 0) < approx.at<int>(top_1, 0)) top_1 = j;
if (approx.at<int>(j, 0) > approx.at<int>(bottom_1, 0)) bottom_1 = j;
}
int top_2 = (top_1+1)%4;
int bottom_2 = (bottom_1+1) % 4;
for (int j = 0; j < 4; j++)
{
if (approx.at<int>(j, 0) < approx.at<int>(top_2, 0) && j != top_1) top_2 = j;
if (approx.at<int>(j, 0) > approx.at<int>(bottom_2, 0) && j != bottom_1) bottom_2 = j;
}
int top_left = 0; int top_right = 0; int bottom_left = 0; int bottom_right = 0;
if (approx.at<int>(top_1, 1) > approx.at<int>(top_2, 1))
{
top_left = top_2; top_right = top_1;
}
else { top_left = top_1; top_right = top_2; }
if (approx.at<int>(bottom_1, 1) > approx.at<int>(bottom_2, 1))
{
bottom_left = bottom_2; bottom_right = bottom_1;
}
else {bottom_left = bottom_1; bottom_right = bottom_2;}
int x = top_right;
top_right=bottom_left;
bottom_left = x;
Point2f TL(approx.at<int>(top_left, 0), approx.at<int>(top_left, 1));
Point2f TR(approx.at<int>(top_right, 0), approx.at<int>(top_right, 1));
Point2f BR(approx.at<int>(bottom_right, 0), approx.at<int>(bottom_right, 1));
Point2f BL(approx.at<int>(bottom_left, 0), approx.at<int>(bottom_left, 1));
float width, height;
//width
if (Distance(TL, TR) > Distance(BL, BR))
width = Distance(TL, TR);
else width = Distance(TL, TR);
//height
if (Distance(TL, BL) > Distance(TR, BR))
height = Distance(TL, TR);
else height = Distance(TL, TR);
Point2f source_points[4] = { TL,TR,BR,BL };
Point2f destination_points[4] = { { 0.0, 0.0 },{ width - 1, 0.0 },{ width - 1, height - 1 },{ 0.0, height - 1 } };
Mat perspective_matrix(3, 3, this->image.type());
perspective_matrix =getPerspectiveTransform(source_points, destination_points);
Mat Sign;
warpPerspective(ORG, Sign, perspective_matrix, Size(width,height));
ObjectAndLocation *obh = addObject(this->filename, TL.x, TL.y, TR.x, TR.y, BR.x, BR.y, BL.x, BL.y, Sign);
training_images.FindBestMatch(obh);
}
}
this->image = ORG.clone();
}
#define BAD_MATCHING_VALUE 1000000000.0;
double ObjectAndLocation::compareObjects(ObjectAndLocation* otherObject)
{
Mat res;
double minVal, maxVal;
Point minLoc, maxLoc;