Sie sind auf Seite 1von 29

Implementation

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

Delhi Technological University


(Govt. of National Capital Territory of Delhi)
BAWANA ROAD, DELHI – 110042

This is certified that the minor project report entitled as Implementation of


Selectionist relaxation: genetic algorithm applied to image segmentation is
a work of Krishna Chandra Soni, M-Tech (SE) a student of Delhi
Technological University. This work is completed under my direct
supervision and guidance and forms a part of Master of Technology
(Software Engg.)course and curriculum. He has completed his work with
utmost sincerity and diligence.

The work embodied in this minor project has not been submitted for the award
of any other degree to the best of my knowledge.

Mrs Akshi Kumar


Assistant Professor & Project Guide
Dept. Of Computer Engineering
DelhiTechnological University
India

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.

Krishna Chandra Soni


M.Tech – Software Engg.
3rd Sem, 10/MT/SE/FT
Delhi Technological University

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.1 Brief description about the concept used....................................................................7

2.2 Algorithm
Details.............................................................................................................................8

2.2.1 Selection Phase: Units and fitness.............................................................................8

2.2.2. Relaxation cycle .....................................................................................................8

2.2.2.1. Selection .............................................................................................................8

2.2.2.2. Crossover and mutation........................................................................................9

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

Selectionist relaxation (SR) is an unsupervised,fine-grained distributed genetic algorithm


applied image segmentation,which is basically a two step proceedure: Selection followed by
Relaxation, which is a three step proceedure consisting of selection, crossover, mutation.
By contrast to the standard GA scheme ,wherein each chromosome can interact through
selection and crossover with any other one in the population, interactions in distributed GAs

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).

2.1 Brief description about the concept used

• In the proposed algorithm , image to be segmented is considered as an artificial


environment, containing collection of different features.
• Different features forms different regions with different characterstics.
• Hence each segmented part are region with different characterstics. And each region
have unique name or label. Region is collection of units.
• Unit is combination of chromosome and their fitness value.
• Initially at begining , each pixel of image represents a unit labelled differently and
thus chromosomes are distributed all over region.
• We apply selection process over entire image using image site windw of size 3*3
which is set of neighboorhood pixels.to find and replace the current observed unit
with the unit which leads to effective segmentation based on fitness value.
• After which we apply relaxation process which contains a cycle of selection , cross
over and mutation.
• During selection phase we select same label units to determine connected region,
• As a result of selection, if a unit is surrounded by other units having same label then
such a unit is reffered as NONEDGE unit , if a unit have atleast one neighboor having
different label then it will be reffered as EDGE unit. In this way we can be able to
determine different regions , which is collection of units of same label, within an
image.
• After selection we apply cross over and mutation , which is only applied to units if it
is a NONEDGE unit.
• During crossover, a NONEDGE unit is recombined ,with one of its eight neighboor
which is picked randomly. Recombination is done with probability X , reffered to as
crossover rate.
• The mutation of a unit depends on the local properties of the image at the site where

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.

2.2 Algorithm Details

2.2.1 Selection Phase: Units and fitness

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. Relaxation cycle


The initial random population is then fed into the relaxation process, which consists in
iterating the classical three-step GA cycle (selection, crossover, mutation).

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.

Fig. 1. Pseudo-code for the selection step.

2.2.2. 2. Crossover and mutation

Crossover and mutation are applied to a unit if and only if it is a NONEDGEUNIT. A unit that
fulfils this mating restric

Fig. 2. Pseudo-code for the crossover step.

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.

Fig. 3. Pseudo-code for the mutation step

3. Conclusion

We have described an unsupervised image segmentation method based on a fine-grained


distributed GA. Two main points characterize the departure of SR from other applications of
GAs to the problem of image segmentation .Firstly, The method is generic since it can be
applied to segmentation according to any criterion (gray level, texture, depth,etc.).
Secondly,no segmentation evaluation criterion is required in SR. Also we try to overcome the
difficulty of evaluating parameter and thieir tuning on existing segmentation algorithms, con-
ventional applications of GAs to the segmentation problem introduce the difficulty of tuning
the parameters of the GA itself. Besides, the conventional approach requires an objective
fitness function, which may itself depend on parameters that have to be heuristically tuned.

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;

for(int i = 1; i < nHeight + 1; i++)


for(int j = 1; j < nWidth + 1; j++){
//local tournament selection
v = population[i][j].getChromo();
temp = 0;
x = y = 0;
distance = MAX_DISTANCE;
for(int m = -1; m < 2; m++)
for(int n = -1; n < 2; n++)
{
temp = getFitness(j + n, i + m, population[i + m][j + n]);
if(temp > distance){
distance = temp;
x = j + n;
y = i + m;
}

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
}

public static void main(String prog[]){


BufferedReader _in = new BufferedReader(new InputStreamReader(System.in));
String path = "Fig8.02(b).jpg";
try{
System.out.print("Input image path: ");
path = _in.readLine();
}
catch(Exception e){
System.out.print("Wrong path");
}
ImageSegmentation ig = new ImageSegmentation();
ig.loadImage(path);
for(int step = 0; step < ImageSegmentation.GENERATION; step++){
ig.crossover();
ig.mutation();
ig.selection();
}
ig.print();
}
}

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 * /

public final void initial(){


int i = 0;
double h = 6.0 / GAUSS_TABLE_LENGTH / 2.0; //²½³¤
double x = -3, temp = 0;
double C = 1 / Math.sqrt(2 * Math.PI);

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;

/ * Calculate normal probability density function * /


fTable[i] = Math.exp (-x * x / 2) * C;
/ * Simpson numerical integration formula normal function * /

FTable[i] = FTable[i - 1] + (fTable[i - 1] + 4 * temp + fTable[i]) * h / 3;


}
}
public double generateRand(){
double dRand = 0.0;
double h = 6.0 / GAUSS_TABLE_LENGTH;
Random rand = new Random();
double temp = rand.nextDouble();
for(int i = 0; i < GAUSS_TABLE_LENGTH; i++){
if(temp <= FTable[i]){
dRand = (-3 + h * i) + (temp - FTable[i-1]) / (FTable[i] - FTable[i-1]) * h;
break;
}
}
return dRand;
}
public static void main(String prog[]){
GaussRandom rand = new GaussRandom(0, 1);
rand.initial();
for(int i = 0; i < 10; i++)
System.out.println(rand.generateRand());
}
}

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
*/

public void setLabel(int label){


this.nLabel = label;
}
/**
* get the chromosome sequence of the local site
* @return : the chromosome sequence of the local site
*/
public int[] getChromo(){
return this.chromosome;
}
/**
* get the edge information of the local site
* @return : if the local site is a EdgeUnit, return true, else, return false
*/
public boolean getEdge(){
return bIsEdge;
}
/**
* set the edge information of the local site
* @param edge : whether the local site is a EdgeUnit
*/
public void setEdge(boolean edge){
this.bIsEdge = edge;
}
/**
* get the variance of the gray level distribution around 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:

Selectionist relaxation: genetic algorithms applied to image segmentation


Philippe Andrey* AnimatLab, De ´partement de Biologie, Ecole Normale Supe ´rieure, 46
rue d’Ulm, 75230 Paris Cedex 05, France

29

Das könnte Ihnen auch gefallen