-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_sfcrf.m
99 lines (89 loc) · 2.5 KB
/
main_sfcrf.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
function [] = main_sfcrf(imgName, icName, maskName, alpha, niter)
%close all;
%clc
%% parameter setting
Ns = 101; %search window size,
Nr = 3; %1 % data similarity patch size
%Unary weight
%alpha = 0.1;
%Pairwise weight
beta = 1-alpha;
%std. of pixel values, used for scaling Euclidean distance
% sigma = 0.10; % smaller sigma, bigger data similarity, means easier to be accepted for performing weighted average
sel_sigma=25; % this is acutally used for distance weights scaling, select connections
%number of iterations
%iter = 10;
iter = niter;
prob = 5; % sampling density
L = 1;
thrd2 = 1;
sigma = 0.5; % gamma for Pij,
%%
close all;
z = double(imread(imgName));
ic = double(imread(icName));
%load(icName,'ic');
%ic = double(ic);
[path,name,ext] = fileparts(imgName);
mask = imread(maskName);
mask = double(mask);
ic = ic.*(mask == 0);
ic(ic>1)=1;
ic(ic<0)=0;
ic_t = 2-ic;
%h = fspecial('gaussian',7,4);
%ic = imfilter(ic,h);
ic = 2-ic;
[m,n] = size(z);
%% enhancement
% normalization
z = z-min(z(:));
z = z./max(z(:));
% z = histeq(z);
z = z+1;
%% initial condition
x_0 =ic;
LD = zeros(m,n);
x = ic;
%z = z(100:400,400:end);
%mask = mask(100:400,400:end);
%x = x(100:400,400:end);
%x_0 = x_0(100:400,400:end);
for k = 1:iter
tic
v = pairwise_FSRF(x,sigma,prob,Ns,Nr,sel_sigma,z,mask,LD); % v: the weighted (binary weights) majority voting of labels among sampled pixles
toc
%df = L*(z./(x.^2)-1./x);
df = (x_0-x);
df2 = v - x;
grad = alpha*df + beta*df2; % grad is a balanced measurement between the resiual and the update
%figure;imagesc(df2);colorbar
x = x + (grad);
% figure;imshow(2-x);colormap jet
save(['data/' name(1:15) '-x' num2str(k)],'x')
continue
%%
I=v;
mu_img = mean(I(:));
sigma_img = std(I(:));
thrd = mu_img - thrd2*sigma_img;
thrd = 1.5;
I(I<thrd) = 1;
I(I>=thrd) = 2;
% figure;imagesc(I);colormap gray;
%%
figure(1)
subplot(321),imshow(I,[]);
title(['itr: ' ,num2str(k)])
subplot(322),imshow(v,[]); colormap jet
title('v')
subplot(323),imshow(x,[]); colormap jet
title('x');
subplot(324),plot(ic(:),df2(:),'.');hold on; plot(1:2,zeros(1,2));hold off
subplot(325),plot(alpha*df(:),beta*df2(:),'.');
subplot(326),imshow(x-x_0,[]);colorbar
title('x-x_0')
pause(0.5)
end;
%% output to disk
%imwrite(uint8((2-x)*255),['sfcrf_',icName '.tif']);