%
% Complete documentation on the extended LaTeX markup used for Insight
% documentation is available in ``Documenting Insight'', which is part
% of the standard documentation for Insight.  It may be found online
% at:
%
%     http://www.itk.org/

\documentclass{InsightArticle}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  hyperref should be the last package to be loaded.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[dvips,
bookmarks,
bookmarksopen,
backref,
colorlinks,linkcolor={blue},citecolor={blue},urlcolor={blue},
]{hyperref}
% to be able to use options in graphics
\usepackage{graphicx}
% for pseudo code
\usepackage{listings}
% subfigures
\usepackage{subfigure}


%  This is a template for Papers to the Insight Journal. 
%  It is comparable to a technical report format.

% The title should be descriptive enough for people to be able to find
% the relevant document. 
\title{Kappa Sigma Clipping}

% Increment the release number whenever significant changes are made.
% The author and/or editor can define 'significant' however they like.
% \release{0.00}

% At minimum, give your name and an email address.  You can include a
% snail-mail address if you like.
\author{Ga\"etan Lehmann}
\authoraddress{INRA, UMR 1198; ENVA; CNRS, FRE 2857, Biologie du D\'eveloppement et
Reproduction, Jouy en Josas, F-78350, France}

\begin{document}
\maketitle

\ifhtml
\chapter*{Front Matter\label{front}}
\fi


\begin{abstract}
\noindent
When an image is mostly composed of background pixels, most of the automatic
thresholding methods are failing to produce a relevant threshold. This is mainly
caused because one mode is over represented compared to the other in the histogram
of the image. The Kappa-Sigma clipping, a method largely used in astronomy, don't
try to separate 2 modes in the histogram, but rather try to find the properties of
the only usable mode, the one of the background, and compute a threshold to select
the values significantly different of the background.
\end{abstract}

% \tableofcontents

\section{Description}
The images mostly formed of background pixels are not so common in biological 2D images.
However, the third dimension make this case a lot more common, and it's not rare to
have some images produced by a laser scanning confocal microscope which contain more than 90\%
of background pixels. The histogram of those images often appear to be mono-modal, as
the one shown in Figure~\ref{histogram}. The classical automatic thresholding methods
are trying to find a threshold which maximize the separation of (at least) two modes in
the histogram, and so are often performing badely when one mode is largely over represented
compared to the other.

\begin{figure}[htbp]
\centering
\includegraphics{histogram}
\caption{Histogram of an image mainly composed of background pixels.\label{histogram}}
\end{figure}

Kappa-Sigma clipping use an other approach: it found the mean and sigma of the background, and
use this two properties to select the pixels significantly different of the background.
Mean and sigma are first computed on the entire image, and a threshold is computed as \verb$mean + f * sigma$.
This threshold is then used to select the background, and recompute a new threshold with only
pixels in the background.
This algorithm shouldn't converge to a value, so the number of iterations must be provided.
In general, two iterations are used.

\section{Code example}

\small \begin{verbatim}
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkSimpleFilterWatcher.h"

#include "itkKappaSigmaThresholdImageFilter.h"


int main(int argc, char * argv[])
{

  if( argc != 5 )
    {
    std::cerr << "usage: " << argv[0]
              << " input output sigmaFactor numberOfIterations" << std::endl;
    exit(1);
    }

  const int dim = 3;
  
  typedef unsigned char PType;
  typedef itk::Image< PType, dim > IType;

  typedef itk::ImageFileReader< IType > ReaderType;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName( argv[1] );

  typedef itk::KappaSigmaThresholdImageFilter< IType > FilterType;
  FilterType::Pointer filter = FilterType::New();
  filter->SetInput( reader->GetOutput() );
  filter->SetSigmaFactor( atof(argv[3]) );
  filter->SetNumberOfIterations( atoi(argv[4]) );

  itk::SimpleFilterWatcher watcher(filter, "filter");

  typedef itk::ImageFileWriter< IType > WriterType;
  WriterType::Pointer writer = WriterType::New();
  writer->SetInput( filter->GetOutput() );
  writer->SetFileName( argv[2] );
  writer->Update();

  return 0;
}
\end{verbatim} \normalsize

% \url{http://www.itk.org}
% \code{Insight/Documentation/Style.pdf}
% \section{Principles of Solar Spot Detection}
% \cite{ITKSoftwareGuide}.
% \doxygen{ImageToImageFilter}

% \small \begin{verbatim}
% \end{verbatim} \normalsize


% The {itemize} environment uses a bullet for each \item.  If you want the 
% \item's numbered, use the {enumerate} environment instead.
% \begin{itemize}
%   \item  Insight Toolkit 2.4.
%   \item  CMake 2.2
% \end{itemize}

% \ref{cthead1}

% \begin{figure}[htbp]
% \centering
% \includegraphics{cthead1}
% \caption{The input image.\label{cthead1}}
% \end{figure}


\appendix



\bibliographystyle{plain}
\bibliography{InsightJournal}
\nocite{ITKSoftwareGuide}

\end{document}

