diff options
Diffstat (limited to 'SD-VBS/common/toolbox/MultiNcut/computeMultiW.m')
-rwxr-xr-x | SD-VBS/common/toolbox/MultiNcut/computeMultiW.m | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/SD-VBS/common/toolbox/MultiNcut/computeMultiW.m b/SD-VBS/common/toolbox/MultiNcut/computeMultiW.m new file mode 100755 index 0000000..b2cb7ea --- /dev/null +++ b/SD-VBS/common/toolbox/MultiNcut/computeMultiW.m | |||
@@ -0,0 +1,245 @@ | |||
1 | %inputs : image, number of layers, distance defining the subgrid, the edge filter scales for each layer, radius for each layer, | ||
2 | %edge variance for filter, shape of the neighbourhood layout ('square', 'star', 'cross'), sigma for intensity affinity, | ||
3 | % sigma for distance influence in affinity, weight coefficients for Wpps in the multiscale matrix. | ||
4 | %output : multiscale affinity matrix , extern constraint matrix, affinity matrices of each layer seperatly. | ||
5 | |||
6 | |||
7 | function [multiWpp,constraintMat, Wind,data,emag,ephase]= computeMultiW (image,data); | ||
8 | |||
9 | %variables | ||
10 | |||
11 | if isempty(data.layers.number) | ||
12 | n=2; | ||
13 | else | ||
14 | n=data.layers.number; | ||
15 | end | ||
16 | |||
17 | if isempty(data.layers.dist) | ||
18 | dist_grid=3; | ||
19 | else | ||
20 | dist_grid=data.layers.dist; | ||
21 | end | ||
22 | |||
23 | if isempty(data.W.scales) | ||
24 | s=1:n; | ||
25 | elseif (length(data.W.scales)==n) | ||
26 | s=data.W.scales; | ||
27 | else | ||
28 | s=1:n; | ||
29 | end | ||
30 | |||
31 | if isempty(data.W.radius) | ||
32 | r(1)=2; | ||
33 | for i=2:n | ||
34 | r(i)=10; | ||
35 | end | ||
36 | else | ||
37 | r=data.W.radius; | ||
38 | end | ||
39 | |||
40 | |||
41 | if isempty(data.W.edgeVariance) | ||
42 | data.W.edgeVariance=0.1; | ||
43 | end | ||
44 | |||
45 | if isempty(data.W.gridtype) | ||
46 | data.W.gridtype='square'; | ||
47 | end | ||
48 | |||
49 | if isempty(data.W.sigmaI) | ||
50 | data.W.sigmaI=0.12; | ||
51 | end | ||
52 | |||
53 | if isempty(data.W.sigmaX) | ||
54 | data.W.sigmaX=10; | ||
55 | end | ||
56 | |||
57 | if isempty(data.layers.weight) | ||
58 | coef(1)=5; | ||
59 | coef(2:n)=200; | ||
60 | elseif (length(data.layers.weight)==n) | ||
61 | coef=data.layers.weight; | ||
62 | else | ||
63 | coef(1)=5; | ||
64 | coef(2:n)=100; %200 | ||
65 | end | ||
66 | |||
67 | if isempty(data.W.mode) | ||
68 | data.W.mode=mixed; | ||
69 | end | ||
70 | |||
71 | |||
72 | [p1,q1,ignore]=size(image); | ||
73 | image=image(:,:,1); | ||
74 | filter_par = [4,30,4]; %[9,30,4] | ||
75 | [x,y,gx,gy,par,threshold,emag,ephase,g,FIe,FIo,mago] = quadedgep2(image,filter_par,data,0.001); | ||
76 | minW=10^(-2); %-3 | ||
77 | |||
78 | |||
79 | % function [multiWpp,constraintMat,p,q,Wppp,subgrid] = computemultiWpp(n,imageX,r,dist_grid,s,dataWpp,emag,ephase,minW,mode,facteurMul,contrainte,tt,gridtype,colormode,imageOriginale,subgridImageReduite,pG,qG) | ||
80 | |||
81 | p= p1*ones(n,1); | ||
82 | q= q1*ones(n,1); | ||
83 | d= dist_grid*ones(n,1); | ||
84 | d(1)=1; | ||
85 | for (i=2:n) | ||
86 | d(i)=d(i)*3^(i-2); | ||
87 | end | ||
88 | p=ceil(p1./d); | ||
89 | q=ceil(q1./d); | ||
90 | |||
91 | %computation of the subgrids (the first pixel is coded by one). S{i,j}(k) is the index of | ||
92 | %the kth pixel of the jth grid in the ith grid. | ||
93 | |||
94 | for i=1:n-1 | ||
95 | for j=i+1:n | ||
96 | a=[0:p(j)*q(j)-1]; | ||
97 | subgrid{i,j}=p(i)*(floor(a/p(j)))*d(j)/d(i)+(1+mod(a,p(j))*d(j)/d(i)); | ||
98 | end | ||
99 | end | ||
100 | |||
101 | %computation of the independent W matrix for each layer Wind{i} 1=<i=<n. | ||
102 | |||
103 | [w1i,w1j]=cimgnbmap([p1,q1], r(1), 1); | ||
104 | |||
105 | if strcmp(data.W.mode,'mixed') | ||
106 | rMin = 0; | ||
107 | imageXX=double(image(:)); | ||
108 | sigmaI= (std(imageXX(:)) + 1e-10 )* data.W.sigmaI; | ||
109 | Wpp{1} = multiIntensityFirstLayer(double(image),w1i,w1j,rMin,data.W.sigmaX,sigmaI,minW); | ||
110 | Wpp2= affinityic(emag(:,:,s(1)),ephase(:,:,s(1)),w1i,w1j,max(max(emag(:,:,s(1)))) * data.W.edgeVariance); | ||
111 | Wpp{1} = sqrt(Wpp{1} .* Wpp2)+0.1*Wpp2; | ||
112 | |||
113 | elseif strcmp(data.W.mode,'notmixed') | ||
114 | Wpp{1}= affinityic(emag(:,:,s(1)),ephase(:,:,s(1)),w1i,w1j,max(max(emag(:,:,s(1)))) * data.W.edgeVariance); | ||
115 | |||
116 | elseif strcmp(data.W.mode,'intensity') | ||
117 | rMin = 0; | ||
118 | imageXX=double(image(:)); | ||
119 | sigmaI= (std(imageXX(:)) + 1e-10 )* data.W.sigmaI; | ||
120 | Wpp{1} = multiIntensityFirstLayer(double(image),w1i,w1j,rMin,data.W.sigmaX,sigmaI,minW); | ||
121 | |||
122 | end | ||
123 | Wpp{1}=coef(1)*(Wpp{1}+Wpp{1}')/2; | ||
124 | %Wpp{1}= coef(1)*Wpp{1}; | ||
125 | Wind{1}=Wpp{1}; | ||
126 | |||
127 | |||
128 | |||
129 | |||
130 | for i=2:n | ||
131 | if strcmp(data.W.gridtype,'square') | ||
132 | [wwi,wwj]=cimgnbmap([p(i),q(i)], r(i), 1); | ||
133 | elseif strcmp(data.W.gridtype,'star') | ||
134 | [wwi,wwj]=cimgnbmap_star([p(i),q(i)], r(i), 1); | ||
135 | elseif strcmp(data.W.gridtype,'cross') | ||
136 | [wwi,wwj]=cimgnbmap_cross([p(i),q(i)], r(i), 1); | ||
137 | end | ||
138 | wwi=double(wwi); | ||
139 | wiInOriginalImage=(p1*(floor(wwi/p(i)))*d(i))+(1+mod(wwi,p(i))*d(i)); | ||
140 | wiInOriginalImage=(p1*(floor(wwi/p(i)))*d(i))+(1+mod(wwi,p(i))*d(i)); | ||
141 | |||
142 | wiInOriginalImage= uint32(wiInOriginalImage); | ||
143 | |||
144 | if strcmp(data.W.mode,'mixed') | ||
145 | Wpp2= multiAffinityic(emag(:,:,i),ephase(:,:,i),wiInOriginalImage,wwj,subgrid{1,i},p(i),q(i),uint32(wwi),max(max(emag(:,:,i))) * data.W.edgeVariance); | ||
146 | a=floor(d(i)/d(i-1)); | ||
147 | if (mod(a,2)==0) | ||
148 | a=a+1; | ||
149 | end | ||
150 | % Wpp{i} = multiIntensityWppc(double(imageX),wiInOriginalImage,wwj,rMin,dataWpp.sigmaX,sigmaI,minW,subgrid{1,i},p(i),q(i),wi{i}); | ||
151 | |||
152 | Wpp{i} = multiIntensityWppc(double(image),wiInOriginalImage,wwj,rMin,data.W.sigmaX,sigmaI,minW,subgrid{1,i},p(i),q(i),uint32(wwi)); | ||
153 | Wpp{i} = sqrt(Wpp{i} .* Wpp2)+0.1*Wpp2; | ||
154 | elseif strcmp(data.W.mode,'notmixed') | ||
155 | Wpp{i}= multiAffinityic(emag(:,:,i),ephase(:,:,i),wiInOriginalImage,wwj,subgrid{1,i},p(i),q(i),uint32(wwi),max(max(emag(:,:,i))) * data.W.edgeVariance); | ||
156 | elseif strcmp(data.W.mode,'intensity') | ||
157 | Wpp{i} = multiIntensityWppc(double(image),wiInOriginalImage,wwj,rMin,data.W.sigmaX,sigmaI,minW,subgrid{1,i},p(i),q(i),uint32(wwi)); | ||
158 | end | ||
159 | Wpp{i}= coef(i)*(Wpp{i}+Wpp{i}')/2; | ||
160 | |||
161 | %Wpp{i}= coef(i)*Wpp{i}; | ||
162 | Wind{i}=Wpp{i}; | ||
163 | |||
164 | end | ||
165 | |||
166 | %computation of the intern contraint matrices C{i,j}. | ||
167 | |||
168 | for i=1:n-1 | ||
169 | r=floor(d(i+1)/(d(i)*2)); | ||
170 | [wwi,wwj]=cimgnbmap([p(i),q(i)], r, 1); | ||
171 | wi{i}=wwi; | ||
172 | wj{i}=wwj; | ||
173 | end | ||
174 | |||
175 | for i=1:n-1 | ||
176 | for j=i+1:n | ||
177 | C{i,j}=sparse(p(i)*q(i),p(j)*q(j)); | ||
178 | firstPointer=double(wj{i}(subgrid{i,j}))+1; | ||
179 | lastPointer=double(wj{i}(subgrid{i,j}+1)); | ||
180 | invNbNeighbours=1./(lastPointer-firstPointer+1); | ||
181 | for (k=1:p(j)*q(j)) | ||
182 | for (m=firstPointer(k):lastPointer(k)) | ||
183 | C{i,j}(double(wi{i}(m))+1,k)=invNbNeighbours(k); | ||
184 | end | ||
185 | end | ||
186 | end | ||
187 | end | ||
188 | |||
189 | %Assembling the built matrices to make up multiWpp. | ||
190 | for i=1:n | ||
191 | if (i>1) | ||
192 | for j=i-1:-1:1 | ||
193 | Wpp{i}=[C{j,i}',Wpp{i}]; | ||
194 | end | ||
195 | end | ||
196 | if (i<n) | ||
197 | for j=i+1:n | ||
198 | Wpp{i}=[Wpp{i},C{i,j}]; | ||
199 | end | ||
200 | end | ||
201 | end | ||
202 | |||
203 | % %Assembling the built matrices to make up Wpp without intern constrains. | ||
204 | % for i=1:n | ||
205 | % if (i>1) | ||
206 | % for j=i-1:-1:1 | ||
207 | % Wpp{i}=[sparse(p(i)*q(i),p(j)*q(j)),Wpp{i}]; | ||
208 | % end | ||
209 | % end | ||
210 | % if (i<n) | ||
211 | % for j=i+1:n | ||
212 | % Wpp{i}=[Wpp{i},sparse(p(i)*q(i),p(j)*q(j))]; | ||
213 | % end | ||
214 | % end | ||
215 | % end | ||
216 | |||
217 | clear Wind;Wind = 1; | ||
218 | |||
219 | multiWpp=Wpp{1}; clear Wpp{1} | ||
220 | for i=2:n | ||
221 | multiWpp=[multiWpp;Wpp{i}];clear Wpp{i} | ||
222 | end | ||
223 | |||
224 | |||
225 | % Computing the average extern constraint | ||
226 | |||
227 | pq=sum(p(2:n).*q(2:n)); | ||
228 | p2q2=p(2)*q(2); | ||
229 | constraintMat=[-C{1,2};speye(p2q2);sparse(pq-p2q2,p2q2)]; | ||
230 | if n>2 | ||
231 | for i=3:n | ||
232 | piqi=p(i)*q(i); | ||
233 | if i~=n | ||
234 | constraintMat=[constraintMat,[sparse(sum(p(1:i-2).*q(1:i-2)),piqi);-C{i-1,i};speye(piqi);sparse(pq-sum(p(2:i).*q(2:i)),piqi)]]; | ||
235 | else | ||
236 | constraintMat=[constraintMat,[sparse(sum(p(1:i-2).*q(1:i-2)),piqi);-C{i-1,i};speye(piqi)]]; | ||
237 | end | ||
238 | end | ||
239 | end | ||
240 | |||
241 | % saving useful information | ||
242 | %subgrids, p and q | ||
243 | data.subgrid=subgrid; | ||
244 | data.p=p; | ||
245 | data.q=q; | ||