Population
Image processing library in C++
Swamping segmentation

See the article http://www.ias-iss.org/ojs/IAS/article/view/826

To illustrate the swamping segmentation, we will show the application with the image given in the article First, you load the 2d matrix :

Mat2UI8 img;
img.load("/home/vincent/population/iex.png");
iex.png

Here, you load a 2d image.
The first step of the swamping segmentation is to locate a single seed inside each object of the image (grain in this granular material). To avoid an oversegmentation, we filter the image with a horizontal filter as Processing::smoothDeriche, Processing::opening, PDE::nonLinearAnisotropicDiffusionDericheFast, following by a vertical filter Processing::dynamic. Then, we extract the regional minima :

Mat2UI8 filter = Processing::smoothDeriche(img,1);
filter = Processing::dynamic(filter,40);
Mat2UI16 minima = Processing::minimaRegional(filter);
Visualization::labelForeground(minima,img).display();
iexminima.png

The second step of the swamping segmentation is to find a seed inside the background of the image. We apply the watershed transformation algorithm with a boundary on the original image :

Mat2UI16 water =Processing::watershedBoundary(minima,filter,1);
Mat2UI16 boundary = Processing::threshold(water,0,0);
boundary.display("boundary",true,false);
iexboundary.png

The third step of the swamping segmentation is to merge the seeds of the first step and of the second step. Then, we apply the watershed transformation on the gradient magitude to enhance the object boundary.

Mat2UI8 gradient = Processing::gradientMagnitudeDeriche(img,1);
water = Processing::watershed(minima,gradient);
iexswamping.png

C++ code

#include"Population.h"//Single header
using namespace pop;//Population namespace
int main(){
Mat2UI8 img;
img.load(POP_PROJECT_SOURCE_DIR+std::string("/image/iex.png"));
filter = Processing::dynamic(filter,40);
// Visualization::labelForeground(minima,img).display();
Mat2UI16 water =Processing::watershedBoundary(minima,filter,1);
Mat2UI16 boundary = Processing::threshold(water,0,0);//the boundary label is 0
// boundary.display("boundary",true,false);
minima = Processing::labelMerge(boundary,minima);
water = Processing::watershed(minima,gradient);
Visualization::labelForeground(water,img).display();
}

Pyton code

1 import sys
2 import os
3 PathPop= ""
4 if os.path.isfile(PathPop+"population.py")==0:
5  print "set the variable PathPop to the path where you compile population, for instance D:\Users/vtariel/Desktop/ANV/Population-build/. This folder must contain population.py"
6  sys.exit(-1)
7 sys.path.append(PathPop)
8 from population import *
9 
10 
11 img = Mat2UI8()
12 img.load(PathPop+"/image/iex.png")
13 proc = Processing()
14 filter = proc.smoothDeriche(img,1)
15 filter = proc.dynamic(filter,40)
16 minima = proc.minimaRegional(filter)
17 visu = Visualization()
18 # visu.labelForeground(minima,img).display()
19 water = proc.watershedBoundary(minima,filter,1)
20 boundary = proc.threshold(water,0,0)#the boundary label is 0
21 #boundary.display("boundary",true,false)
22 minima = proc.labelMerge(Mat2UI32(boundary),minima)#the pixel type of the image must be the same f.
23 gradient = proc.gradientMagnitudeDeriche(img,1)
24 water = proc.watershed(minima,gradient)
25 visu.labelForeground(water,img).display()
26