<?php
include("../../header.php");
?>

<h1 style="text-align:center"> Tutorial on the <br/> Synthesis-Oriented Texton </h1>

<div style="margin-left:auto;margin-right:auto;width:550px">

<p> &emsp; This tutorial can be executed in the Matlab console, provided the archive <a href="../codes/sot_matlab.zip">sot_matlab.zip</a> has been extracted in the current path.</p>

<h2>Load a texture image<a name="1"></a></h2>

<pre class="codeinput">
close <span class="string">all</span>
clear <span class="string">all</span>

u = im2double(imread(<span class="string">'textures/0000_2.png'</span>));

figure
imshow(u)
title(<span class="string">'Original image'</span>)
</pre>

<img class="center" src="tuto_01.png">

<h2>Compute the normalized kernel associated to u<a name="2"></a></h2>
 
<pre class="codeinput">[M,N,C] = size(u);
mu = repmat(mean(mean(u),2),[M N 1]);
tu = 1/sqrt(M*N)*(u-mu);
</pre>

<h2>Definition of a rectangular support for the SOT<a name="3"></a></h2>

<pre class="codeinput">r = 15; <span class="comment">% support half-size</span>
M2 = floor(M/2)+1; N2 = floor(N/2)+1;
sup = zeros(M,N);
xind = M2-r:M2+r; m=2*r+1;
yind = N2-r:N2+r; n=2*r+1;
sup(xind,yind) = Inf;
suprgb = repmat(sup,[1 1 3]);
cardsup = sum(sum(sup&gt;0));

figure
imshow(sup&gt;0)
title(<span class="string">'Support'</span>)
</pre>

<img class="center" src="tuto_02.png" > 

<h2>SOT computation<a name="4"></a></h2>

<pre class="codeinput">options.err = 1e-3;
options.nmax = 1000;
options.visu = 1;
options.nonverbose = 1;
t = so_texton(u,suprgb,options);
</pre>

<img class="center" src="tuto_03.png" > 

<h2>Crop the central part<a name="5"></a></h2>

<pre class="codeinput">tc = t(xind,yind,:);

muc = mu(xind,yind,:);

figure
imshow(muc+sqrt(m*n)*tc)
title(sprintf([<span class="string">'SOT t \n RME = '</span> <span class="keyword">...</span>
    num2str(otdist(tu,tc,M,N,1))]))
</pre>

<img class="center" src="tuto_04.png" > 

<h2>DSN synthesis<a name="6"></a></h2>

<pre class="codeinput">
<span class="comment">% Size of synthesis domain</span>
Ms = 256;
Ns = 256;
nimp = 30; 
<span class="comment">% number of impacts per pixel</span>

<span class="comment">% Mean image</span>
mus = repmat(mu(1,1,:),[Ms Ns 1]);
<span class="comment">% DSN</span>
[sn,pp] = dsn(tc,mus,nimp/cardsup);
<span class="comment">% ADSN (Gaussian limit)</span>
adsnt = adsn(tc,mus);

figure
imshow(u)
title(<span class="string">'Original texture u'</span>)
figure
imshow(adsnt)
title(<span class="string">'ADSN(t)'</span>)
figure
imshow(sn)
title([<span class="string">'SN(t), '</span> num2str(nimp) <span class="string">' impacts/pix'</span>])
</pre>

<img class="center" src="tuto_05.png" > 

<img class="center" src="tuto_06.png" > 

<img class="center" src="tuto_07.png" > 

<p>&emsp; One can observe that <strong>SN(t)</strong> is a visually satisfying approximation
of the Gaussian random field <strong>ADSN(t)</strong>, even if the number of
impacts per pixel is reasonably low.</p>

<h2>The underlying Poisson process is indeed sparse<a name="7"></a></h2>

<pre class="codeinput">
figure
imshow(pp)
title(<span class="string">'Underlying Poisson Process'</span>)
</pre>

<img class="center" src="tuto_08.png" > 

<h2>Comparison between the ADSN models<a name="8"></a></h2>

<pre class="codeinput">
<span class="comment">% ADSN associated to u</span>
adsnu = adsn(tu,mus);
<span class="comment">
% Compute the Gaussian relative model error</span>
gme = otdist(tu,t,Ms,Ns,1);

figure
imshow(adsnu)
title(<span class="string">'ADSN(t_u)'</span>)
figure
imshow(adsnt)
title([<span class="string">'ADSN(t), RME = '</span> num2str(gme)])
</pre>

<img class="center" src="tuto_09.png" > 

<img class="center" src="tuto_10.png" > 

