Beruflich Dokumente
Kultur Dokumente
of
Selectionist relaxation:genetic algorithm
applied to
image segmentation
Under Guidance
Of
Respected Mrs. Akshi Kumar
From:
Krishna Chandra Soni
M.Tech – Software Engg.
3rd Sem, 10/MT/SE/FT
Delhi Technological University
1
CERTIFICATE
The work embodied in this minor project has not been submitted for the award
of any other degree to the best of my knowledge.
Acknowledgement
2
It is a great pleasure of mine to have the opportunity to extend my heartiest felt gratitude to
everybody who helped me throughout the course of this project.
It is distinct pleasure to express my deep sense of gratitude and indebtedness to my learned
supervisor Respected Mrs. Akshi Kumar, Assistant Professor, Computer Department for her
invaluable guidance, encouragement and patient reviews. Her continuous inspiration only has
made me complete this dissertation. Without her help and guidance, this dissertation would
have been impossible. She provided the conceptions and theoretical background for this study
as well as suggested us the rational approach. She remained a pillar of help throughout the
project.
With her continuous inspiration only, it becomes possible to complete this dissertation.
I would also like to take this opportunity to present my sincere regards to my teachers Dr.
Daya Gupta and the other staff of computer engineering department for providing me
unconditional and any time access to the resources and guidance.
I am grateful to my parents for their moral support all the time; they have been always around
to cheer me up, in the odd times of this work. I am also thankful to my classmates for their
unconditional support and motivation during this work. Last but not least, I special thanks to
the crowd who are active in the field of Image Mining and Ambiguity issues.
Abstract
3
In this project , implementation of Selectionist relaxation: genetic algorithms is done to
segment image. The method used is an unsupervised (i.e without training tuples) method that
is based on on a fine-grained distributed genetic algorithm. The implemented method do not
requires objective fitness functions unlike other genetic algorithms instead output
segmentation emerges as a by-product of the evolution of a population of chromosomes that
are mapped onto the image and that locally adapt to its features.
Basically what we are doing is that we first selecting the units composed of chromosomes
and then applying relaxation process over selected units. Relaxation pproach composed of
selection, crossover, mutation which is a classical three step GA cycle.
Because the segmentation progressively emerges as a by-product of a relaxation process
mainly driven by selection, the method has been called selectionist relaxation.
TABLE OF CONTENTS
4
Certificate ..........……………………………………………………………..................ii
Acknowledgement……............………………………………………………..............iii
Abstract …….…............………………………………………………………..............iv
1.Introduction............................................................................................................. .....6
2. Method Description.....................................................................................................6
2.2 Algorithm
Details.............................................................................................................................8
3. Conclusion..................................................................................................................10
4. Snapshots ...................................................................................................................11
5. Implementation...........................................................................................................13
6.References....................................................................................................................29
5
1. Introduction
Image segmentation is a low-level image processing task that aims at partitioning an image
into homogeneous regions and output of which can be fed as input to higher-level processing
tasks, such as model-based object recognition systems. Today we are available with large
number of segmentation techniques and large number of researches are being carried out to
find new methods in this domain.
Genetic algorithm is one of the approach that is applied in this domain to enhance results.
The idea behind this approach is that natural evolution is a search process that seeks to
optimize the structures it generates. In a GA, a population of candidate solutions to the
problem to be solved, which are encoded into chromosomes, is iteratively updated through
mechanisms of selection, crossover, and mutation. With time, good solutions spread within
the population by being selected to replace bad ones, while new, possibly better, solutions are
permanently being generated from previous ones through crossover and mutation.
But problem with existing GA is that they requires an objective function which are and may
be complicated to predict .More exact the objective function , more will be the accuracy
hence better will be the results. Each objective functions requires one or more parameter that
are dufficult to predict , also they need to be adjusted with evolution which is quite complex
task. Thus evaluating a segmentation result is itself a difficult task. To date, no standard
evaluation method prevails and different measures may yield distinct rankings.
To avoid these drawbacks, Philippe Andrey advocated an unsupervised GA-based image
segmentation method that does not require the definition of an objective fitness function
evaluating candidate segmentation results. The output segmentation instead emerges as a by-
product of the evolution of a population of chromosomes that are mapped onto the image and
that locally adapt to its features.
2. Method Description
6
are restricted to occur within subsets of individuals.
In fine grained distributed scheme , the unique population is mapped onto a grid and
interactions are restricted to occur among neighboring chromosomes. The use of a fine-
grained scheme is here imposed by the two-dimensional image structure.
After having defined the chromosomes, herein referred to as units, and the fitness function, we
describe the three main steps of the GA cycle (selection, crossover, and mutation).
7
the unit is located. The mutation rate m is the probability that a position along the
chromosome undergoes a mutation. Mutating the ith allele ui of the unit located at site
s consists of replacing ui by ui+d , where d is randomly sampled from the Gaussian
distribution N(0,m*variance). m is referred to as mutation amplitude . It is computed
over the k 3 k image window centeredon s.
• The above process is repeated till we segment entire image.
A unit is a real-coded chromosome of length p that represents apoint in the feature space. The
coordinates of the unit in feature space are referred to as its alleles. A population of units is
built by assigning a unit to each site of the image. At site s, the fitness fs of the associated unit
us = (u1,...,up) is computed as the opposite of the city-block distance between us and vs, so that
a unit whose alleles are close to the actual local pixels values will have a high fitness: p
Each unit also owns a label used as a region label for the pixel where the unit is located.
Initially, all units have distinct labels and their alleles are randomly generated by uniform
sampling in the range of gray level values (typically, 0-255).
2.2.2.1. Selection
The selection scheme implemented in SR is a local tournament selection .It consists in replacing the
unit located at site s by the unit having the highest fitness value in the neighborhood. The
neighborhood hs, at site s is arbitrarily defined as the 3* 3 set of units centered on s.To avoid potential
side-effects of sequential replacement,local tournaments take place simultaneously at all sites.As a
result of local selections, a unit can be surrounded by neighboring units all having the same label as
its own label. Such a unit will be referred to as a NonEdgeUnit.Alternatively, a unit can have at least
one neighbor with a different label and will then be referred to as an EdgeUnit. Our selection
8
scheme departs from other local tournament selection implemented in fine grained GAs ,since
at a site occupied by an EdgeUnit, the tournament involves an additional unit picked at
random in the population. This modification allows units to propagate over regions sharing
identical gray level characteristics though not being connected.
Crossover and mutation are applied to a unit if and only if it is a NONEDGEUNIT. A unit that
fulfils this mating restric
tion is allowed to recombine with one of its eight neighbors, which is picked at random.
Crossover is generalized uniform crossover [24,25]. For each position along the two
chromosomes, an exchange occurs with probability x, referred to as crossover rate.
The mutation of a unit depends on the local properties of the image at the site where the unit is
9
located. The mutation rate /A is the probability that a position along the chromosome
undergoes a mutation. Mutating the ith allele ui of the unit located at site s consists of replacing
ui by ui + 6, where d is randomly sampled from the Gaussian distribution 9\2 (0,mas). m is
referred to as mutation amplitude, while as is the variance of the local gray level distribution
around site s. It is computed over the kX k image window centered on s.
3. Conclusion
10
4. Snapshots
Snapshot 1
11
Snapshot 2
Snapshot 3
12
Snapshot 4
5. Implementation
ImageSegmentation.java
package segmentation;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.HashMap;
import java.util.Random;
import java.awt.*;//for JFrame and JLabel etc
/**
* ImageSegmentation is a class encapsulate the process of segmentation using GA
*/
public class ImageSegmentation {
//windows size
final static int WINDOW_K = 3;
//the probability of crossover
final static double CROSSOVER_RATE = 0.5;
//the total generations of the process
final static int GENERATION = 64;
//the probability of mutation
final static double MUTATION_RATE = 1.0 / GENERATION;
//the mutation amplitude of the local gray level distribution
final static double MUTATION_AMPLITUDE = 1.0;
//the max distance of the unit and local site
final static int MAX_DISTANCE = -2304;
//the original image which is going to be processed
BufferedImage im;
//the width and height of the image
13
int nWidth, nHeight;
//the matrix of the units
Unit[][] population;
/**
* constructor
*/
public ImageSegmentation(){
im = null;
population = null;
}
/**
* load a image from file
*/
public void loadImage(String path){
try{
InputStream in = new BufferedInputStream(new FileInputStream(path));
im = ImageIO.read(in);
nWidth = im.getWidth();
nHeight = im.getHeight();
}
catch(IOException e){
System.out.println("Read image failed.");
}
initial();
}
/**
* initialization
*/
public void initial(){
int[] ch = null;
int length = WINDOW_K * WINDOW_K;
int gray = 0;
14
int x = 0, y = 0;
Random rand = new Random();
population = new Unit[nHeight + 2][];
for(int i = 0; i < nHeight + 2; i++)
{
population[i] = new Unit[nWidth + 2];
for(int j = 0; j < nWidth + 2; j++)
{
y = i;
x = j;
if(y == 0)
y++;
if(x == 0)
x++;
if(y == nHeight + 1)
y--;
if(x == nWidth + 1)
x--;
gray = (im.getRGB(x - 1, y - 1) & 0xff) % 256;
population[i][j] = new Unit(gray, i * nWidth + j);
//initial chromosome
ch = population[i][j].getChromo();
//randomly initial chromosome
for(int m = 0; m < length; m++)
{
ch[m] = rand.nextInt(256);
}
}
}
//compute local gray level distribution around site (j,i)
double mean;
double variance;
double temp = 0;
for(int i = 1; i < nHeight + 1; i++)
15
for(int j = 1; j < nWidth + 1; j++)
{
mean = 0;
for(int m = -WINDOW_K / 2; m < (WINDOW_K + 1) / 2; m++)
for(int n = -WINDOW_K / 2; n < (WINDOW_K + 1) / 2; n++){
mean += population[i + m][j + n].getGray();
}
variance = 0;
temp = 0;
for(int m = -WINDOW_K / 2; m < (WINDOW_K + 1) / 2; m++)
{
for(int n = -WINDOW_K / 2; n < (WINDOW_K + 1) / 2; n++)
{
temp = population[i + m][j + n].getGray() - mean;
variance += temp * temp;
}
population[i][j].setCita(Math.sqrt(variance * MUTATION_AMPLITUDE));
}
}
/**
* fitness function
*/
public int getFitness(int x, int y, Unit u){
int sum = 0, index = 0;
int xt = 0, yt = 0;
int[] c = u.getChromo();
for(int i = -WINDOW_K / 2; i < (WINDOW_K + 1) / 2; i++)
for(int j = -WINDOW_K / 2; j < (WINDOW_K + 1) / 2; j++){
yt = y + i;
xt = x + j;
if(yt < 0 || yt >= nHeight + 2 || xt < 0 || xt >= nWidth + 2)
continue;
index = (i + WINDOW_K / 2) * WINDOW_K + (j + WINDOW_K / 2);
16
sum += Math.abs(c[index] - population[yt][xt].getGray());
}
sum = 0 - sum;
return sum;
}
/**
* selection process
*/
public void selection(){
int distance = MAX_DISTANCE;
int x = 0, y = 0;
int temp = 0;
int[] v = null, u = null;
int length = WINDOW_K * WINDOW_K;
boolean isEdge = false;
int label;
17
}
//deal with the EdgeUnit
if(population[i][j].getEdge()){
Random rand = new Random();
int rx = rand.nextInt(nWidth);
int ry = rand.nextInt(nHeight);
for(int m = -1; m < 2; m++)
for(int n = -1; n < 2; n++)
{
temp = getFitness(j + n, i + m, population[ry][rx]);
if(temp > distance){
distance = temp;
x = rx;
y = ry;
}
}
}
//replacement
isEdge = false;
u = population[y][x].getChromo();
label = population[y][x].getLabel();
population[i][j].setLabel(label);
for(int m = 0; m < length; m++)
v[m] = u[m];
for(int m = -1; m < 2; m++)
for(int n = -1; n < 2; n++){
if(population[i + m][j + n].getLabel() != label){
isEdge = true;
break;
}
}
population[i][j].setEdge(isEdge);
}
}
18
/**
* crossover process
*/
public void crossover(){
Random rand = new Random();
for(int i = 1; i < nHeight + 1; i++)
for(int j = 1; j < nWidth + 1; j++){
if(!population[i][j].getEdge()){
int x = 0, y = 0;
while(x == 0 && y == 0){
x = rand.nextInt(3) - 1;
y = rand.nextInt(3) - 1;
}
int[] chv = population[i][j].getChromo();
int[] chu = population[y + i][x + j].getChromo();
int temp = 0;
for(int m = 0; m < 9; m++){
if(rand.nextDouble() < CROSSOVER_RATE){
temp = chv[m];
chv[m] = chu[m];
chu[m] = temp;
}
}
}
}
}
/**
* mutation process
*/
public void mutation(){
int length = WINDOW_K * WINDOW_K;
for(int i = 1; i < nHeight + 1; i++)
19
for(int j = 1; j < nWidth + 1; j++){
if(!population[i][j].getEdge()){
Random rand = new Random();
int[] ch = population[i][j].getChromo();
for(int m = 0; m < length; m++){
if(rand.nextDouble() < MUTATION_RATE){
ch[m] += rand.nextGaussian() * population[i][j].getCita();
}
}
}
}
}
/**
* print the original image, processed image and the segmentation result
*/
public void print(){
//create a new image to show the segmentation result
int color = 0;
int[] ch = null;
BufferedImage image = new BufferedImage(nWidth, nHeight,
BufferedImage.TYPE_INT_ARGB);
for(int i = 1; i < nHeight + 1; i++){
for(int j = 1; j < nWidth + 1; j++){
ch = population[i][j].getChromo();
color = ch[4] % 256;
//color = population[i][j].getGray();
color += (color << 16) + (color << 8) + (255 << 24);
image.setRGB(j - 1, i - 1, color);
}
}
//create a new image to show the label result
BufferedImage labeledImage = new BufferedImage(nWidth, nHeight,
BufferedImage.TYPE_INT_ARGB);
20
HashMap<Integer, Integer> labmap = new HashMap<Integer, Integer>(nWidth * nHeight /
8);
int label = 0;
int lcolor = 0;
Random rand = new Random();
int red = 128, blue = 128, green = 128;
for(int i = 1; i < nHeight + 1; i++){
for(int j = 1; j < nWidth + 1; j++){
label = population[i][j].getLabel();
if(labmap.containsKey(Integer.valueOf(label))){
lcolor = labmap.get(Integer.valueOf(label)).intValue();
labeledImage.setRGB(j - 1, i - 1, lcolor | 0xff000000);
continue;
}
red = rand.nextInt(256);
green = rand.nextInt(256);
blue = rand.nextInt(256);
lcolor = (red << 16) + (green << 8) + blue;
labmap.put(Integer.valueOf(label), Integer.valueOf(lcolor));
labeledImage.setRGB(j - 1, i - 1, lcolor | 0xff000000);
}
}
// Use a label to display the image
JFrame frame = new JFrame(); ;
JLabel _jlabel = new JLabel(new ImageIcon(im));
JLabel _jlabel2 = new JLabel(new ImageIcon(image));
JLabel _jlabel3 = new JLabel(new ImageIcon(labeledImage));
frame.getContentPane().add(_jlabel, BorderLayout.CENTER);
frame.getContentPane().add(_jlabel2, BorderLayout.EAST);
frame.getContentPane().add(_jlabel3, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
//close window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21
}
22
GaussRandom.java
package segmentation;
import java.util.Random;
/*
FTable: normal distribution function table
fTable: normal probability density table
*/
public class GaussRandom {
final int GAUSS_TABLE_LENGTH = 256;
double miu, cita;
double[] FTable = new double[GAUSS_TABLE_LENGTH];
double[] fTable = new double[GAUSS_TABLE_LENGTH];
public GaussRandom(double m, double c){
miu = m;
cita = c;
}
/ * Generate the normal distribution function and the normal probability density function
table, the table width is 3 times the variance
Normal distribution function with mean 0 and variance 1 * /
fTable[0] = Math.exp(-x * x / 2) * C;
for(i = 1; i < GAUSS_TABLE_LENGTH; i++){
x += h;
temp = Math.exp(-x * x / 2) * C;
23
x += h;
24
Unit.java
package segmentation;
/**
* Unit is a class of the informations of chromosome and gray level about the site of the
image
*/
public class Unit {
//region label
private int nLabel;
//gray level
private int nGray;
//chromosome sequence
private int[] chromosome;
//whether the unit is a EdgeUnit
private boolean bIsEdge;
//the variance of the local gray level distribution around local site
private double dCita;
/**
* constructor
* @param gray:gray level of local site
*/
public Unit(int gray)
{
this.nGray = gray;
this.nLabel = 0;
int k = ImageSegmentation.WINDOW_K;
chromosome = new int[k * k];
this.bIsEdge = true;
this.dCita = 0;
}
25
/**
* constructor
* @param gray:gray level of the local site
* @param label:segmentation label of the local site
*/
public Unit(int gray, int label)
{
this.nGray = gray;
this.nLabel = label;
int k = ImageSegmentation.WINDOW_K;
chromosome = new int[k * k];
this.bIsEdge = true;
this.dCita = 0;
}
/**
* get the gray level of the local site
* @return: gray level of the local site
*/
public int getGray(){
return nGray;
}
/**
* set the gray level of the local site
* @param gray: gray level of the local site
*/
public void setGray(int gray){
this.nGray = gray;
}
/**
* get the segmentation label of the local site
* @return : the segmentation label of the local site
*/
public int getLabel(){
26
return nLabel;
}
/**
* set the segmentation label of the local site
* @param label : the segmentation label of the local site
*/
27
* @return : the variance
*/
public double getCita(){
return this.dCita;
}
/**
* set the variance of the gray level distribution around local site
* @param c : the variance
*/
public void setCita(double c){
this.dCita = c;
}
}
28
6. References:
29