This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_rings_OAR_dev.m
1066 lines (753 loc) · 30.8 KB
/
remove_rings_OAR_dev.m
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
% Remove ring artefacts from image.
%
% Input:
% im - image to be corrected for rings, must be 2 dimensional with center
% of rotation at the center of the image
% median_size - maximum size of the rings that can be reomved by the
% filter
% number_of_pass - indicate the number of pas of the filter to increase
% the correction quality (typically two pass)
% structure_removal_level - threshold value to select the resildual
% structures to be removed before the linear filtering
% blur_angle - minimum anbular extension of the rings to
% correct (typically 40 degrees)
% fusion_angle - angular legnth for the fusion of the two
% parts of the pictures in the 180 degrees version
% visualization - 1 to see the result, 0 to process and record
% the picture
%
%
% Output:
% im_corr - corrected picture
% ring_im - image containing rings of the image
%
% credits and comments at the end of the program
%
% 05/05/2012 acceleration of the double polar correction by resizing
% 13/03/2014 new version for median filter, faster and more robust, reduce many
% troubles
% 09/10/2016 better handling of resizing to accelerate in case of very
% short filter
% change in multipass logic to limit the amount of back and forth polar
% transform
function [imcorr,ring_im] = remove_rings_OAR(im,median_size,number_of_pass,structure_removal_level,blur_angle,fusion_angle,visualization,rotate_slices,amin,amax,amedian,strong_rings,double_polar_corr,high_contrast_cut,varargin);
switch nargin
case 14
range_auto=1;
vmin=0;
vmax=65535;
cropim=0;
case 16
range_auto=0;
vmin=varargin{1};
vmax=varargin{2};
cropim=0;
case 17
range_auto=0;
vmin=varargin{1};
vmax=varargin{2};
cropim=1;
end
save_memory=1;
vdis=0;
hdis=0;
debug= 0 ; % set to 1 to see all the steps of the processing
vertical_LF_correction = 1 ;
center_correction=2 ; % 1 for fading of the ring mask at the center, 2 for adaptive filter length near the center (better)
%disp( ' NEW VERSION, SHOULD BE MORE EFFICIENT THAN BEFORE. PLEASE REPORT ANY PROBLEM ')
% need implementation of HF correction for residual and induced rings
% disp (' WARNING, ONGOING TEST FOR MAJOR DEVELOPMENT, THE RESULTS OF TESTS CAN BE DIFFERENT FROM THE ONES OBTAINED FOR FULL CALCULATION')
% disp (' please do not use the system for the moment, it will restart very soon with optimized system')
%return
%%
tic
%% conversion into 32bits in case of 8 bits data
imorig=im;
imclass=class(im);
switch imclass
case 'uint8' % way to know if it is 8 bits when you dont know how to do with matlab
disp('original data are in unsigned 8 bits')
im_conv=1;
im=single(im);
case 'uint16'
disp('original data are in unsigned 16 bits')
im_conv=1;
im=single(im);
case 'double' % added single precision 32 bit case, WL
disp('original data are in 64 bits')
im_conv=1;
im=single(im);
case 'single'
disp('original data are already in 32 bits')
im_conv=0;
end
switch imclass
case {'double','single'}
if vmax==65535;
vmin=amin;
vmax=amax;
end
end
if abs(vdis) || abs(hdis) > 0
im=interpolates(im,vdis,hdis);
end
if rotate_slices
im=imrotate(im,rotate_slices);
end
%% inits
% square image if it is not, and store the size for later cropping
[size_orig_rows,size_orig_columns]=size(im);
%imorig=im;
if size_orig_rows>size_orig_columns % more rows that columns
% topleftx=(size_orig_rows-size_orig_columns)/2;toplefty=1;
% im=gtPlaceSubImage(im,zeros(size_orig_rows),topleftx,toplefty);
disp ('WARNING, this correction can be applied only on square pictures with center of rotation at the middle of the picture')
return
elseif size_orig_rows<size_orig_columns
% topleftx=1;toplefty=(size_orig_columns-size_orig_rows)/2;
% im=gtPlaceSubImage(im,zeros(size_orig_columns),topleftx,toplefty);
disp ('WARNING, this correction can be applied only on square pictures with center of rotation at the middle of the picture')
return
else
topleftx=1;toplefty=1;
end
%% angular resolution and filter length determination
%theta = ceil(40000/blur_angle)
theta = ceil(min(ceil(40000/blur_angle),720)/2)*2;
blur_len = round(blur_angle/360*theta);
fusion_len = round(fusion_angle/360*theta);
median_len = round(median_size/size(im,2)*theta);
h_blur = fspecial('average',[1 blur_len]);
h_fusion = fspecial('average',[1 fusion_len]);
%%
%% slice presegmentation in case of high contrast cut
if high_contrast_cut>0
if debug==1
tic
end
im=im-amedian;
disp ('WARNING, you activated the high contrast cut system, are you sure that you need it ?')
se=strel('disk',ceil(median_size/10));
neutral=abs(im);
contrast_mask=max(neutral,high_contrast_cut)-high_contrast_cut;
contrast_mask=single(im2bw(contrast_mask,0.0001));
contrast_mask=imdilate(contrast_mask,se);
high_contrast_parts=medfilt_rapid(im.*contrast_mask,[10 10]);
im2=im-high_contrast_parts;
%local_corr_mask=imresize_GPU(imdilate(medfilt_rapid(imresize_GPU(im2,0.2,'bilinear'),[5 5]),se),[size(im2,1) size(im2,2)],'bilinear').*contrast_mask;
local_corr_mask=imresize(imdilate(medfilt_rapid(imresize(im2,0.2,'bilinear'),[5 5]),se),[size(im2,1) size(im2,2)],'bilinear').*contrast_mask;
im=im2+local_corr_mask;
if debug==1
disp('result of the high contrast cut');
toc
imshow(im,[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
if save_memory
clear light_parts
clear dense_parts
end
end
%% STARTING OF THE RING CORRECTION
%for k=1:number_of_pass
%%
if debug==1
close all
figure
disp('initial preparation of the picture')
toc
end
im=im';
%% correction of absorption by a simple median filter
if debug==1
tic
end
im2=im-medfilt_rapid(im,[median_size median_size],'replicate');
im2=im2-medfilt_rapid(im2,[median_size median_size],'replicate');
if debug==1
disp('result of the median fitering to remove absorption');
toc
imshow([im, im2],[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
%% convert from Cartesian to polar coordinates
if debug==1
tic
end
polar = polartrans(im2, size(im2,1), theta);
polar(find(isnan(polar)))=0;
if debug==1
disp('polar_transformation');
toc
imshow(polar',[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
if save_memory==1
clear im2
end
%% removal of low frequencies in polar coordinates
polar=polar-medfilt_rapid(polar,[median_size ceil(median_len/2)],'replicate');
polar=polar-medfilt_rapid(polar,[median_size ceil(median_len/2)],'replicate');
%% double polar correction
if double_polar_corr==1
if debug==1
tic
end
%im3=imresize_GPU(polar,0.2,'bilinear');
im3=imresize(polar,0.2,'bilinear');
im3=circshift(im3,[size(im3,1)-round(size(im3,1)/sqrt(2))-2 0]);
im3=flipud(im3);
im3=invpolar2(im3,size(im3,1));
im3=medfilt_rapid(im3,[ceil(median_size/7) ceil(median_size/7)],'replicate'); % median size is original median size / sqrt of 2 / resizing factor (5 for the moment)
im3=medfilt_rapid(im3,[ceil(median_size/7) ceil(median_size/7)],'replicate');
im3=polartrans(im3,size(im3,1),theta);
im3(find(isnan(im3)))=0;
im3=flipud(im3);
im3=circshift(im3,[-(size(im3,1)-round(size(im3,1)/sqrt(2))-2) 0]);
%im3=imresize_GPU(im3,[size(polar,1) size(polar,2)],'bicubic');
im3=imresize(im3,[size(polar,1) size(polar,2)],'bicubic');
im3=medfilt_rapid(im3,[5 5],'replicate');
polar=polar-im3;
if debug==1||debug==2
disp('double_polar_correction');
toc
imshow(polar',[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
end
%% new position for the multipass system
init_polar=polar;
pre_processing_time=toc
pre_pass_time=0;
for k=1:number_of_pass
%% correction of low frequencies in polar coordinates
if vertical_LF_correction==1
polar_VLF=medfilt_rapid(polar,[median_size 1],'replicate');
polar_VLF=medfilt_rapid(polar_VLF,[median_size 1],'replicate');
polar_VLF=medfilt_rapid(polar_VLF,[median_size 1],'replicate');
polar=polar-polar_VLF;
polar_VLF=medfilt_rapid(polar,[median_size 1],'replicate');
polar_VLF=medfilt_rapid(polar_VLF,[median_size 1],'replicate');
polar_VLF=medfilt_rapid(polar_VLF,[median_size 1],'replicate');
polar=polar-polar_VLF;
if debug==1||debug==2
disp('correction of low frequencies in polar coordinates');
toc
imshow(polar',[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
end
%% split into upper/lower part: 0-180 deg. / 180-360 deg.
if debug==1
tic
end
upper_pol = polar(:,1:end/2);
lower_pol = polar(:,end/2+1:end);
upper_pol_pad = padarray(upper_pol,[0 blur_len*4],'symmetric','both');
lower_pol_pad = padarray(lower_pol,[0 blur_len*4],'symmetric','both');
%% vertical concatenation to avoid bad effect at the center of the picture
tot_pol_pad=[(flipud(upper_pol_pad))' lower_pol_pad']';
neutral_mask=medfilt_rapid(tot_pol_pad,[median_size ceil(median_size/(size(im,1)/theta))],'replicate');
tot_pol_pad=tot_pol_pad-neutral_mask;
%% correction of low frequencies in polar coordinates
if vertical_LF_correction==1
tot_pol_pad_VLF=medfilt_rapid(tot_pol_pad,[median_size 1],'replicate');
tot_pol_pad_VLF=medfilt_rapid(tot_pol_pad_VLF,[median_size 1],'replicate');
tot_pol_pad=tot_pol_pad-tot_pol_pad_VLF;
tot_pol_pad_VLF=medfilt_rapid(tot_pol_pad,[median_size 1],'replicate');
tot_pol_pad_VLF=medfilt_rapid(tot_pol_pad_VLF,[median_size 1],'replicate');
tot_pol_pad=tot_pol_pad-tot_pol_pad_VLF;
if debug==1||debug==2
disp('correction of low frequencies in polar coordinates');
toc
imshow(tot_pol_pad',[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
end
if center_correction==1 || center_correction==2
h=fspecial('average',[ceil(median_size/2) 1]);
neutral_center=imcrop(tot_pol_pad,[0 round(size(tot_pol_pad,1)/2-median_size*2) size(tot_pol_pad,2) median_size*4-1]);
neutral_center=medfilt_rapid(neutral_center,[ceil(median_size/2) ceil(median_size/(size(im,1)/theta))],'replicate');
neutral_center=medfilt_rapid(neutral_center,[ceil(median_size/2) ceil(median_size/(size(im,1)/theta))],'replicate');
neutral_pond_center=neutral_center.*0+1;
padsize=round((size(tot_pol_pad,1)-size(neutral_center,1))/2);
neutral_center=padarray(neutral_center,[padsize 0],'both');
neutral_pond_center=padarray(neutral_pond_center,[padsize 0],'both');
%neutral_pond_center=imresize_GPU(neutral_pond_center,0.1,'nearest');
neutral_pond_center=imresize(neutral_pond_center,0.1,'nearest');
neutral_pond_center=imfilter(neutral_pond_center,h,'replicate');
neutral_pond_center=imfilter(neutral_pond_center,h,'replicate');
%neutral_pond_center=imresize_GPU(neutral_pond_center,[size(neutral_center,1) size(neutral_center,2)],'bilinear');
neutral_pond_center=imresize(neutral_pond_center,[size(neutral_center,1) size(neutral_center,2)],'bilinear');
neutral_center=neutral_center.*neutral_pond_center;
tot_pol_pad=tot_pol_pad-neutral_center;
h=fspecial('average',[ceil(median_size*1) 1]);
neutral_center=imcrop(tot_pol_pad,[0 round(size(tot_pol_pad,1)/2-median_size) size(tot_pol_pad,2) median_size*2-1]);
neutral_center=medfilt_rapid(neutral_center,[ceil(median_size/4) ceil(median_size/(size(im,1)/theta))],'replicate');
neutral_center=medfilt_rapid(neutral_center,[ceil(median_size/4) ceil(median_size/(size(im,1)/theta))],'replicate');
neutral_pond_center=neutral_center.*0+1;
padsize=round((size(tot_pol_pad,1)-size(neutral_center,1))/2);
neutral_center=padarray(neutral_center,[padsize 0],'both');
neutral_pond_center=padarray(neutral_pond_center,[padsize 0],'both');
%neutral_pond_center=imresize_GPU(neutral_pond_center,0.1,'nearest');
neutral_pond_center=imresize(neutral_pond_center,0.1,'nearest');
neutral_pond_center=imfilter(neutral_pond_center,h,'replicate');
neutral_pond_center=imfilter(neutral_pond_center,h,'replicate');
%neutral_pond_center=imresize_GPU(neutral_pond_center,[size(neutral_center,1) size(neutral_center,2)],'bilinear');
neutral_pond_center=imresize(neutral_pond_center,[size(neutral_center,1) size(neutral_center,2)],'bilinear');
neutral_center=neutral_center.*neutral_pond_center;
tot_pol_pad=tot_pol_pad-neutral_center;
h=fspecial('average',[ceil(median_size/2) 1]);
neutral_center=imcrop(tot_pol_pad,[0 round(size(tot_pol_pad,1)/2-median_size*2) size(tot_pol_pad,2) median_size*4-1]);
neutral_center=medfilt_rapid(neutral_center,[ceil(median_size/2) ceil(median_size/(size(im,1)/theta))],'replicate');
neutral_center=medfilt_rapid(neutral_center,[ceil(median_size/2) ceil(median_size/(size(im,1)/theta))],'replicate');
neutral_pond_center=neutral_center.*0+1;
padsize=round((size(tot_pol_pad,1)-size(neutral_center,1))/2);
neutral_center=padarray(neutral_center,[padsize 0],'both');
neutral_pond_center=padarray(neutral_pond_center,[padsize 0],'both');
%neutral_pond_center=imresize_GPU(neutral_pond_center,0.1,'nearest');
neutral_pond_center=imresize(neutral_pond_center,0.1,'nearest');
neutral_pond_center=imfilter(neutral_pond_center,h,'replicate');
neutral_pond_center=imfilter(neutral_pond_center,h,'replicate');
%neutral_pond_center=imresize_GPU(neutral_pond_center,[size(neutral_center,1) size(neutral_center,2)],'bilinear');
neutral_pond_center=imresize(neutral_pond_center,[size(neutral_center,1) size(neutral_center,2)],'bilinear');
neutral_center=neutral_center.*neutral_pond_center;
tot_pol_pad=tot_pol_pad-neutral_center;
h=fspecial('average',[ceil(median_size*1) 1]);
neutral_center=imcrop(tot_pol_pad,[0 round(size(tot_pol_pad,1)/2-median_size) size(tot_pol_pad,2) median_size*2-1]);
neutral_center=medfilt_rapid(neutral_center,[ceil(median_size/4) ceil(median_size/(size(im,1)/theta))],'replicate');
neutral_center=medfilt_rapid(neutral_center,[ceil(median_size/4) ceil(median_size/(size(im,1)/theta))],'replicate');
neutral_pond_center=neutral_center.*0+1;
padsize=round((size(tot_pol_pad,1)-size(neutral_center,1))/2);
neutral_center=padarray(neutral_center,[padsize 0],'both');
neutral_pond_center=padarray(neutral_pond_center,[padsize 0],'both');
%neutral_pond_center=imresize_GPU(neutral_pond_center,0.1,'nearest');
neutral_pond_center=imresize(neutral_pond_center,0.1,'nearest');
neutral_pond_center=imfilter(neutral_pond_center,h,'replicate');
neutral_pond_center=imfilter(neutral_pond_center,h,'replicate');
%neutral_pond_center=imresize_GPU(neutral_pond_center,[size(neutral_center,1) size(neutral_center,2)],'bilinear');
neutral_pond_center=imresize(neutral_pond_center,[size(neutral_center,1) size(neutral_center,2)],'bilinear');
neutral_center=neutral_center.*neutral_pond_center;
tot_pol_pad=tot_pol_pad-neutral_center;
end
pyra_tot=tot_pol_pad;
if debug==1
disp('removal of vertical low frequencies');
toc
imshow(pyra_tot',[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
if save_memory==1
clear upper_pol_pad
clear lower_pol_pad
clear polar
clear tot_pol_pad
end
%% strong rings and floating selection
if debug==1
tic
end
if strong_rings>0
im_hor_med=pyra_tot-medfilt_rapid(pyra_tot,[strong_rings 1]);
%im_hor_med=imresize_GPU(median(im_hor_med,2),[size(pyra_tot,1) size(pyra_tot,2)]);
im_hor_med=imresize(median(im_hor_med,2),[size(pyra_tot,1) size(pyra_tot,2)]);
pyra_tot=pyra_tot-im_hor_med;
end
pyra_tot=medfilt_rapid(pyra_tot,[3 3],'symmetric');
struct_tot=abs(pyra_tot);
imin=0;
imax=(amax-amin)/2;
% figure;imshow(pyra_tot',[]);
se=strel('disk',1);
if structure_removal_level>0
for shift_value=[-round(blur_len*1.5) blur_len round(blur_len/2) round(-blur_len/4) -round(blur_len/2) round(blur_len/4) -round(blur_len/2.5) round(blur_len/4.5) round(blur_len*1.5) -blur_len round(blur_len/2.5) -round(blur_len/4.5)] %round(median_size/8)
selection_tot=((struct_tot-imin)/(imax-imin));
selection_tot=selection_tot-(structure_removal_level);
selection_tot=min(max(selection_tot,0)*1e20,1);
selection_tot=imdilate(selection_tot,se);
shifted_mask_tot=circshift(selection_tot,[0 shift_value]);
rustine_tot=shifted_mask_tot.*pyra_tot;
rustine_tot=circshift(rustine_tot,[0 -shift_value]);
pyra_tot=pyra_tot.*(1-selection_tot)+rustine_tot;
struct_tot=abs(pyra_tot);
% figure;imshow(pyra_tot',[])
selection_tot=((struct_tot-imin)/(imax-imin));
selection_tot=selection_tot-(structure_removal_level);
selection_tot=min(max(selection_tot,0)*1e20,1);
selection_tot=imdilate(selection_tot,se);
shifted_mask_tot=circshift(selection_tot,[0 -shift_value]);
rustine_tot=shifted_mask_tot.*pyra_tot;
rustine_tot=circshift(rustine_tot,[0 shift_value]);
pyra_tot=pyra_tot.*(1-selection_tot)+rustine_tot;
struct_tot=abs(pyra_tot);
% figure;imshow(pyra_tot',[])
end
else
disp(' be careful, you disabled the floating selection security')
end
if strong_rings>0
pyra_tot=pyra_tot+im_hor_med;
end
if debug==1
disp('structure residual removed');
toc
imshow(pyra_tot',[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
if save_memory==1
clear struct_tot
clear selection_tot
clear shifted_mask_tot
clear rustine_tot
end
%% final median and motion blurring
if debug==1
tic
end
pyra_tot=medfilt_rapid(pyra_tot,[1 blur_len]);
pyra_tot=medfilt_rapid(pyra_tot,[1 blur_len]);
pyra_tot=imfilter_GPU(pyra_tot,h_blur);
pyra_tot=imfilter_GPU(pyra_tot,h_blur);
if debug==1
disp('median and motion filtering');
toc
imshow(pyra_tot',[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
%% vertical low frequencies correction
if vertical_LF_correction==1
if debug==1
tic
end
pyra_tot_VLF=medfilt_rapid(pyra_tot,[median_size 1],'replicate');
pyra_tot_VLF=medfilt_rapid(pyra_tot_VLF,[median_size 1],'replicate');
pyra_tot_VLF=medfilt_rapid(pyra_tot_VLF,[median_size 1],'replicate');
pyra_tot=pyra_tot-pyra_tot_VLF;
pyra_tot_VLF=medfilt_rapid(pyra_tot,[median_size 1],'replicate');
pyra_tot_VLF=medfilt_rapid(pyra_tot_VLF,[median_size 1],'replicate');
pyra_tot_VLF=medfilt_rapid(pyra_tot_VLF,[median_size 1],'replicate');
pyra_tot=pyra_tot-pyra_tot_VLF;
if debug==1
disp('induced vertical low frequencies correction');
toc
imshow(pyra_tot,[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
end
%% separation of the two parts of the picture
inv_pyra_up = flipud(pyra_tot(1:end/2,:));
inv_pyra_lo = pyra_tot(end/2+1:end,:);
%% return to original size
inv_pyra_up=imcrop(inv_pyra_up,[blur_len*4 0 size(upper_pol,2)-1 size(upper_pol,1)]);
inv_pyra_lo=imcrop(inv_pyra_lo,[blur_len*4 0 size(lower_pol,2)-1 size(lower_pol,1)]);
%% assemble upper/lower part again
if debug==1
tic
end
inv_pyra = [inv_pyra_up, inv_pyra_lo];
if save_memory==1
clear upper_pol
clear lower_pol
clear inv_pyra_up
clear inv_pyra_lo
end
if debug==1
disp('merging of the two images parts');
toc
imshow(inv_pyra,[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
%% circular padding of the picture
if debug==1
tic
end
inv_pyra_pad=padarray(inv_pyra,[0 blur_len*5],'circular','both');
if debug==1
disp('circular padding of the merged parts');
toc
imshow(inv_pyra_pad,[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
%% central correction with variable lenght median filter near the filter
if center_correction==2
center_factor=40/blur_angle;
center_corr_proportion=center_factor/40;
if center_factor>1
%disp ('you used an angular value smaller than 60 degrees, I apply a specific correction to the center of the picture')
if debug==1
tic
end
number_of_blocks = 50 ; % total number of blocks
block_size = ceil(size(inv_pyra_pad,1)*center_corr_proportion/number_of_blocks);
max_blur_len = blur_len*center_factor; % maximum angular blur lengh for the center compared to the general value
top_part=zeros(block_size*number_of_blocks,size(inv_pyra_pad,2));
for i=0:number_of_blocks-1
first_line=i*block_size+1;
last_line=i*block_size+block_size;
% disp(sprintf('processing the lines %1.0i to %1.0i', first_line,last_line));
block=imcrop(inv_pyra_pad,[1 first_line size(inv_pyra_pad,2) block_size-1 ] );
filter_size=ceil(max_blur_len*(1-i/number_of_blocks));
block=medfilt_rapid(block,[ 1 filter_size]);
top_part(first_line:last_line,1:size(top_part,2))=block; %=[top_part' block']';
end
inv_pyra_pad(1:size(top_part,1),1:size(top_part,2))=top_part;
if debug==1
disp('adaptive filter length for correction of the center of the image');
imshow(inv_pyra_pad,[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
toc
end
if save_memory==1
clear top_part
clear block
end
end
end
%% fusion of the two images parts
if debug==1
tic
end
inv_pyra_pad=imfilter_GPU(inv_pyra_pad,h_fusion,'circular');
inv_pyra_pad=imfilter_GPU(inv_pyra_pad,h_fusion,'circular');
if debug==1
disp('fusion of the two parts by motion blur');
toc
imshow(inv_pyra_pad,[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
%% filtering of induced low frequencies
if debug==1
tic
end
inv_pyra_pad=inv_pyra_pad-medfilt_rapid(inv_pyra_pad,[median_size blur_len]);
inv_pyra_pad=inv_pyra_pad-medfilt_rapid(inv_pyra_pad,[median_size blur_len]);
if debug==1
disp('removal of induced low frequencies');
toc
imshow(inv_pyra_pad,[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
%% going back to original size
if debug==1
tic
end
inv_pyra=imcrop(inv_pyra_pad,[blur_len*5-1 0 size(inv_pyra,2)-1 size(inv_pyra,1)]);
if save_memory==1
clear inv_pyra_pad
end
if debug==1
disp('going back to original size');
toc
imshow(inv_pyra,[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
pass_time=toc-pre_processing_time-pre_pass_time;
disp(sprintf('pass %1.0f on %1.0f took %1.2f seconds',k,number_of_pass,pass_time));
pre_pass_time=toc-pre_processing_time;
%% new position for multipass process
if k==1;
ring_mask_polar=inv_pyra;
else
ring_mask_polar=ring_mask_polar+inv_pyra;
end
if k<number_of_pass
polar=init_polar-ring_mask_polar;
end
end
%% polar into cartesian coordinates (this will give us the ring artifacts)
if debug==1
tic
end
ring_im = invpolar2(ring_mask_polar,size(im,1));
%%
if debug==1
disp('resulting ring mask');
toc
imshow(ring_im,[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
%% application of the ring mask
if debug==1
tic
end
imcorr=im-ring_im;
im=imcorr';
if debug==1
disp('corrected_picture');
toc
imshow(im',[]);drawnow
next_step=inputwdefault('do you want to continue ? ','y');
if next_step=='n' | next_step=='no'
return
end
end
% end
%% taking into account transposition
ring_im=ring_im';
imcorr=im;
if high_contrast_cut>0
imcorr=imcorr+high_contrast_parts+amedian-local_corr_mask;
end
%% go back into 8 or 16 bits if necessary
if im_conv>0
switch imclass
case 'uint8'
disp('going back to the original 8 bits range')
imcorr=uint8(round(imcorr));
case 'uint16'
disp('going back to the original 16 bits range')
imcorr=uint16(round(imcorr));
case 'double'
disp('going back to the original double range')
%imcorr=double(round(imcorr));
imcorr=double(imcorr);
case 'single'
disp('going back to the original single range')
%imcorr=double(round(imcorr));
imcorr=single(imcorr);
end
end
%% cropping and rotation of the picture
ring_im=ring_im(toplefty:toplefty+size_orig_rows-1,topleftx:topleftx+size_orig_columns-1);
imcorr=imcorr(toplefty:toplefty+size_orig_rows-1,topleftx:topleftx+size_orig_columns-1);
if rotate_slices
imcorr=imrotate(imcorr,-rotate_slices);
end
%% visualization of the result
post_processing_time=toc-pre_pass_time-pre_processing_time
if visualization>0 && cropim==0
LH=stretchlim(imcorr,0.001);
switch imclass
case 'uint16'
LH=LH*65635;
case 'uint8'
LF=LH*255;
end
figure(1)
clf
ax(1)=subplot(1,3,1);
imshow(single(imorig),LH);%[vmin vmax])
axis off
ax(2)=subplot(1,3,2);
imshow(single(imcorr),LH);%[vmin vmax]);
axis off
ax(3)=subplot(1,3,3);
imshow(single(imorig)-single(imcorr),[]);
axis off
linkaxes(ax,'xy')
impixelinfo
drawnow
elseif visualization>0 && cropim==1
aa=1500;
bb=2500;
cc=2000;
dd=3000;
figure(1)