<p>&emsp; One can observe a slight loss in the color diversity when comparing
<strong>u</strong> with <strong>SN(t)</strong>, or when comparing <strong>ADSN(t<sub>u</sub>)</strong> with <strong>ADSN(t)</strong>.
Indeed, the computation of the SOT does not ensure that the
marginal color covariance of <strong>ADSN(t)</strong> is the same than the
empirical color covariance of u.
This will be confirmed by the next paragraph.</p><br/>

<h2>Color covariance<a name="9"></a></h2>

<pre class="codeinput">
<span class="comment">% Compute the matricial autocorrelation of t, which is the</span>
<span class="comment">%   marginal color covariance of ADSN(t)</span>
act = zeros(C);
<span class="keyword">for</span> i=1:C
    <span class="keyword">for</span> j=1:C
        act(i,j) = sum(sum(t(:,:,i).*t(:,:,j)));
    <span class="keyword">end</span>
<span class="keyword">end</span>

<span class="comment">% Empirical color covariance of u :</span>
cov(reshape(u,[M*N 3]))
<span class="comment">% Marginal color covariance of ADSN(t) :</span>
act
<span class="comment">% Empirical color covariance of adsnt :</span>
cov(reshape(adsnt,[Ms*Ns 3]))
</pre>

<pre class="codeoutput">
ans =

    0.0060    0.0047    0.0048
    0.0047    0.0045    0.0040
    0.0048    0.0040    0.0050


act =

    0.0047    0.0039    0.0041
    0.0039    0.0033    0.0034
    0.0041    0.0034    0.0036


ans =

    0.0046    0.0038    0.0040
    0.0038    0.0032    0.0033
    0.0040    0.0033    0.0035

</pre>

<h2>Color covariance correction<a name="10"></a></h2>

<p>&emsp; One can correct this color loss by applying a transformation in the
color space so that the marginal color covariance of <strong>ADSN(t)</strong>
matches the empirical color covariance of <strong>u</strong>.</p>

<pre class="codeinput">
tc_cc = cov_correction(tc,u);

<span class="comment">% Visual comparison of the textons</span>
muc = mu(xind,yind,:);
figure
subplot(1,2,1)
imshow(muc+sqrt(m*n)*tc)
title(sprintf([<span class="string">'SOT t \n RME = '</span> <span class="keyword">...</span>
    num2str(otdist(tu,tc,M,N,1))]))
subplot(1,2,2)
imshow(muc+sqrt(m*n)*tc_cc)
title(sprintf([<span class="string">'SOT t_{cc} with color correction \n RME = '</span> <span class="keyword">...</span>
    num2str(otdist(tu,tc_cc,M,N,1))]))
</pre>

<img class="center" src="tuto_11.png" style="border:0px"> 

<h2>Comparison of the DSN synthesis results<a name="11"></a></h2>

<pre class="codeinput">
sn_cc = dsn(tc_cc,mus,nimp/cardsup);

figure
imshow(sn)
title(<span class="string">'DSN(t)'</span>)
figure
imshow(sn_cc)
title([<span class="string">'DSN(t_{cc}), '</span> num2str(nimp) <span class="string">' impacts/pix'</span>])
</pre>

<img class="center" src="tuto_12.png" > 

<img class="center" src="tuto_13.png" > 

<h2>Comparison of the ADSN synthesis results<a name="12"></a></h2>

<pre class="codeinput">
adsnt_cc = adsn(tc_cc,mus);

figure
imshow(adsnt)
title(<span class="string">'ADSN(t)'</span>)
figure
imshow(adsnt_cc)
title(<span class="string">'ADSN(t_{cc})'</span>)
</pre>

<img class="center" src="tuto_14.png" > 

<img class="center" src="tuto_15.png" > 

<h2>The marginal color covariance is well corrected<a name="13"></a></h2>

<pre class="codeinput">
act_cc = zeros(C);
<span class="keyword">for</span> i=1:C
    <span class="keyword">for</span> j=1:C
        act_cc(i,j) = sum(sum(tc_cc(:,:,i).*tc_cc(:,:,j)));
    <span class="keyword">end</span>
<span class="keyword">end</span>

<span class="comment">% Empirical color covariance of u :</span>
cov(reshape(u,[M*N 3]))
<span class="comment">% Marginal color covariance of ADSN(t_{cc}) :</span>
act_cc
<span class="comment">% Empirical color covariance of adsnt_cc :</span>
cov(reshape(adsnt_cc,[Ms*Ns 3]))
</pre>

<pre class="codeoutput">
ans =

    0.0060    0.0047    0.0048
    0.0047    0.0045    0.0040
    0.0048    0.0040    0.0050


act_cc =

    0.0060    0.0047    0.0048
    0.0047    0.0045    0.0040
    0.0048    0.0040    0.0050


ans =

    0.0060    0.0048    0.0048
    0.0048    0.0045    0.0040
    0.0048    0.0040    0.0050

</pre>

</div>

<?php
include("../../bottom.php");
?>
  
