-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDriver.cpp
More file actions
1360 lines (1023 loc) · 34.6 KB
/
Driver.cpp
File metadata and controls
1360 lines (1023 loc) · 34.6 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
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 <iostream>
#include <cmath>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include "image.h"
#include "Stack.h"
#include "Queue.h"
#include "QuadTree.h"
using namespace std;
/*Prototypes*/
int readImageHeader(char[], int&, int&, int&, bool&, bool&); //reads the information provided by an image when it is saved in the .pgm format
int readImage(char[], ImageType&); //reads in an image from a file, has its pixel values stored in an array, and the image width, height, and number of gray-levels are placed under the appropriate variables
int writeImage(char[], ImageType&); //writes out an image to a file in the format of .pgm
int computeComponents(ImageType& inputImage, ImageType& outputImage, ImageType& image3, bool BFS, SortedList& ListOfRegions); // Computes and labels the number of regions in the image
void findComponentBFS(ImageType& inputImage, ImageType& outputImage, int label, Queue& queue, nodeData& region); // Performs a breadth-first search recursively
void findComponentDFS(ImageType& inputImage, ImageType& outputImage, int label, Stack& stack); // Performs a depth-first search recursively
void deleteSmallComponents(SortedList& listOfRegions, int threshold);
double computeMoment( int p, int q, double xCenter, double yCenter, UnsortedList& list ); // Computes the p,q order moment for a region
int main()
{
int i, j;
int sizeThreshold;
int length;
double A, B;
int M, N, Q;
int K;
char choice = '\0';
int S, T, thresh;
char reflectChoice = '\0';
char searchChoice = '\0';
bool BFSFlag;
int components;
bool type;
bool vertFlag;
bool continueProgram = true;
bool fileRead = false;
char filename[50];
char filename2[50];
char fileSave[50];
int val;
point pixel;
SortedList ListOfRegions;
UnsortedList pixelList;
nodeData region;
cout << endl << endl << endl << endl << endl;
cout << "Welcome to Bobby and Will's image editor.";
ImageType *image;
// allocate memory for the image array
image = new ImageType(1, 1, 255);
ImageType *image2;
image2 = new ImageType(1, 1, 255);
ImageType *image3;
image3 = new ImageType(1, 1, 255);
// DRIVER MENU
// Keep program running until quit
while (continueProgram)
{
// Main Menu
cout << endl << "///////////////////////////////////////////////////" << endl << "Enter the capital letter of your menu choice";
if ( fileRead == false )
{
cout << endl << "WARNING: NO IMAGE FILE. YOU MUST FIRST READ A FILE.";
}
cout << endl << "A. Read an image file. WARNING: You will lose all unsaved changes." << endl;
cout << "B. Save the image." << endl;
cout << "C. Extract a subimage from the image." << endl;
cout << "D. Compute the average gray-level value of the image." << endl;
cout << "E. Enlarge the image." << endl;
cout << "F. Shrink the image." << endl;
cout << "G. Reflect the image over the horizontal or vertical axis." << endl;
cout << "H. Translate the image." << endl;
cout << "I. Rotate the image." << endl;
cout << "J. Add an image to the current image." << endl;
cout << "K. Subtract an image from the current image." << endl;
cout << "L. Compute the negative of the image." << endl;
cout << "M. Label and compute the number of regions in the image." << endl;
cout << "N. Quit." << endl << endl;
cout << "Enter Selection: ";
cin >> choice;
cout << endl << endl ;
// Test for user input
switch (choice)
{
case 'A':
delete image;
cout << endl << endl << "Please enter the file name of the image you would like to read/edit." << endl << "You may change this later. The name must be under 50 characters.";
cout << endl << "File name: ";
cin >> filename;
// read image header
readImageHeader(filename, N, M, Q, type, fileRead);
// allocate memory for the image array
if ( fileRead == true )
{
image = new ImageType(N, M, Q);
readImage( filename, *image );
cout << "Image file read successfully." << endl;
}
else
{
image = new ImageType(1,1,255);
}
break;
case 'B':
//Test that a file has been read and save the image
if ( fileRead == true )
{
cout << endl << endl << "Enter the file name that you want to save the image under." << endl << " Be sure to use the .pgm extension." << endl;
cin >> fileSave;
writeImage(fileSave, *image);
break;
}
cout << endl << endl << "Sorry, there is no current image.";
break;
case 'C':
int nStart, nEnd, mStart, mEnd;
//Test that a file has been read and crop the image
if ( fileRead == false )
{
cout << endl << endl << "Sorry, there is no current image.";
break;
}
// Get the subimage coordinates from the user and test that they are in the bounds
cout << endl << "Enter the left column. (Between 0 and " << (N-2) << ", inclusive ): ";
cin >> nStart;
if ( nStart < 0 || nStart >= (N-1) )
{
cout << endl << "ERROR: Invalid dimension. Try again. " << endl;
break;
}
cout << endl << "Enter the right column. (Between " << nStart+1 << " and " << (N-1) << ", inclusive ): ";
cin >> nEnd;
if ( nEnd <= nStart || nEnd >= N )
{
cout << endl << "ERROR: Invalid dimension. Try again. " << endl;
break;
}
cout << endl << "Enter the top row. (Between 0 and " << (M-2) << ", inclusive ): ";
cin >> mStart;
if ( mStart < 0 || mStart >= (M-1) )
{
cout << endl << "ERROR: Invalid dimension. Try again. " << endl;
break;
}
cout << endl << "Enter the bottom row. (Between " << mStart+1 << " and " << (M-1) << ", inclusive ): ";
cin >> mEnd;
if ( mEnd <= mStart || mEnd >= M )
{
cout << endl << "ERROR: Invalid dimension. Try again. " << endl;
break;
}
(*image2).getSubImage( nStart, nEnd, mStart, mEnd, *image );
*image = *image2;
break;
case 'D':
//Test that a file has been read and compute the average gray-level
if ( fileRead == false )
{
cout << endl << endl << "Sorry, there is no current image.";
break;
}
cout << endl << endl << "The average gray-level value is: " << (*image).meanGray();
break;
case 'E':
//Test that a file has been read and enlarge the image
if ( fileRead == false )
{
cout << endl << endl << "Sorry, there is no current image.";
break;
}
//Get the enlargement factor from the user and test that it is between 0 and 10
cout << endl << endl << "Enter the enlargement factor S (Greater than 0 but less than 10): ";
cin >> S;
if ( S <= 0 || S >= 10)
{
cout << endl << "ERROR: Invalid S. Try Again." << endl;
break;
}
(*image2).enlargeImage( S, *image );
*image = *image2;
break;
case 'F':
//Test that a file has been read and shrink the image
if ( fileRead == false )
{
cout << endl << endl << "Sorry, there is no current image.";
break;
}
//For the case that the image is tall make sure the shrinking factor is less than half of the width
if ( N < M )
{
cout << endl << endl << "Enter the shrinking factor S (Greater than 0 but less than " << N/2+1 << " ): ";
cin >> S;
if ( S <= 0 || S >= (N/2+1) )
{
cout << endl << "ERROR: Invalid S. Try Again." << endl;
break;
}
}
//For the case that the image is wide make sure the shrinking factor is less than half of the height
else
{
cout << endl << endl << "Enter the shrinking factor S (Greater than 0 but less than " << M/2+1 << " ): ";
cin >> S;
if ( S <= 0 || S >= (M/2+1) )
{
cout << endl << "ERROR: Invalid S. Try Again." << endl;
break;
}
}
(*image2).shrinkImage( S, *image );
*image = *image2;
break;
case 'G':
//Test that a file has been read and reflect the image
if ( fileRead == false )
{
cout << endl << endl << "Sorry, there is no current image.";
break;
}
// Get the reflection axis from the user and test that it is within bounds. Set the vertFlag
cout << endl << endl << "Please enter the capital letter of your choice." << endl << "A. Reflect over the vertical axis.";
cout << endl << "B. Reflect over the horizontal axis." << endl << endl << "Choice: ";
cin >> reflectChoice;
if ( reflectChoice == 'A' )
{
vertFlag = true;
}
else if ( reflectChoice == 'B' )
{
vertFlag = false;
}
else
{
cout << endl << "ERROR: Invalid choice. Try Again." << endl;
break;
}
(*image2).reflectImage( vertFlag, *image );
*image = *image2;
break;
case 'H':
//Test that a file has been read and translate the image
if ( fileRead == false )
{
cout << endl << endl << "Sorry, there is no current image.";
break;
}
//If the image is tall, test that the translation value is between zero and the width
if ( N < M )
{
cout << endl << endl << "Enter the translation value T (Between 0 and " << N-1 << " ): ";
cin >> T;
if ( T < 0 || T >= N )
{
cout << endl << "ERROR: Invalid T. Try Again." << endl;
break;
}
}
//If the image is wide, test that the translation value is between zero and the height
else
{
cout << endl << endl << "Enter the translation value T (Between 0 and " << M-1 << " ): ";
cin >> T;
if ( T < 0 || T >= M )
{
cout << endl << "ERROR: Invalid T. Try Again." << endl;
break;
}
}
(*image2).translateImage( T, *image );
*image = *image2;
break;
case 'I':
//Test that a file has been read and rotate the image
if ( fileRead == false )
{
cout << endl << endl << "Sorry, there is no current image.";
break;
}
//Test that the angle of rotation is between 0 and 360
cout << endl << endl << "Enter the angle of rotation: ";
cin >> S;
if ( S < 0 || S > 360)
{
cout << endl << "ERROR: Invalid S. Try Again." << endl;
break;
}
(*image2).rotateImage( S, *image );
*image = *image2;
break;
case 'J':
//Test that a file has been read and add two images
if ( fileRead == true )
{
int Noriginal, Moriginal;
delete image2;
//Get the second image file name from user and attempt to open image
cout << endl << endl << "Enter the file name that you want to add to the current file." << endl << " Be sure the image is the same size as the first." << endl << "File: ";
cin >> filename2;
readImageHeader(filename2, N, M, Q, type, fileRead);
if ( fileRead == true )
{
image2 = new ImageType(N, M, Q);
readImage( filename2, *image2 );
cout << "Image file read successfully." << endl;
}
else
{
image = new ImageType(1,1,255);
}
(*image).getImageInfo(Noriginal, Moriginal, Q);
if (Moriginal == M && Noriginal == N)
{
(*image) + (*image2);
}
//If the images are not the same size, or the image file name was invalid, return error
else
{
cout << "Images not the same size, please try another image.";
}
break;
}
cout << endl << endl << "Sorry, there is no current image.";
break;
case 'K':
//Test that a file has been read and subtract two images
if ( fileRead == true )
{
int Noriginal, Moriginal;
delete image2;
//Get the second image file name from the user and attempt to open image
cout << endl << endl << "Enter the file name that you want to subtract from the current file." << endl << " Be sure the image is the same size as the first." << endl << "File: ";
cin >> filename2;
readImageHeader(filename2, N, M, Q, type, fileRead);
if ( fileRead == true )
{
image2 = new ImageType(N, M, Q);
readImage( filename2, *image2 );
cout << "Image file read successfully." << endl;
}
else
{
image = new ImageType(1,1,255);
}
(*image).getImageInfo(Noriginal, Moriginal, Q);
//If the images are not the same size, or the image file name was invalid, return error
if (Moriginal == M && Noriginal == N)
{
(*image) - (*image2);
}
else
{
cout << "Images not the same size, please try another image.";
}
break;
}
cout << endl << endl << "Sorry, there is no current image.";
break;
case 'L':
//Test that a file has been read and negate the image
if ( fileRead == true )
{
(*image).negateImage();
cout << "The image has beem negated";
}
else
{
image = new ImageType(1,1,255);
cout << endl << endl << "Sorry, there is no current image.";
}
break;
case 'M':
//Test that a file has been read and count the regions in the image
if ( fileRead == false )
{
cout << endl << endl << "Sorry, there is no current image.";
break;
}
//Obtain threshold from user and test that it is within bounds
cout << endl << endl << "Please enter the threshold you would like to use." << endl << "(Hint: Approx. 127 is a good value)" << endl;
cin >> thresh;
if ( thresh < 0 || thresh > 255 )
{
cout << endl << "ERROR: Invalid choice. Try Again." << endl;
break;
}
//Get search type from user and return error if invalid entry
cout << endl << endl << "Please enter the capital letter of your choice." << endl << "A. Breadth-First Search.";
cout << endl << "B. Depth-First Search." << endl << endl << "Choice: ";
cin >> searchChoice;
if ( searchChoice == 'A' )
{
BFSFlag = true;
}
else if ( searchChoice == 'B' )
{
BFSFlag = false;
}
else
{
cout << endl << "ERROR: Invalid choice. Try Again." << endl;
break;
}
//Threshold, diate, erode, and then compute the components of the image
*image3 = *image;
(*image2).thresholdImage( thresh, *image );
*image = *image2;
(*image2).dilateImage( *image );
*image = *image2;
(*image2).erodeImage( *image );
*image = *image2;
components = computeComponents( *image, *image2, *image3, BFSFlag, ListOfRegions );
cout << endl << "There are " << components << " regions in this image.";
*image = *image2;
// Delete small regions by getting threshold
cout << endl << endl << "Please enter the threshold of region sizes you would like to delete." << endl;
cin >> sizeThreshold;
sizeThreshold = abs(sizeThreshold);
deleteSmallComponents(ListOfRegions, sizeThreshold);
//Print the submenu for the user
cout << endl << endl << "Please enter the capital letter of your choice." << endl << "A. Display regions having size between X and Y.";
cout << endl << "B. Display regions having orientation between X and Y." << endl << "C. Display regions having eccentricity between X and Y.";
cout << endl << "D. Display regions having mean intensity between X and Y." << endl << "E. Display the K regions closest to (X,Y)." << endl << "F. Quit to main menu and display labeled image" << endl << endl << "Choice: ";
cin >> searchChoice;
if (( searchChoice != 'A' )&&( searchChoice != 'B' )&&( searchChoice != 'C' )&&( searchChoice != 'D' )&&( searchChoice != 'E' ))
break;
delete image;
(*image3).getImageInfo( N, M, Q );
image = new ImageType( N, M, Q );
cout << endl << endl << "Please enter the values for X and Y";
if (( searchChoice == 'A' )&&( searchChoice == 'B' )&&( searchChoice == 'C' )&&( searchChoice == 'D' ))
cout << ", X being less than Y.";
cout << endl << "X: ";
cin >> A;
cout << endl << "Y: ";
cin >> B;
ListOfRegions.ResetList();
//Find and display sizes between A and B
if ( searchChoice == 'A' )
{
ListOfRegions.GetNextItem( region );
while ( (region.size < A) && !ListOfRegions.IsLastItem() )
{
ListOfRegions.GetNextItem( region );
}
while ( (region.size <= B) && !ListOfRegions.IsLastItem() )
{
pixelList = region.coordList;
pixelList.ResetList();
length = pixelList.LengthIs();
while ( length > 0 )
{
pixelList.GetNextItem(pixel);
i = pixel.x;
j = pixel.y;
(*image3).getPixelVal( i, j, val );
(*image).setPixelVal( i, j, val );
length--;
}
ListOfRegions.GetNextItem( region );
}
if ( (region.size <= B) )
{
pixelList = region.coordList;
pixelList.ResetList();
length = pixelList.LengthIs();
while ( length > 0 )
{
pixelList.GetNextItem(pixel);
i = pixel.x;
j = pixel.y;
(*image3).getPixelVal( i, j, val );
(*image).setPixelVal( i, j, val );
length--;
}
}
}
//Find and display orientations between A and B
if ( searchChoice == 'B' )
{
while ( !ListOfRegions.IsLastItem() )
{
ListOfRegions.GetNextItem( region );
if ( ( region.orientation >= A ) && ( region.orientation <= B ) )
{
pixelList = region.coordList;
pixelList.ResetList();
length = pixelList.LengthIs();
while ( length > 0 )
{
pixelList.GetNextItem(pixel);
i = pixel.x;
j = pixel.y;
(*image3).getPixelVal( i, j, val );
(*image).setPixelVal( i, j, val );
length--;
}
}
}
}
//Find and display eccentricities between A and B
if ( searchChoice == 'C' )
{
while ( !ListOfRegions.IsLastItem() )
{
ListOfRegions.GetNextItem( region );
if ( ( region.eccentricity >= A ) && ( region.eccentricity <= B ) )
{
pixelList = region.coordList;
pixelList.ResetList();
length = pixelList.LengthIs();
while ( length > 0 )
{
pixelList.GetNextItem(pixel);
i = pixel.x;
j = pixel.y;
(*image3).getPixelVal( i, j, val );
(*image).setPixelVal( i, j, val );
length--;
}
}
}
}
//Find and display mean intensities between A and B
if ( searchChoice == 'D' )
{
while ( !ListOfRegions.IsLastItem() )
{
ListOfRegions.GetNextItem( region );
if ( ( region.mean >= A ) && ( region.mean <= B ) )
{
pixelList = region.coordList;
pixelList.ResetList();
length = pixelList.LengthIs();
while ( length > 0 )
{
pixelList.GetNextItem(pixel);
i = pixel.x;
j = pixel.y;
(*image3).getPixelVal( i, j, val );
(*image).setPixelVal( i, j, val );
length--;
}
}
}
}
if ( searchChoice == 'E' )
{
coord query;
coord closest;
query.x = B;
query.y = A;
cout << endl << "Please enter the number, K, of neighbors you would like to find: ";
cin >> K;
QuadTree newTree( ListOfRegions );
cout << endl << "An X has been placed at the query point in the new image.";
cout << endl << "The centroids of the " << K << " nearest neighbors are at";
for( int index = 0; index < K; index++ )
{
newTree.FindClosest( query, closest );
cout << endl << "(" << closest.y << "," << closest.x << ")";
ListOfRegions.ResetList();
while ( !ListOfRegions.IsLastItem() )
{
ListOfRegions.GetNextItem( region );
//Place the closest galaxy in the new image
if ( ((region.centroid).x == closest.x ) && ((region.centroid).y == closest.y))
{
pixelList = region.coordList;
pixelList.ResetList();
length = pixelList.LengthIs();
while ( length > 0 )
{
pixelList.GetNextItem(pixel);
i = pixel.x;
j = pixel.y;
(*image3).getPixelVal( i, j, val );
(*image).setPixelVal( i, j, val );
length--;
}
}
}
}
//Output an X on the query point
(*image).setPixelVal( query.x, query.y, 255 );
(*image).setPixelVal( query.x + 1, query.y + 1, 255 );
(*image).setPixelVal( query.x - 1, query.y + 1, 255 );
(*image).setPixelVal( query.x - 1, query.y - 1, 255 );
(*image).setPixelVal( query.x + 1, query.y - 1, 255 );
(*image).setPixelVal( query.x + 2, query.y + 2, 255 );
(*image).setPixelVal( query.x - 2, query.y + 2, 255 );
(*image).setPixelVal( query.x - 2, query.y - 2, 255 );
(*image).setPixelVal( query.x + 2, query.y - 2, 255 );
}
break;
case 'N':
//Quit the program
continueProgram = false;
delete image;
break;
}
}
return (1);
}
// Supporting function implementation
int computeComponents(ImageType& inputImage, ImageType& outputImage, ImageType& image3, bool useBFS, SortedList& ListOfRegions)
{
int N, M, Q;
int inputPixelVal, outputPixelVal;
int length;
int gray;
int size;
coord centroid;
double orientation;
double eccentricity;
double mean;
double median;
int min;
int max;
point pixel;
double lamMin;
double lamMax;
double placeHolder;
int connComp, label;
nodeData region;
coordQ queueCoords;
coordS stackCoords;
//Create the queue and stack for searches
Queue queue( N*M );
Stack stack( N*M );
outputImage = inputImage;
inputImage.getImageInfo( N, M, Q );
//Clear the list of regions
ListOfRegions.MakeEmpty();
//Set the output image to white
for ( int i = 0; i < N; i++ )
{
for ( int j = 0; j < M; j++ )
{
outputImage.setPixelVal( i, j, 255 );
}
}
connComp=0; //Initializes region count to 0
// Iterate through original image and find unlabeled pixels
for (int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
{
inputImage.getPixelVal( i, j, inputPixelVal );
outputImage.getPixelVal( i, j, outputPixelVal );
//If the pixel is white in original and unlabeled in output, define its region
if( inputPixelVal == 255 && outputPixelVal == 255)
{
++connComp;
label += 7; // label
// IF the label goes above viewable shades, reset it to 0
if ( label >= 250 )
{
label = 0;
}
//Test the useBFS flag and use appropriate recursive search to label region
if ( useBFS )
{
(region.coordList).MakeEmpty();
mean = 0;
min = 255;
max = 0;
queueCoords.i = i;
queueCoords.j = j;
queue.enqueue( queueCoords );
pixel.x = i;
pixel.y = j;
(region.coordList).InsertItem(pixel);
findComponentBFS(inputImage, outputImage, label, queue, region);
//Get the size and centroid coords of the region
size = computeMoment( 0, 0, 0, 0, region.coordList );
centroid.x = computeMoment( 1, 0, 0, 0, region.coordList ) / size;
centroid.y = computeMoment( 0, 1, 0, 0, region.coordList ) / size;
//Get the lambda min and lambda max of the region
lamMin = 0.5*(computeMoment( 2, 0, centroid.x, centroid.y, region.coordList ) + computeMoment( 2, 0, centroid.x, centroid.y, region.coordList ) );
placeHolder = pow( computeMoment( 2, 0, centroid.x, centroid.y, region.coordList ) , 2) + pow( computeMoment( 0, 2, centroid.x, centroid.y, region.coordList ) , 2);
placeHolder = placeHolder - 2*( computeMoment( 2, 0, centroid.x, centroid.y, region.coordList ) * computeMoment( 0, 2, centroid.x, centroid.y, region.coordList ) );
placeHolder = placeHolder + 4*pow(computeMoment( 1, 1, centroid.x, centroid.y, region.coordList ), 2 );
placeHolder = pow( placeHolder, 0.5 );
lamMin = lamMin - 0.5*(placeHolder);
lamMax = 0.5*(computeMoment( 2, 0, centroid.x, centroid.y, region.coordList ) + computeMoment( 2, 0, centroid.x, centroid.y, region.coordList ) );
lamMax = lamMax + 0.5*(placeHolder);
// Get the orientation and eccentricity of the region
placeHolder = lamMax - computeMoment( 2, 0, centroid.x, centroid.y, region.coordList );
placeHolder = placeHolder / computeMoment( 1, 1, centroid.x, centroid.y, region.coordList );
orientation = atan( placeHolder );
placeHolder = lamMax/lamMin;
eccentricity = pow( placeHolder, 0.5 );
// Get the mean intensity, min, and max
length = (region.coordList).LengthIs();
(region.coordList).ResetList();
while ( length > 0 )
{
(region.coordList).GetNextItem( pixel );
image3.getPixelVal( pixel.x, pixel.y, gray );
if ( gray < min )
{
min = gray;
}
if ( gray > max )
{
max = gray;
}
mean+= gray;
length--;
}
mean = mean/size;
//Set these values to the region and add it to the list
region.size = size;
region.centroid = centroid;
region.orientation = orientation;
region.eccentricity = eccentricity;
region.mean = mean;
region.median = median;
region.min = min;
region.max = max;
ListOfRegions.InsertItem( region );
}
else
{
stackCoords.i = i;
stackCoords.j = j;
stack.push( stackCoords );
findComponentDFS(inputImage, outputImage, label, stack);
}
}
}
}
//Returns the number of regions
return connComp;
}
void findComponentBFS(ImageType& inputImage, ImageType& outputImage, int label, Queue& queue, nodeData& region)
{
coordQ Qcoords, newCoords;
int N, M, Q;
point pixel;
int val = 0, valtl = 0, valbl = 0, vall = 0, valt = 0, valb = 0, valr = 0, valtr = 0, valbr = 0;
int out, outtl, outbl, outl, outt, outb, outr, outtr, outbr;
//Test that queue is not empty
if (!queue.empty())
{
//Dequeue the next pixel to be labeled
Qcoords = queue.dequeue();
outputImage.getImageInfo( N, M, Q );
outputImage.setPixelVal( Qcoords.i, Qcoords.j, label ); // label this pixel
//Check that you will not be reaching outside of the image and obtain all surrounding pixel vals
if ( Qcoords.i-1 >= 0 )
{
if ( Qcoords.j-1 >= 0 )
{
outputImage.getPixelVal(Qcoords.i-1, Qcoords.j-1, outtl);
inputImage.getPixelVal(Qcoords.i-1, Qcoords.j-1, valtl);
}
if ( Qcoords.j+1 < M )
{
inputImage.getPixelVal(Qcoords.i-1, Qcoords.j+1, valbl);
outputImage.getPixelVal(Qcoords.i-1, Qcoords.j+1, outbl);
}
outputImage.getPixelVal(Qcoords.i-1, Qcoords.j, outl);
inputImage.getPixelVal(Qcoords.i-1, Qcoords.j, vall);
}
if ( Qcoords.i+1 < N )
{
if ( Qcoords.j-1 >= 0 )
{
inputImage.getPixelVal(Qcoords.i+1, Qcoords.j-1, valtr);
outputImage.getPixelVal(Qcoords.i+1, Qcoords.j-1, outtr);
}
if ( Qcoords.j+1 < M )
{
inputImage.getPixelVal(Qcoords.i+1, Qcoords.j+1, valbr);
outputImage.getPixelVal(Qcoords.i+1, Qcoords.j+1, outbr);