This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment.c
7915 lines (6911 loc) · 222 KB
/
segment.c
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
/*
Cell-ID is intended to identify cells in images and to calculate a
number of statistics, including statistics derived from corresponding
fluorescence images.
Copyright (C) 2005 Andrew Gordon
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Andrew Gordon can be contacted at
Molecular Sciences Institute
2168 Shattuck Ave, 2nd Floor
Berkeley, CA 94704
**********************************************************************
Start-copyright-notice-for-libtiff
Libtiff software is used by Cell-ID for some of the reading in of
TIF image file data and also for creating new TIF files. Libtiff is
available at http://www.remotesensing.org/libtiff/. The libtiff software
was written by Sam Leffler while working for Silicon Graphics and
contains the following copyright notice:
"Copyright (c) 1988-1997 Sam Leffler
Copyright (c) 1991-1997 Silicon Graphics, Inc.
Permission to use, copy, modify, distribute, and sell this software and
its documentation for any purpose is hereby granted without fee, provided
that (i) the above copyright notices and this permission notice appear in
all copies of the software and related documentation, and (ii) the names
of Sam Leffler and Silicon Graphics may not be used in any advertising or
publicity relating to the software without the specific, prior written
permission of Sam Leffler and Silicon Graphics.
THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE."
End-copyright-notice-for-Libtiff
*********************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "fit.h"
#include "tif_routines.h"
#include "segment.h"
#include "nums.h"
#include "fft_stats.h"
#include "image_type.h"
#include "split_and_overlap.h"
#include "contiguous.h"
#include "fft.h"
#include "fl_dist.h"
#include "parameters.h"
#include "flatten.h"
#define pi 3.1415926535
#define twopi 2.0*3.1415926535
#define nbins_for_cut_calculation 1000
// #define I_over_U_for_match 0.2--> now in parameters.txt
#define isect_max 1000
//#define min_pixels_per_cell 75
#define min_pixels_per_cell_hard 100
#define ccd_floor 128.0
#define ccd_floorp1 129.0
int xmax,ymax;
int xmax_ymax;
//Global Arrays
float *c=NULL; //Bright field image array
float *fl=NULL; //Fluorescence image array
float *third_image=NULL;
int *d=NULL;
int *work_array=NULL;
void sort_list_by_fl(struct point *);
void check_mem(void);
int total_overlap_of_all_cells(int,int);
struct point *point_malloc(void);
void point_free(struct point *);
void point_list_free(struct point *);
void point_list_adjust(struct point *,int,int);
struct point *fix_spirals(struct point *);
struct point *clean_up_tails(struct point *);
struct point *find_interior_points(struct point *);
float area_of_cell(struct point *);
float circumference_of_cell(struct point *);
struct point *circularize_points(struct point *, float);
void calculate_cut(void);
float cut,cut_high,cut_low,mid; //Cuts from gaussian fit to histogram
//of pixels.
float max_d_over_s(struct point *, struct point **, struct point **);
struct point *copy_cell_for_split_regions(struct point *,int);
void neighbor_interpolation(int,int,float,float,float *,float *,float *);
int get_median(int,int,int,float *,float *,float *,float *);
int get_median_deviations(int,int,float,float *,float *);
int combine_cells_in_cs_array(int,int,int);
int mark_distance_from_boundary(struct point *, int);
#define n_points_r_vs_theta 128
float r_vs_theta[n_points_r_vs_theta];
void r_vs_theta_from_boundary(struct point*);
struct point *boundary_from_r_vs_theta(void);
void fill_cos_sin_arrays(void);
double dtheta;
double *cos_theta=NULL;
double *sin_theta=NULL;
float centroid_x,centroid_y;
void statistics_from_r_vs_theta(int,
float *,float *,float *,float *,
float *,float *);
float smallest_circumference;
//Calculate the mode of the background for each of the time points
#define max_time 50000
float back_cur=0.0;
float back_pixels[max_time];
float back_pixels_1[max_time];
float back_pixels_2[max_time];
float total_fluorescence[max_time];
float total_area[max_time];
//Pointers to save the pixels which constitute the largest background.
//These pixels will be used to calculate the background level in the
//fluorescence image, and they'll be calculated from the BF image.
unsigned short int *pixels_for_background_x=NULL;
unsigned short int *pixels_for_background_y=NULL;
int n_pixels_for_background;
//Use over[] array to record where the cells are for overlap calculation.
//Put it up here so don't have to keep re-allocating the space. Will
//calloc() space for it below, so it must be initialized to NULL otherwise
//space won't be allocated for it.
unsigned char *over=NULL;
int over_array_max=0;
void update_overlap_value(void);
void fill_overlap_array_with_point_list(struct point *);
void fill_overlap_array_with_point_list_offset(struct point *,int,int);
unsigned char overlap_value=0;
//Make maximum overlap_value the maximum for a signed short (ie,
// 7fff, ie, assume 2-bytes. This should be by far large enough
//since at most it needs to be larger than the number of cells found
//in any given image (0x7fff=32767). (On the other hand, an
//unsigned char (max=255) would probably be too small sometimes.)
#define overlap_value_max 254
//We define a "cell" as a closed set of segments.
#define max_cells 200000
//struct point *vacuole[max_cells];
struct point *boundary[max_cells];
struct point *interior[max_cells];
struct point *boundary_p1[max_cells];
struct point *interior_p1[max_cells];
struct point *boundary_m1[max_cells];
struct point *interior_m1[max_cells];
struct point *boundary_m2[max_cells];
struct point *interior_m2[max_cells];
struct point *boundary_m3[max_cells];
struct point *interior_m3[max_cells];
int save_realign_offset_x[max_cells];
int save_realign_offset_y[max_cells];
void re_align_cell(struct point *,int *,int *);
float calculate_volume_cone_old(struct point *);
int calculate_volume(struct point *,struct point *,
float *,float *,
float *,float *,
float *,float *,
float *,float *);
float integrate_gaussian_from_0_to_x(float,float);
double *gaussian_integral=NULL;
int gaussian_integral_nbins=5000;
double max_gaussian_integral=5.0;
float gaussian_integral_bin_width;
//For "local background correction"
struct point *boundary_p5[max_cells];
float back_p5[max_cells];
float pixels_back_p5[max_cells];
float pixels_total_p5[max_cells];
struct point *boundary_phalfminor[max_cells];
float back_phalfminor[max_cells];
float pixels_back_phalfminor[max_cells];
float pixels_total_phalfminor[max_cells];
float mean_x[max_cells],mean_y[max_cells];
float fft_stat[max_cells];
float vol_rotation[max_cells];
float vol_cone[max_cells];
float vol_sphere[max_cells];
float surface_area[max_cells];
float vol_eff_1[max_cells];
float vol_eff_2[max_cells];
float vol_eff_3[max_cells];
float vol_eff_4[max_cells];
float vol_eff_5[max_cells];
float vol_eff_6[max_cells];
float circumference[max_cells];
float major_axis1[max_cells],minor_axis1[max_cells];
float major_axis2[max_cells],minor_axis2[max_cells];
int n_points[max_cells];
float fluorescence[max_cells];
float fluorescence_p1[max_cells];
float fluorescence_m1[max_cells];
float fluorescence_m2[max_cells];
float fluorescence_m3[max_cells];
float med_fl[max_cells];
float mad_fl[max_cells];
float mcd_fl[max_cells];
float mcd_rms_fl[max_cells];
float vacuole_area[max_cells];
float vacuole_fl[max_cells];
float cell_area[max_cells];
float cell_area_p1[max_cells];
float cell_area_m1[max_cells];
float cell_area_m2[max_cells];
float cell_area_m3[max_cells];
int x_nucl[max_cells];
int y_nucl[max_cells];
#define nucleus_distribution_types 6 //V1.4.5 modified from 8 to 6
float fl_nucleus[max_cells][nucleus_distribution_types];
float area_nucleus[max_cells][nucleus_distribution_types];
float fl_nucleus_from_search[max_cells][nucleus_distribution_types];
struct point *p_nuclear_center[max_cells];
int fret_copy_type[max_cells];
//Some variables for the pixel-by-pixel comparison of current
//image to the previous
float *flprev=NULL;
float *cur_prev=NULL;;
int first_cur_prev_comparison=0; //A flag for first time
float diff_fl_prev(int,int); //For calculating offset
void calculate_global_stats_from_interior_and_boundary();
float pos_sig_mean_x[max_cells];
float pos_sig_mean_y[max_cells];
float pos_sig_mean_r[max_cells];
float pos_sig_rms_r[max_cells];
float neg_sig_mean_x[max_cells];
float neg_sig_mean_y[max_cells];
float neg_sig_mean_r[max_cells];
float neg_sig_rms_r[max_cells];
int location_in_cs_array[max_cells];
int pixel_list[max_cells];
int n_found=0;
int n_before_fret_copy=0;
//For connection between bottom and top of split images
float fret_mx,fret_my,fret_bx,fret_by;
int *fret_labels=NULL;
#define fret_offset 1000
#define lower_fret_region 1
#define higher_fret_region 2
int fret_region_use;
int have_fret_image=0;
#define n_fft_max 100
#define max_offsets 1000
int nucleus_offset_x[max_offsets];
int nucleus_offset_y[max_offsets];
int nucleus_n_offset=0;
struct blob {
int index; //A unique number to label this cell list
int flag; //for whatever use
float x,y; //mean of interior points to cell
float a; //Area of interior of cell
int n; //Number of interior pixels to cell
float fluor; //Sum of fluorescence from fluorescence picture
float fluor_p1; //Sum of fluorescence from fluorescene picture
float fluor_m1; //Sum of fluorescence from fluorescence picture
float fluor_m2; //Sum of fluorescence from fluorescence picture
float fluor_m3; //Sum of fluorescence from fluorescence picture
float area_p1;
float area_m1;
float area_m2;
float area_m3;
float vacuole_area;
float vacuole_fl;
//Some local background stuff
float back_p5;
float pixels_back_p5;
float pixels_total_p5;
float back_phalfminor;
float pixels_back_phalfminor;
float pixels_total_phalfminor;
int i_time; //Which loop was this found in
int secs; //Time in seconds, starting at first image. Time stamp is
//take from metamorph's tag in the tiff file
float i_over_u; //Intersection over Union with previous time point
float fft_stat;
int x_nucleus, y_nucleus; //V1.4.5
float area_nucleus1;
float area_nucleus2;
float area_nucleus3;
float area_nucleus4;
float area_nucleus5;
float area_nucleus6;
float area_nucleus7;
float area_nucleus8;
float fl_nucleus1;
float fl_nucleus2;
float fl_nucleus3;
float fl_nucleus4;
float fl_nucleus5;
float fl_nucleus6;
float fl_nucleus7;
float fl_nucleus8;
float fl_nucleus_from_search1;
float fl_nucleus_from_search2;
float fl_nucleus_from_search3;
float fl_nucleus_from_search4;
float fl_nucleus_from_search5;
float fl_nucleus_from_search6;
float fl_nucleus_from_search7;
float fl_nucleus_from_search8;
//Some variables for the measure of the comparison of the current
//fluorescence with the previous image.
float pos_sig_mean_x;
float pos_sig_mean_y;
float pos_sig_mean_r;
float pos_sig_rms_r;
float neg_sig_mean_x;
float neg_sig_mean_y;
float neg_sig_mean_r;
float neg_sig_rms_r;
float vol_rotation;
float vol_cone;
float vol_sphere;
float surface_area;
float vol_eff_1;
float vol_eff_2;
float vol_eff_3;
float vol_eff_4;
float vol_eff_5;
float vol_eff_6;
float circumference;
float major_axis_length1;
float minor_axis_length1;
float major_axis_length2;
float minor_axis_length2;
struct point *boundary;
struct point *interior;
struct blob *next;
struct blob *prev;
};
struct blob *cs[max_cells];
int n_known=0;
int next_index=0;
int total_time=0;
struct point *make_boundary_list(int,int,int);
int neighboring_points(int, int, int);
//For my own data allocation system
//#define n_mem_blocks 1000
//Not using anymore (as of 5/22/03) so just make it 1
#define n_mem_blocks 1
struct point_mem{
struct point p;
struct point_mem *next;
};
struct point_mem *mem_blocks[n_mem_blocks];
struct point_mem *pmem_cur=NULL;
struct point_mem *pmem_start=NULL;
#define mem_size 10000
int next_block=0;
/*************************************************************/
int find_cells(struct point ***boundary_out,struct point ***interior_out){
int i,j,k,l;
float minor_s0,major_s0,minor_s1,major_s1;
int di_split,dj_split;
float tmp1,tmp2,r,r_max;
int ix,iy;
int isection,uon;
float I_over_U,I_over_U_max;
//double theta,ctheta,stheta;
int n_small,n_norm,n_small_max;
//int n_norm_max;
int n_p;
float tmp_x,tmp_y,tmp;
float sum1_xy,sum_xx,sum_x,sum1_y,sum_n;
float sum2_xy,sum2_y;
int recalc_fret_offsets_loop;
struct point *p;
struct point *ptmp;
struct point *ptmp2;
struct point *s0;
struct point *s1;
int n_cells=0;
int max_size;
float cut_step;
int n_fft_cur;
int n_fft;
int i_fft[n_fft_max];
int j_fft[n_fft_max];
double re_fft[n_fft_max];
double im_fft[n_fft_max];
struct complex *fft_in=NULL;
struct complex *fft_out=NULL;
double dtmp;
double max1,cur_max;
double min1;
int smallest_white_area=75;
struct contiguous_search *csearch;
struct contiguous_search csearch_memory;
//Some arrays for contiguous searches
int *clist_x;
int *clist_y;
int n_contiguous=0;
int list_cur=0;
//For connection between bottom and top of split images
int fret_offx,fret_offy;
static int first=1;
//float *ftmp;
//float laplace[]=
// {
// 0.0, 0.0, -1.0, 0.0, 0.0,
// 0.0, -1.0, -2.0, -1.0, 0.0,
// -1.0, -2.0, 16.0, -2.0, -1.0,
// 0.0, -1.0, -2.0, -1.0, 0.0,
// 0.0, 0.0, -1.0, 0.0, 0.0
// };
//int laplace_lims;
//int laplace_size=5;
//int ux,uy;
//int jlim;
//double r_dot_x,r_dot_y;
//double rmax,rmin;
int u;
if (first==1){
first=0;
//Do some initialiations here even though it's not
//strictly necessary. (Note that in C external and
//static variables are supposed to be initialized to
//0, but 1) 0 isn't necessarily NULL for pointers, although
//in reality it probably always is, and 2) I may want
//to make these boundary, interior, etc variables non-global
//at some point, and weird crashes might ensue.
for(u=0;u<max_cells;u++){
boundary[u]=NULL;
interior[u]=NULL;
boundary_p1[u]=NULL;
interior_p1[u]=NULL;
boundary_m1[u]=NULL;
interior_m1[u]=NULL;
boundary_m2[u]=NULL;
interior_m2[u]=NULL;
boundary_m3[u]=NULL;
interior_m3[u]=NULL;
boundary_p5[u]=NULL;
boundary_phalfminor[u]=NULL;
}
}
*boundary_out=NULL; //to default to
*interior_out=NULL;
csearch=(&csearch_memory);
//Define some arrays for the contiguous searching.
(csearch->data_array)=c;
(csearch->label_array)=d;
(csearch->xmax)=xmax;
(csearch->ymax)=ymax;
n_pixels_for_background=0;
if (image_type==bright_field){
flatten_image(c,xmax,ymax,overwrite_image,x_and_y,linear);
}
if ((image_type==fret_bf_bottom_only)||
(image_type==fret_bf_top_only)||
(image_type==fret_bf_bottom_and_top)){
have_fret_image=1;
//x-projection only
flatten_image(c,xmax,ymax,overwrite_image,x_only,nonlinear);
}
//Which region of BF to use. (If we're doing bottom and top, then
//start with bottom.)
fret_region_use=lower_fret_region;
if (image_type==fret_bf_top_only){
fret_region_use=higher_fret_region;
}
if (have_fret_image==1){
if(fret_labels==NULL){
printf("Haven't calculated upper and lower regions from");
printf("fluorescence image (while removing edge points).\n");
fflush(stdout);
exit(0);
}
//Calculate offset from bottom to top of image
fret_mx=0.0; //Starting values passed in
fret_my=0.0;
fret_bx=0.0;
fret_by=-(float)(ymax/2);
if (image_type!=fret_bf_bottom_and_top){
calculate_split_offset(fret_labels,
fl,
&fret_mx,
&fret_bx,
&fret_my,
&fret_by,
xmax,ymax);
}else{
calculate_split_offset(fret_labels,
//c,
fl,
&fret_mx,
&fret_bx,
&fret_my,
&fret_by,
xmax,ymax);
}
}
n_found=0;
for(i=0;i<max_cells;i++){
//fret_copy_type[] will be used to flag whether the cells we
//end up with were found in the image, or were copied from the
//lower or upper region to the opposite region.
//0==>original, 1==>a copy.
//Set to -999 here just to make sure that it gets set for every id.
//(We'll check it below, not being set is bad.)
fret_copy_type[i]=-999;
}
n_before_fret_copy=0;
//Set a marker for doing fret_region_use=lower_ and then higher_fret_region
//(Note n_found will continue to be incremented on second pass.
fret_loop:
if (have_fret_image==1){
if (fret_region_use==lower_fret_region){
printf("Searching lower part of split BF image for cells.\n");
}else{
printf("Searching higher part of split BF image for cells.\n");
}
}
//Calculate cut to put on the data for the searching
if((image_type==bright_field)||(image_type==confocal_transmission)
||(have_fret_image==1)){
calculate_cut();
smallest_white_area=75;
smallest_circumference=(float)sqrt(((double)smallest_white_area)/3.14159)
*2.0*3.14159;
}else if(image_type==metamorph_deconvolution){
//Deconvolution case, calculate cut differently
//We're going to vary the low cut and keep track of the number of very
//small contiguous regions and the number of normal ones. We're going to
//find the cut values that maximize the number of very small ones and then
//maximizes the ratio of the small to normal ones. (The number of small
//grows as the giant background regions start to break up.) The number
//of normal ones starts to decline as we cut into the good regions.
cut_step=10.0;
cut_low=0.5;
cut_high=1.0e30;
n_small_max=0;
r_max=0.0;
//The background are the points between cut_low and cut_high
csearch->cut_behavior=cut_between;
(csearch->p)=NULL; //No point list means loop over entire array
decon_start:
csearch->cut_low=cut_low;
csearch->cut_high=cut_high;
do_contiguous_search(csearch);
//Now search results
n_small=0;
n_norm=0;
for(i=0;i<(csearch->n_lists_found);i++){
n_contiguous=(csearch->npoints_in_list)[i];
if((n_contiguous>150)&&(n_contiguous<1000)){
n_norm++;
}else if((n_contiguous>0)&&(n_contiguous<20)){
n_small++;
}
}
printf("For cut_low=%e nsmall=%i, nnorm=%i.\n",
cut_low,n_small,n_norm);fflush(stdout);
if (n_small>n_small_max){
tmp1=cut_low;
n_small_max=n_small;
r_max=0.0; //don't consider max ratio until after we find the n_small max
if (image_type==hexagonal_grid){
cut_step=1.0;
}
} else if (n_small>0){
r=((float)n_norm)/((float)n_small);
if (r>r_max){
tmp2=cut_low;
r_max=r;
}
}else{
r=-1.0;
}
if ((r>=r_max)||(n_small>=n_small_max)){
cut_low+=cut_step;
goto decon_start;
}
if(image_type==metamorph_deconvolution){
cut_high=(tmp1+tmp2)/2.0;
cut_low=cut_high+5.0;
printf("Decon cut values are low=%e and high=%e\n",cut_low,cut_high);
}else{
cut_high=tmp1;
cut_low=cut_high+5.0;
printf("Array data cut vals are low=%e and high=%e\n",cut_low,cut_high);
}
//V1.2a TODO hard coded!
smallest_white_area=75;
smallest_circumference=(float)sqrt(((double)smallest_white_area)/3.14159)
*2.0*3.14159;
}else if(image_type==hexagonal_grid){
//We have a giant hexagonal grid. Use a fourier transform to pick
//it out.
free(fft_in);
free(fft_out);
fft_in=(struct complex *)malloc(xmax_ymax*sizeof(struct complex));
for(k=0;i<xmax_ymax;k++){
fft_in[k].r=(double)(c[k]);
fft_in[k].i=0.0;
}
fft_out=FFT_2d(fft_in,xmax,1);
//Find top six locations
max1=1.0e50;
cur_max=-1.0;
n_fft=6;
n_fft_cur=0;
l=0;
while(l<n_fft){
for(i=0;i<xmax;i++){
for(j=0;j<ymax;j++){
if((i==0)&&(j==0)) continue; //Skip the 0-frequency part.
k=(j*xmax+i);
dtmp=(fft_out[k].r)*(fft_out[k].r)+(fft_out[k].i)*(fft_out[k].i);
if(dtmp>=max1) continue;
if(dtmp>cur_max){
cur_max=dtmp;
i_fft[n_fft_cur]=i;
j_fft[n_fft_cur]=j;
re_fft[n_fft_cur]=fft_out[k].r;
im_fft[n_fft_cur]=fft_out[k].i;
}
}
}
max1=cur_max;
cur_max=-1.0;
n_fft_cur++;
l++;
}
n_fft=n_fft_cur;
//Zero everything but the top n_fft points
for(k=0;k<(xmax_ymax);k++){
fft_in[k].r=0.0;
fft_in[k].i=0.0;
}
for(l=0;l<n_fft;l++){
i=i_fft[l];
j=j_fft[l];
k=(j*xmax+i);
fft_in[k].r=re_fft[l];
fft_in[k].i=im_fft[l];
printf("(%i,%i)=(%e,%e)\n",i,j,fft_in[k].r,fft_in[k].i);
}
free(fft_out); //To prevent memory leak
fft_out=FFT_2d(fft_in,xmax,-1);
//Now reset c[] to be the real part of the fixed image.
max1=-1.0e50;
min1=1.0e50;
for(i=0;i<xmax;i++){
for(j=0;j<ymax;j++){
k=(j*xmax+i);
dtmp=fft_out[k].r;
c[k]=(float)(dtmp);
if(dtmp>max1) max1=dtmp;
if(dtmp<min1) min1=dtmp;
}
}
//Subtract off minimum so c[] is always >=0
tmp1=(float)min1;
for(i=0;i<xmax_ymax;i++){
c[i]-=tmp1;
}
cut_high=0.4*((float)(max1-min1));
cut_low=cut_high+5.0;
smallest_white_area=0;
smallest_circumference=0.0;
max_d_over_s_cut=6.0e30; //Remove cut basically
printf("Cut=%e\n",cut_high);
}else if(image_type==membrane_tagged_fl){
//Nothing yet....
//TODO
} //end of cut calculation for different image types
printf("BF-cut-low=%e, BF-cut-high=%e\n",cut_low,cut_high);fflush(stdout);
//Now, the cells are surrounded by a dark region and inside the cells is
//higher than the background. Divide the pixels into high pixels, low
//pixels and middle pixels (background).
for(i=0;i<xmax_ymax;i++){
if(c[i]>cut_high){ //inside cells
d[i]=cell_in;
}else if(c[i]>cut_low){ //background
d[i]=cell_out;
}else{
d[i]=cell_border;
}
}
//Change the behavior of the contiguous search to use the
//integer array d[]
(csearch->label_array)=d;
(csearch->cut_behavior)=equal_to_labels;
(csearch->p)=NULL; //No point list means use entire image
//If we have a deconvolution image then the border regions might be set
//to 0. If this is the case, then we don't want "half-cells" that get
//cut off by the border region. To prevent the program from picking them
//up, we set the border regions to be "cell_in" and then find all the
//contiguous pixels starting at a point in the border, and then removing
//all of those from the image. This will pick up the cells that are partly
//in the border region and then remove them.
if((image_type==bright_field)||(image_type==confocal_transmission)
||(have_fret_image==1)){
//V1.2a TODO hard coded!
max_size=1000; //Normal case, inside cells has some noise, use this
//to set some noise regions to be "inside cells"
}else if(image_type==metamorph_deconvolution){ //deconvolution case
max_size=1000;
for(k=0;k<xmax;k++){
j=xmax-1-k;
for(i=k;i<=j;i++){ //Proceed around border in decreasing squares
l=0;
u=(i*xmax+k);
if (c[u]<0.5){
d[u]=cell_in;
}else{
l++; //count how many of the four cases failed.
}
u=(i*xmax+j);
if (c[u]<0.5){
d[u]=cell_in;
}else{
l++; //count how many of the four cases failed.
}
u=(k*xmax+i);
if (c[u]<0.5){
d[u]=cell_in;
}else{
l++; //count how many of the four cases failed.
}
u=(j*xmax+i);
if (c[u]<0.5){
d[u]=cell_in;
}else{
l++; //count how many of the four cases failed.
}
if (l==4){ //All four are non-zero
goto done_square_loop; //Stop at first zero pixel
}
}
}
done_square_loop:
if (k>0) {
//Find all contiguous "cell_in" pixels starting in this border region.
(csearch->p)=point_malloc();
(csearch->p)->i=0;
(csearch->p)->j=0;
(csearch->p)->prev=NULL;
(csearch->p)->next=NULL;
(csearch->label_value)=cell_in;
(csearch->label_value)=cell_in;
do_contiguous_search(csearch);
//Remove all of found pixels since they overlap the border.
//printf("n,k=%i,%i\n",list_cur,n_contiguous);
if ((csearch->n_lists_found)>0){
list_cur=(csearch->list_start)[0];
n_contiguous=(csearch->npoints_in_list)[0];
clist_x=(csearch->list_found_x);
clist_y=(csearch->list_found_y);
for(k=list_cur;k<(list_cur+n_contiguous);k++){
d[(clist_y[k]*xmax)+(clist_x[k])]=pixel_removed;
}
}
point_free(csearch->p);
}
}else{
max_size=100000;
}
if ((image_type==bright_field)||(image_type==confocal_transmission)
||(have_fret_image==1)){
//Change background groups that are too small to cell_in
(csearch->p)=NULL; //Use entire array
(csearch->cut_behavior)=equal_to_labels;
(csearch->label_array)=d;
(csearch->label_value)=cell_out;
do_contiguous_search(csearch);
(clist_x)=(csearch->list_found_x);
(clist_y)=(csearch->list_found_y);
for(i=0;i<(csearch->n_lists_found);i++){
n_contiguous=(csearch->npoints_in_list)[i];
list_cur=(csearch->list_start)[i];
if(n_contiguous>max_size){ //These guys are solidly background, so
//remove them from image
//First check if they're so big that we should average their
//fluorescence level to get the background.
//if (n_contiguous>n_pixels_for_background){ //Save for below
// free(pixels_for_background_x);
// free(pixels_for_background_y);
// pixels_for_background_x=(unsigned short int *)
// malloc(n_contiguous*sizeof(unsigned short int));
// pixels_for_background_y=(unsigned short int *)
// malloc(n_contiguous*sizeof(unsigned short int));
// n_pixels_for_background=0;
// for(k=list_cur;k<(list_cur+n_contiguous);k++){
// pixels_for_background_x[n_pixels_for_background]=clist_x[k];
// pixels_for_background_y[n_pixels_for_background]=clist_y[k];
// n_pixels_for_background++;
// }
//}
//Now remove these pixels
for(k=list_cur;k<(list_cur+n_contiguous);k++){
d[(clist_y[k]*xmax)+(clist_x[k])]=pixel_removed;
}
}else{ //Otherwise set them to cell_in to correct interior of
//cells for background
for(k=list_cur;k<(list_cur+n_contiguous);k++){
d[(clist_y[k]*xmax)+(clist_x[k])]=cell_in;
}
}
}
}
//Change cell_border groups that are too small to cell_in
if(image_type!=hexagonal_grid){
(csearch->p)=NULL; //Use entire array
(csearch->cut_behavior)=equal_to_labels;
(csearch->label_array)=d;
(csearch->label_value)=cell_border;
do_contiguous_search(csearch);
(clist_x)=(csearch->list_found_x);
(clist_y)=(csearch->list_found_y);
for(i=0;i<(csearch->n_lists_found);i++){
n_contiguous=(csearch->npoints_in_list)[i];
list_cur=(csearch->list_start)[i];
if(n_contiguous<50){ //Set them to cell_in
for(k=list_cur;k<(list_cur+n_contiguous);k++){
d[(clist_y[k]*xmax)+(clist_x[k])]=cell_in;
}
}
}
}
//Remove groups of white that are too small, and give the others
//their own label
(csearch->p)=NULL; //Use entire array
(csearch->cut_behavior)=equal_to_labels;
(csearch->label_array)=d;
(csearch->label_value)=cell_in;
do_contiguous_search(csearch);
(clist_x)=(csearch->list_found_x);
(clist_y)=(csearch->list_found_y);
for(i=0;i<(csearch->n_lists_found);i++){
n_contiguous=(csearch->npoints_in_list)[i];
list_cur=(csearch->list_start)[i];
if(n_contiguous<smallest_white_area){ //Remove points
for(k=list_cur;k<(list_cur+n_contiguous);k++){
d[(clist_y[k]*xmax)+(clist_x[k])]=pixel_removed;
}
}else{ //Save these guys with their own label
n_p=100+n_cells;
for(k=list_cur;k<(list_cur+n_contiguous);k++){
d[(clist_y[k]*xmax)+(clist_x[k])]=n_p;
}
pixel_list[n_cells]=list_cur;
n_cells++;
}
}
//Treat each contiguous list of white cells as a potential cell.
//We have the start of each location in the clist_x,clist_y arrays
//in pixel_list[n_cells]. Now take each blob and make a border.
//Set work_array[] to 0 since make_boundary_list() uses it.
memset(work_array,0,(xmax_ymax)*sizeof(int));
for(i=0;i<n_cells;i++){
j=pixel_list[i];
p=make_boundary_list(clist_x[j],clist_y[j],100+i);
//Remove cells that have a border point on edge of image
j=0;
for(ptmp=p;ptmp!=NULL;ptmp=ptmp->next){
if((ptmp->i==0)||(ptmp->i==(xmax-1))||(ptmp->j==0)||(ptmp->j==(ymax-1))){
j=1;
break;
}
//Do same for the split image edges
if(have_fret_image==1){
u=((ptmp->j)+1)*xmax+(ptmp->i);
if (u>=xmax_ymax){
j=1;
break;
}else{
if (fret_labels[u]!=fret_region_use){
j=1;
break;