forked from new299/DS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.h
More file actions
1537 lines (1168 loc) · 43.8 KB
/
Copy pathImage.h
File metadata and controls
1537 lines (1168 loc) · 43.8 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
#ifndef DS_IMAGE_H
#define DS_IMAGE_H
#include "tiffio.h"
#include <iostream>
#include <math.h>
#include <vector>
#include <map>
#include "stringify.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <algorithm>
#define TIFFSetR(pixel, x) ((unsigned char *)pixel)[0] = x
#define TIFFSetG(pixel, x) ((unsigned char *)pixel)[1] = x
#define TIFFSetB(pixel, x) ((unsigned char *)pixel)[2] = x
#define TIFFSetA(pixel, x) ((unsigned char *)pixel)[3] = x
using namespace std;
double distance(double x1,double y1,double z1,
double x2,double y2,double z2) {
double x_d = (x1-x2)*(x1-x2);
double y_d = (y1-y2)*(y1-y2);
double z_d = (z1-z2)*(z1-z2);
double distance = sqrt(x_d+y_d+z_d);
return distance;
}
class Position {
public:
Position() {}
Position(size_t x,size_t y): m_x(x), m_y(y) {}
bool operator==(Position &other) {
if ((m_x == other.m_x) && (m_y == other.m_y)) return true;
return false;
}
bool operator<(const Position &other) const {
if(m_x < other.m_x) return true;
if(m_x > other.m_x) return false;
if(m_y < other.m_y) return true;
if(m_y > other.m_y) return false;
return false;
}
bool operator!=(Position &other) {
return !(*this == other);
}
string get_str() {
return stringify(m_x) + " " + stringify(m_y);
}
size_t m_x;
size_t m_y;
};
void rotate_point(double x,
double y,
double o_x,
double o_y,
double radians,
double &r_x,
double &r_y) {
//1. calculate theta
//tan theta = opp (y) / adj (x)
double dx = x - o_x;
double dy = y - o_y;
if(dx < 0) dx = 0-dx;
if(dy < 0) dy = 0-dy;
int q=1;
if((x > o_x) && (y >= o_y)) q = 1;
if((x >= o_x) && (y < o_y)) q = 2;
if((x < o_x) && (y <= o_y)) q = 3;
if((x <= o_x) && (y > o_y)) q = 4;
double theta;
if(q == 1) theta = atan(dy/dx);
if(q == 2) theta = atan(dx/dy);
if(q == 3) theta = atan(dy/dx);
if(q == 4) theta = atan(dx/dy);
double h = sqrt(dx*dx + dy*dy);
if((dy==0) || (dx==0)) theta = 0;
theta -= radians;
for(;theta < 0;) { theta = 0 - theta; theta = ((2*3.14)/4) - theta; q++; if(q==5) q=1;}
for(;theta > ((2*3.14)/4);) {theta -= ((2*3.14)/4); q--; if(q==0) q=4; }
if(q == 1) { r_y = (sin(theta) * h); r_x = (cos(theta) * h); }
if(q == 2) { r_x = (sin(theta) * h); r_y = (cos(theta) * h); }
if(q == 3) { r_y = (sin(theta) * h); r_x = (cos(theta) * h); }
if(q == 4) { r_x = (sin(theta) * h); r_y = (cos(theta) * h); }
if(q == 1) {r_x = o_x + r_x; r_y = o_y + r_y;}
if(q == 2) {r_x = o_x + r_x; r_y = o_y - r_y;}
if(q == 3) {r_x = o_x - r_x; r_y = o_y - r_y;}
if(q == 4) {r_x = o_x - r_x; r_y = o_y + r_y;}
}
map<uint32_t,int> grow_pixel_set(map<uint32_t,int> pixels,int delta) {
map<uint32_t,int> new_pixels;
for(map<uint32_t,int>::iterator i=pixels.begin();i != pixels.end();i++) {
int r1 = TIFFGetR(i->first);
int g1 = TIFFGetG(i->first);
int b1 = TIFFGetB(i->first);
for(int r_d=0-delta;r_d<(delta+1);r_d++) {
for(int g_d=0-delta;g_d<(delta+1);g_d++) {
for(int b_d=0-delta;b_d<(delta+1);b_d++) {
if(((r1+r_d) >= 0) && ((r1+r_d) < 256) &&
((g1+g_d) >= 0) && ((g1+g_d) < 256) &&
((b1+b_d) >= 0) && ((b1+b_d) < 256)) {
uint32_t v = 0xFFFFFFFF;
TIFFSetR(&v,(uint8_t) r1+r_d);
TIFFSetG(&v,(uint8_t) g1+g_d);
TIFFSetB(&v,(uint8_t) b1+b_d);
new_pixels[v]++;
}
}
}
}
}
return new_pixels;
}
class Image {
public:
Image() {
}
enum fragment_type { frag_type_arrow, frag_type_rectangle } ;
void load_tiff(string input_filename) {
m_image_data.clear();
TIFF* tif = TIFFOpen(input_filename.c_str(), "r");
if (tif) {
uint32 w, h;
size_t npixels;
uint32* raster;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
npixels = w * h;
m_width = w;
m_height = h;
raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));
if (raster != NULL) {
if (TIFFReadRGBAImageOriented(tif, w, h, raster,ORIENTATION_TOPLEFT, 0)) {
for(size_t n=0;n<npixels;n++) m_image_data.push_back(raster[n]);
}
_TIFFfree(raster);
}
TIFFClose(tif);
}
}
void load_tiff_8bit_grey(string input_filename) {
m_image_data.clear();
TIFF* tif = TIFFOpen(input_filename.c_str(), "r");
if (tif) {
uint32 w, h;
size_t npixels;
uint32* raster;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &w);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &h);
npixels = w * h;
m_width = w;
m_height = h;
raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));
if (raster != NULL) {
if (TIFFReadRGBAImageOriented(tif, w, h, raster,ORIENTATION_TOPLEFT, 0)) {
for(size_t n=0;n<npixels;n++) {
//m_image_data.push_back((255-raster[n])*(256*256*256));
m_image_data.push_back((raster[n])*(256*256*256));
}
}
_TIFFfree(raster);
}
TIFFClose(tif);
}
}
void save_tiff_rgb(string output_filename) {
TIFF *output_image;
// Open the TIFF file
if((output_image = TIFFOpen(output_filename.c_str(), "w")) == NULL){
cerr << "Unable to write tif file: " << output_filename << endl;
}
// We need to set some values for basic tags before we can add any data
TIFFSetField(output_image, TIFFTAG_IMAGEWIDTH, m_width);
TIFFSetField(output_image, TIFFTAG_IMAGELENGTH, m_height);
TIFFSetField(output_image, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(output_image, TIFFTAG_SAMPLESPERPIXEL, 4);
TIFFSetField(output_image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(output_image, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
TIFFSetField(output_image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
// Write the information to the file
TIFFWriteEncodedStrip(output_image, 0, &m_image_data[0], m_width*m_height * 4);
// Close the file
TIFFClose(output_image);
}
void save_tiff_grey_32bit(string output_filename) {
TIFF *output_image;
// Open the TIFF file
if((output_image = TIFFOpen(output_filename.c_str(), "w")) == NULL){
cerr << "Unable to write tif file: " << output_filename << endl;
}
// We need to set some values for basic tags before we can add any data
TIFFSetField(output_image, TIFFTAG_IMAGEWIDTH, m_width);
TIFFSetField(output_image, TIFFTAG_IMAGELENGTH, m_height);
TIFFSetField(output_image, TIFFTAG_BITSPERSAMPLE, 32);
TIFFSetField(output_image, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(output_image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(output_image, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
TIFFSetField(output_image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
// Write the information to the file
TIFFWriteEncodedStrip(output_image, 0, &m_image_data[0], m_width*m_height * 4);
// Close the file
TIFFClose(output_image);
}
void save_tiff_grey_8bit(string output_filename) {
TIFF *output_image;
// Open the TIFF file
if((output_image = TIFFOpen(output_filename.c_str(), "w")) == NULL){
cerr << "Unable to write tif file: " << output_filename << endl;
}
// We need to set some values for basic tags before we can add any data
TIFFSetField(output_image, TIFFTAG_IMAGEWIDTH, m_width);
TIFFSetField(output_image, TIFFTAG_IMAGELENGTH, m_height);
TIFFSetField(output_image, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(output_image, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(output_image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(output_image, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
TIFFSetField(output_image, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
// convert data to 8bit
vector<uint8_t> data;
for(size_t n=0;n<m_image_data.size();n++) {
data.push_back(255-(m_image_data[n]/(256*256*256)));
}
// Write the information to the file
TIFFWriteEncodedStrip(output_image, 0, &data[0], m_width*m_height);
// Close the file
TIFFClose(output_image);
}
void make_greyscale(uint32_t bg_val=0) {
for(size_t n=0;n<m_image_data.size();n++) {
double r = TIFFGetR(m_image_data[n]);
double g = TIFFGetG(m_image_data[n]);
double b = TIFFGetB(m_image_data[n]);
if(m_image_data[n] != bg_val) {
double grey = (0.3*r) + (0.59*g) + (0.11*b); // See http://en.wikipedia.org/wiki/Grayscale
if(grey == bg_val) grey++;
m_image_data[n] = grey * 256 * 256 * 256;
}
}
}
bool is_on_image(size_t x,size_t y) {
if(x < 0) return false;
if(y < 0) return false;
if((x < m_width) && (y < m_height)) return true;
return false;
}
uint32_t get(size_t x,size_t y) {
return m_image_data[(y*m_width)+x];
}
uint32_t get(Position p) {
return get(p.m_x,p.m_y);
}
void set(size_t x,size_t y,uint32_t value) {
m_image_data[(y*m_width)+x] = value;
}
void set(Position p,uint32_t value) {
set(p.m_x,p.m_y,value);
}
map<uint32_t,int> random_walk_collect(size_t x, ///< Starting x position
size_t y, ///< Starting y position
size_t walk_distance, ///< How long should we continue to walk.
double max_delta ///< Maximum delta while performing walk.
) {
// This method performs a random walk around the image from a given starting point.
// It collects pixels as it goes, so as to extract a collection of background values.
map<uint32_t,int> pixels_found;
for(size_t n=0;n<walk_distance;n++) {
int x_delta;
int y_delta;
x_delta = ((int)(rand()%3)) - 1;
y_delta = ((int)(rand()%3)) - 1;
for(;!is_on_image(x+x_delta,y+y_delta);) {
x_delta = ((int)(rand()%3)) - 1;
y_delta = ((int)(rand()%3)) - 1;
}
double r1 = TIFFGetR(get(x,y));
double g1 = TIFFGetG(get(x,y));
double b1 = TIFFGetB(get(x,y));
double r2 = TIFFGetR(get(x+x_delta,y+y_delta));
double g2 = TIFFGetG(get(x+x_delta,y+y_delta));
double b2 = TIFFGetB(get(x+x_delta,y+y_delta));
double d = distance(r1,g1,b1,r2,g2,b2);
if(d < max_delta) {
pixels_found[get(x+x_delta,y+y_delta)]++;
x = x+x_delta;
y = y+y_delta;
}
}
return pixels_found;
}
void set_pixels(map<uint32_t,int> values,uint32_t set_val,double delta) {
for(size_t n=0;n<m_image_data.size();n++) {
if(values.find(m_image_data[n]) != values.end()) m_image_data[n] = set_val; else {
if(delta != 0)
for(map<uint32_t,int>::iterator i=values.begin();i != values.end();i++) {
double d = distance(TIFFGetR(i->first),TIFFGetG(i->first),TIFFGetB(i->first),
TIFFGetR(m_image_data[n]),TIFFGetG(m_image_data[n]),TIFFGetB(m_image_data[n]));
if(d < delta) m_image_data[n] = set_val;
}
}
}
}
void not_set_pixels(map<uint32_t,int> values,uint32_t set_val,double delta) {
for(size_t n=0;n<m_image_data.size();n++) {
if(values.find(m_image_data[n]) == values.end()) m_image_data[n] = set_val; else {
if(delta != 0)
for(map<uint32_t,int>::iterator i=values.begin();i != values.end();i++) {
double d = distance(TIFFGetR(i->first),TIFFGetG(i->first),TIFFGetB(i->first),
TIFFGetR(m_image_data[n]),TIFFGetG(m_image_data[n]),TIFFGetB(m_image_data[n]));
if(d > delta) m_image_data[n] = set_val;
}
}
}
}
vector<Position> get_all_adjacent(size_t x,size_t y,uint32_t bg_val) {
vector<Position> adjacent_positions;
for(int d_x=-1;d_x<2;d_x++) {
for(int d_y=-1;d_y<2;d_y++) {
if(!((d_x == 0) && (d_y == 0))) {
if(is_on_image(x+d_x,y+d_y)) {
if(get(x+d_x,y+d_y) != bg_val) { adjacent_positions.push_back(Position(x+d_x,y+d_y)); }
}
}
}
}
return adjacent_positions;
}
// gets all adjacent pixels but NOT those diagonally connected
vector<Position> get_all_adjacent_nodiag_val(size_t x,size_t y,uint32_t bg_val,uint32_t frag_val) {
vector<Position> adjacent_positions;
if(is_on_image(x+1,y ) && (get(x+1,y ) == frag_val)) adjacent_positions.push_back(Position(x+1,y ));
if(is_on_image(x-1,y ) && (get(x-1,y ) == frag_val)) adjacent_positions.push_back(Position(x-1,y ));
if(is_on_image(x ,y+1) && (get(x ,y+1) == frag_val)) adjacent_positions.push_back(Position(x ,y+1));
if(is_on_image(x ,y-1) && (get(x ,y-1) == frag_val)) adjacent_positions.push_back(Position(x ,y-1));
return adjacent_positions;
}
// gets all adjacent pixels but NOT those diagonally connected
vector<Position> get_all_adjacent_nodiag(size_t x,size_t y,uint32_t bg_val) {
vector<Position> adjacent_positions;
if(is_on_image(x+1,y ) && (get(x+1,y ) != bg_val)) adjacent_positions.push_back(Position(x+1,y ));
if(is_on_image(x-1,y ) && (get(x-1,y ) != bg_val)) adjacent_positions.push_back(Position(x-1,y ));
if(is_on_image(x ,y+1) && (get(x ,y+1) != bg_val)) adjacent_positions.push_back(Position(x ,y+1));
if(is_on_image(x ,y-1) && (get(x ,y-1) != bg_val)) adjacent_positions.push_back(Position(x ,y-1));
return adjacent_positions;
}
// gets all adjacent pixels but NOT those diagonally connected (BGVALS)
vector<Position> get_all_adjacent_nodiag_bg(size_t x,size_t y,uint32_t bg_val) {
vector<Position> adjacent_positions;
if(is_on_image(x+1,y ) && (get(x+1,y ) == bg_val)) adjacent_positions.push_back(Position(x+1,y ));
if(is_on_image(x-1,y ) && (get(x-1,y ) == bg_val)) adjacent_positions.push_back(Position(x-1,y ));
if(is_on_image(x ,y+1) && (get(x ,y+1) == bg_val)) adjacent_positions.push_back(Position(x ,y+1));
if(is_on_image(x ,y-1) && (get(x ,y-1) == bg_val)) adjacent_positions.push_back(Position(x ,y-1));
return adjacent_positions;
}
void zero_image() {
m_image_data = vector<uint32_t>(m_width*m_height,0);
}
Image get_region_image(size_t min_x,size_t max_x,size_t min_y,size_t max_y) {
Image i;
i.m_width = max_x-min_x;
i.m_height = max_y-min_y;
i.zero_image();
for(size_t x=min_x;x<max_x;x++) {
for(size_t y=min_y;y<max_y;y++) {
i.set(x-min_x,y-min_y,get(x,y));
}
}
return i;
}
// Extracts the contiguous region representing this fragment.
// This method operates in 2 parts:
// 1. Find the region extreme values, cut this region out and creating a new image.
// 2. Remove the region from the image
Image grow_and_extract(size_t x,size_t y,int32_t bg_val) {
//cout << "grow and extract: " << x << "," << y << endl;
Image this_copy = *this;
this_copy.zero_image();
size_t min_x = 10000000000;
size_t max_x = 0;
size_t min_y = 10000000000;
size_t max_y = 0;
this_copy.set(x,y,get(x,y));
set(x,y,bg_val);
// Find region extremities
vector<Position> positions = get_all_adjacent(x,y,bg_val);
if(positions.size() == 0) {Image i; i.m_width=0; i.m_height=0; return i;} // It was a single pixel image, which breaks the code below, return a zero sized image...
size_t i=0;
for(;positions.size() != 0;) {
vector<Position> o_pos;
for(size_t n=0;n<positions.size();n++) {
if(positions[n].m_x < min_x) min_x = positions[n].m_x;
if(positions[n].m_x > max_x) max_x = positions[n].m_x;
if(positions[n].m_y < min_y) min_y = positions[n].m_y;
if(positions[n].m_y > max_y) max_y = positions[n].m_y;
o_pos.push_back(Position(positions[n].m_x,positions[n].m_y));
if(get(positions[n].m_x,positions[n].m_y) != bg_val) {
this_copy.set(positions[n].m_x,positions[n].m_y,get(positions[n].m_x,positions[n].m_y));
}
set(positions[n].m_x,positions[n].m_y,bg_val);
}
positions.clear();
for(size_t n=0;n<o_pos.size();n++) {
vector<Position> p = get_all_adjacent(o_pos[n].m_x,o_pos[n].m_y,bg_val);
// This ensures that the positions don't get used again, by setting them to bgval
for(size_t i=0;i<p.size();i++) {
if(get(p[i].m_x,p[i].m_y) != bg_val) {
this_copy.set(p[i].m_x,p[i].m_y,get(p[i].m_x,p[i].m_y));
}
set(p[i].m_x,p[i].m_y,bg_val);
}
positions.insert(positions.begin(),p.begin(),p.end());
}
if((i%1000 == 0) && (i!=0)) cerr << "Growing region, current bounds: " << min_x << "," << max_x << " " << min_y << "," << max_y << endl;
i++;
}
return this_copy.get_region_image(min_x,max_x,min_y,max_y);
}
Image trim(uint32_t bg_val) {
size_t min_x = 10000000000;
size_t max_x = 0;
size_t min_y = 10000000000;
size_t max_y = 0;
bool nothing=true;
for(size_t x=0;x<m_width;x++) {
for(size_t y=0;y<m_height;y++) {
if(get(x,y) != bg_val) {
if(x<min_x) min_x = x;
if(x>max_x) max_x = x;
if(y<min_y) min_y = y;
if(y>max_y) max_y = y;
nothing = false;
}
}
}
if(nothing==true) {
min_x = 0;
max_x = 0;
min_y = 0;
max_y = 0;
}
return get_region_image(min_x,max_x,min_y,max_y);
}
/// Performs image segmentation, contiguous regions that are not "value" are placed in the same segment.
vector<Image> segment_on_value(uint32_t value) {
Image this_image = *this; // create a copy of ourselves, this segmentation will be destructive.
vector<Image> all_fragments;
for(size_t x=0;x<m_width;x++) {
for(size_t y=0;y<m_height;y++) {
if(((y*m_width)+x)%1000 == 0) cerr << "Current segmentation position: " << x << "," << y << endl;
if(this_image.get(x,y) != value) {
//cout << "DBG get value was non bg" << endl;
Image result = this_image.grow_and_extract(x,y,value);
all_fragments.push_back(result);
//if(result.pixel_count() > 20) {
cerr << "Identified fragment, size: " << result.pixel_count() << " total: " << all_fragments.size() << endl;
// result.save_tiff_rgb(string("lastfrag.tif"));
//}
}
}
}
return all_fragments;
}
size_t pixel_count() {
return m_image_data.size();
}
// Return true if the cluster is greater than size
bool cluster_greater(size_t x,size_t y,uint32_t bg_val,int size) {
Position p(x,y);
vector<Position> pixels;
pixels.push_back(p);
//cout << "cluster greater: " << x << " " << y << endl;
size_t old_pixels_size = 1000000000;
for(;pixels.size() != old_pixels_size;) {
vector<Position> all_res;
for(size_t n=0;n<pixels.size();n++) {
vector<Position> res = get_all_adjacent_nodiag(pixels[n].m_x,pixels[n].m_y,bg_val);
all_res.insert(all_res.begin(),res.begin(),res.end());
}
all_res.insert(all_res.begin(),pixels.begin(),pixels.end());
//for(size_t n=0;n<all_res.size();n++) cout << "all_res: " << all_res[n].m_x << " " << all_res[n].m_y << endl;
sort(all_res.begin(),all_res.end());
//for(size_t n=0;n<all_res.size();n++) cout << "all_srt: " << all_res[n].m_x << " " << all_res[n].m_y << endl;
vector<Position>::iterator iend = unique(all_res.begin(),all_res.end());
vector<Position> res_unique;
res_unique.insert(res_unique.begin(),all_res.begin(),iend);
// for(vector<Position>::iterator i=all_res.begin();i<all_res.end();i++) res_unique.push_back(*i);
old_pixels_size = pixels.size();
pixels = res_unique;
//for(size_t n=0;n<pixels.size();n++) cout << "pixel: " << pixels[n].m_x << " " << pixels[n].m_y << endl;
//cout << "size: " << pixels.size() << endl;
if(pixels.size() > size) return true;
}
return false;
}
// Return true if the cluster is greater than size
bool frag_cluster_greater(size_t x,size_t y,uint32_t frag_val,uint32_t bg_val,int size) {
Position p(x,y);
vector<Position> pixels;
pixels.push_back(p);
//cout << "cluster greater: " << x << " " << y << endl;
size_t old_pixels_size = 1000000000;
for(;pixels.size() != old_pixels_size;) {
vector<Position> all_res;
for(size_t n=0;n<pixels.size();n++) {
vector<Position> res = get_all_adjacent_nodiag_val(pixels[n].m_x,pixels[n].m_y,bg_val,frag_val);
all_res.insert(all_res.begin(),res.begin(),res.end());
}
all_res.insert(all_res.begin(),pixels.begin(),pixels.end());
//for(size_t n=0;n<all_res.size();n++) cout << "all_res: " << all_res[n].m_x << " " << all_res[n].m_y << endl;
sort(all_res.begin(),all_res.end());
//for(size_t n=0;n<all_res.size();n++) cout << "all_srt: " << all_res[n].m_x << " " << all_res[n].m_y << endl;
vector<Position>::iterator iend = unique(all_res.begin(),all_res.end());
vector<Position> res_unique;
res_unique.insert(res_unique.begin(),all_res.begin(),iend);
// for(vector<Position>::iterator i=all_res.begin();i<all_res.end();i++) res_unique.push_back(*i);
old_pixels_size = pixels.size();
pixels = res_unique;
//for(size_t n=0;n<pixels.size();n++) cout << "pixel: " << pixels[n].m_x << " " << pixels[n].m_y << endl;
//cout << "size: " << pixels.size() << endl;
if(pixels.size() > size) return true;
}
return false;
}
void fill_hole(uint32_t bg_val) {
for(int x=0;x<m_width;x++) {
for(int y=0;y<m_height;y++) {
if(get(x,y) == bg_val) {
bool notisolated = frag_cluster_greater(x,y,bg_val,bg_val,20);
if(notisolated == false) {
//find a near pixel
uint32_t adjcol=bg_val;
int cx = x;
int cy = y;
for(int n=0;n<1000;n++) {
cx += (rand()%3)-1;
// cy += (rand()%3)-1; // this would be more accurate, but due to features of this image, only x may work better (test DARPA dataset)
if(is_on_image(cx,cy)) adjcol = get(cx,cy);
if(adjcol != bg_val) break;
}
set(x,y,adjcol);
}
}
}
}
}
void remove_fragments(uint32_t frag_col,uint32_t bg_val,int fragsize=30) {
for(int x=0;x<m_width;x++) {
for(int y=0;y<m_height;y++) {
if(get(x,y) == frag_col) {
vector<Position> all_p;
bool notisolated = frag_cluster_greater(x,y,frag_col,bg_val,fragsize); // remove all pixel clusters < N pixels in size.
if(notisolated == false) set(x,y,bg_val);
}
}
}
}
void remove_isolated_pixels(uint32_t bg_val) {
for(int x=0;x<m_width;x++) {
for(int y=0;y<m_height;y++) {
if(get(x,y) != bg_val) {
vector<Position> all_p;
bool notisolated = cluster_greater(x,y,bg_val,100); // remove all pixel clusters < 10 pixels in size.
if(notisolated == false) set(x,y,bg_val);
}
}
}
}
void remove_hangs(uint32_t bg_val) {
for(int x=0;x<(((int)m_width)-1);x++) {
for(int y=0;y<(((int)m_height)-1);y++) {
uint32_t sq = get_square(x,y,bg_val);
if((sq == 6) || (sq == 9)) {
set(x ,y,bg_val);
set(x+1,y,bg_val);
set(x ,y+1,bg_val);
set(x+1,y+1,bg_val);
}
}
}
}
Image rotate(size_t o_x,size_t o_y,double radians,uint32_t bg_val) {
Image out = rotate_source(o_x,o_y,radians,bg_val);
out.fill_gaps(bg_val);
return out;
}
Image rotate_source(size_t o_x,size_t o_y,double radians,uint32_t bg_val) {
Image new_image; // image to copy data in to
size_t pad;
if(m_width > m_height) pad = m_width;
else pad = m_height;
new_image.m_width = pad*4;
new_image.m_height = pad*4;
new_image.zero_image();
size_t x_min = 1000000000;
size_t x_max = 0;
size_t y_min = 1000000000;
size_t y_max = 0;
for(uint32_t cx=0;cx<m_width;cx++) {
for(uint32_t cy=0;cy<m_height;cy++) {
double r_x, r_y;
if(get(cx,cy) != bg_val) {
rotate_point(cx,cy,o_x,o_y,radians,r_x,r_y);
size_t x_pos = r_x+(2*pad);
size_t y_pos = r_y+(2*pad);
new_image.set(x_pos,y_pos,get(cx,cy));
if(x_pos > x_max) x_max = x_pos;
if(x_pos < x_min) x_min = x_pos;
if(y_pos > y_max) y_max = y_pos;
if(y_pos < y_min) y_min = y_pos;
}
}
}
return new_image.get_region_image(x_min,x_max,y_min,y_max);
//return new_image;
}
void fill_gaps(uint32_t bg_val) {
for(size_t x=0;x<m_width;x++) {
for(size_t y=0;y<m_height;y++) {
if(get(x,y) == bg_val) {
if(is_on_image(x-1,y-1) && is_on_image(x+1,y+1)) {
vector<uint32_t> adjs;
adjs.push_back(get(x+1,y));
adjs.push_back(get(x-1,y));
adjs.push_back(get(x,y+1));
adjs.push_back(get(x,y-1));
int perform_set=0;
for(size_t n=0;n<adjs.size();n++) if(adjs[n] == bg_val) perform_set++;
if(perform_set < 2) {
sort(adjs.begin(),adjs.end());
set(x,y,adjs[2]);
}
}
}
}
}
}
Image rotate_dest(size_t o_x,size_t o_y,double radians,uint32_t bg_val) {
Image new_image; // image to copy data in to
int32_t pad;
if(m_width > m_height) pad = m_width;
else pad = m_height;
new_image.m_width = pad*4;
new_image.m_height = pad*4;
new_image.zero_image();
for(int32_t cx=0-pad;cx<pad;cx++) {
for(int32_t cy=0-pad;cy<pad;cy++) {
double r_x, r_y;
rotate_point(cx,cy,o_x,o_y,0-radians,r_x,r_y);
if(is_on_image(r_x,r_y)) new_image.set(cx+pad,cy+pad,get(r_x,r_y));
}
}
return new_image;
}
bool is_line_concave(size_t y,uint32_t bg_val) {
bool was_inside = false;
bool went_outside = false;
bool line_concave = false;
// Clear out any isolated pixels, they are probably artifacts TODO: THIS COULD BE A SEPERATE IMAGE CLEANING FUNCTION...
for(size_t x=1;x<(m_width-1);x++) {
int pix[3];
pix[0] = get(x-1,y);
pix[1] = get(x ,y);
pix[2] = get(x+1,y);
for(size_t n=0;n<3;n++) if(pix[n] != bg_val) pix[n] = 1; else pix[n] = 0;
if((pix[1] != pix[0]) && (pix[1] != pix[2])) { set(x,y,get(x-1,y));}
}
for(size_t x=0;x<m_width;x++) { if(get(x,y) == bg_val) cout << "0"; else cout << "1"; }
cout << endl;
for(size_t x=0;x<m_width;x++) {
if(get(x,y) != bg_val) {
if(was_inside && went_outside) {
line_concave = true;
}
}
if(get(x,y) != bg_val) was_inside = true;
if(get(x,y) == bg_val) {
if(was_inside) { went_outside = true; }
}
}
if(line_concave) cout << "concave" << endl; else cout << "not concave" << endl;
return line_concave;
}
int top_is_concave(uint32_t bg_val) {
cout << "CHECKING TOP" << endl;
size_t concave_count = 0;
for(size_t y=0;y<(m_height/8);y++) {
bool line_concave = is_line_concave(y,bg_val);
if(line_concave) { concave_count++; }
}
cout << "TOP concave_count: " << concave_count << endl;
return concave_count;
}
int bottom_is_concave(uint32_t bg_val) {
cout << "CHECKING BOTTOM" << endl;
size_t concave_count = 0;
for(size_t y=(m_height-1);y>=(m_height-(m_height/8));y--) {
bool line_concave = is_line_concave(y,bg_val);
if(line_concave) concave_count++;
}
cout << "BOTTOM concave_count: " << concave_count << endl;
return concave_count;
}
Image orientate(Image::fragment_type f_type,uint32_t bg_val) {
size_t min_width=100000000;
double min_width_radians = 0;
for(double r=0;r<(2*3.141);r+=0.01) {
Image i2 = rotate(m_width/2,m_height/2,r,0);
if(i2.m_width < min_width) {
min_width = i2.m_width;
min_width_radians = r;
}
}
cout << "Orientated at radians: " << min_width_radians << endl;
Image rotated = rotate(m_width/2,m_height/2,min_width_radians,0);
// if(!(rotated.top_is_concave(bg_val) && rotated.bottom_is_convex(bg_val))) {
int top_conv_count = rotated.top_is_concave (bg_val);
int bot_conv_count = rotated.bottom_is_concave(bg_val);
if(bot_conv_count > top_conv_count) {
cerr << "Fragment upside-down" << endl;
rotated.rotate180();
}
return rotated;
}
void swap_pixel(size_t x1,size_t y1,
size_t x2,size_t y2) {
uint32_t t = get(x1,y1);
set(x1,y1,get(x2,y2));
set(x2,y2,t);
}
// Inplace 180 degree rotation.
void rotate180() {
size_t h;
if((m_height%2) == 1) {
h = (m_height/2)-1;
size_t ymid = ((m_height-1)/2);
for(size_t x=0;x<(m_width/2);x++) {
swap_pixel(x,ymid,(m_width-x-1),ymid);
}
} else h = (m_height/2)-1;
for(size_t x=0;x<m_width;x++) {
for(size_t y=0;y<=h;y++) {
swap_pixel(x,y,m_width-x-1,m_height-y-1);
}
}
}
Image erode(uint32_t bg_val) {
Image i = *this;
for(size_t x=1;x<(m_width-1);x++) {
for(size_t y=1;y<(m_height-1);y++) {
if(get(x,y) != bg_val) {
if(!((get(x+1,y ) != bg_val) && (get(x-1,y ) != bg_val) &&
(get(x ,y+1) != bg_val) && (get(x ,y-1) != bg_val))) {
i.set(x,y,bg_val);
}
}
}
}
return i;
}