[go: up one dir, main page]

0% found this document useful (0 votes)
27 views6 pages

TP5 Image Segmentation

This document presents different methods of image segmentation, including histogram thresholding, region growing, k-means, and split/merge. MATLAB commands are provided to apply these algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views6 pages

TP5 Image Segmentation

This document presents different methods of image segmentation, including histogram thresholding, region growing, k-means, and split/merge. MATLAB commands are provided to apply these algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

ATE Department

Master's degree in Telecommunications Systems


Semester 2 / May 2020
TP 5
Module: Image Processing Image Segmentation
But

Discuss some image segmentation methods:


 Thresholding method on histogram
 Method of growth for regions
 K-means algorithm
 Division/merging method.

1. Segmentation by the histogram thresholding method


 When the regions in the image have distinctly different gray levels, one can easily them
distinguish on the histogram (distinct areas)
 and by carefully calculating the separation thresholds between the histogram zones, we will arrive at
therefore separate the points of the regions in question and consequently segment them.
 Otsu's method calculates one or more separation thresholds between areas.
histograms, known as segmentation thresholds for regions in the image

Matlab commands
 allows calculating, using Otsu's method,
thresh = multithresh(I,N); the N segmentation thresholds of the image I.
thres: would be a vector of size × ( )

thresh = multithresh(I);  if N is not specified, by default N=1, we


will have only one threshold.
would therefore be a scalar
 Quantify (threshold) the image I according to the
threshold or the thresholds contained in thres.
 For each threshold
Is = imquantize(I,thres); o I(x,y)=0 if :
I(x,y)<threshold
o I(x,y)=label if I(x,y)≥threshold
 Is: a label matrix whose
values (labels) are = 1,2,...,N

Allows converting a matrix of labels (labels) Z_label


Zrgb=label2rgb(Z_label);
in a color matrix Zrgb.

Manipulation
1. Read the image 'medtest.png', assign it to the variable I, then display it.
2. Display its histogram. Distinguish the separable areas on the histogram.
3. calculate a segmentation threshold(N = 1)

1
4. segment the image I, to obtain the matrix Is.
 open the Is matrix, and check that its content is made up of 0 and 1
 Display the segmentation result
5. Calculate two segmentation thresholds(N = 2)
6. segment the image I, to obtain the label matrixIs,
 open the matrix Is, and check that its content is made up of 0, 1, and 2
 convert it to a color image
 Display the segmentation result
 commenter
7. Rephrase question 6. for = 3, then4

2. Segmentation by the region growing method

To segment a region having a standard deviation of


1. we start with a seed point of a given gray level (this is the initial value of
the average of )
2. Then we expand the region around the germ by adding to the region all the nearby points.
in the grayscale ( , ) check the condition:
| ( ,) − | ≤ , where is the average of
3. Recalculate the average of after growth
4. Repeat operations 2 and 3, until there is no more growth.

Matlab Commands:
Allows to apply the algorithm of
growth of a single region:
 in image I,
 around the germ (initial pixel) of
coordonnées [x,y],
J = regiongrowing(I,x,y,sigma_R);  the standard deviation of the region sigma_R
 J is the same size as I, it is a
matrix of labels and
where the values are:
. 1 (true): segmented point
. 0 (false): non-segmented point
Allows to extract the coordinates of the
[x, y] = getpts; point of the figure pointed with the
mouse

Manipulation
1. Read the image'medtest.png', assign it to the variable I, and convert it to double precision.
Choose as the germ the point[x y] = [198 359

2
3. Apply the region growth algorithm to segment a region with a standard deviation of 0.2.
around this germ. You will obtain an image (matrix) of labels , which we will call
segmented image.
4. Display the segmented image on the original image
figure, imshow(I+J);

5. Now try to segment a region by selecting the germ using the


mouse.
6. Vary the standard deviation 0.2, 0.3, or 0.4. Comment.
7. We would now like to segment 4 regions, following the steps below:
a) read the image 'medtest.png' and assign it to I, then display it in figure (3)
b) initialize the label matrix to 0: Js=zeros(size(I));
c) perform a loop for i=1:4 (4 is the number of regions)
1. display figure (3)
2. select a seed with the mouse
3. segment the region around the germ for a standard deviation of 0.2 (or 0.3 or 0.4 or ...)
4. Add the segmented region to the label matrix after labeling it.
(to i, for example): Js=Js+J*i;
5. convert the label matrix into a color image, then display it in the
figure(4)
6. take a break before moving on to the next iteration: pause;
 Comment on the results.
 Does the algorithm always converge?

3. Segmentation by the k-means method


 This method aims to divide the image into k statistically homogeneous classes (regions), of which the
gray level values are similar to (close to) the center (mean) of the class.
 This similarity is estimated by calculating the distance between the grayscale value and the center.
 It is necessary to know in advance the number of classes (regions)

Matlab Commands:
Allows for the classification of the points of vector X into k classes
 classification result, label vector
vector)
oIdx is the same size as X.
[idx,C] = kmeans(X,k); the label (classification) of each point X at index i,
is contained in Idx at the same index i.
The possible values of Idx are the values of the labels
(classes) : 1, 2, 3, ..., k.
 C: a vector of size ( × ), it contains the k-centers of
classes.

Rearrange the elements of matrix Z into a column X.


X=im2col(Z, size(Z));  With this syntax, if Z has a size , , the vector
[ ]X will have a
size ×
3
Rearrange the elements of vector X into a matrix Z of size
Z=col2im(X,[1 1], ); [ ]
[ , ].

Manipulation:
1. Read and display the image in grayscale'medtest.png', assign it to the variableI
2. Rearrange the pixels of I into a single column II
3. Apply the k-means algorithm to classify the points of II into 2 regions. You will obtain the
vector of labels (idx) and the centers of classes (C)
 What are the centers of the regions.
4. Rearrange the label vector idx into a matrix Is, then display it.
 Observe the 2 regions
5. Change the number of classes (regions) to 3 and then to 4.
 commenter.

4. Segmentation by the diffusion/fusion method (split and merging)

 It is a method that aims first to divide the image into homogeneous regions according to a certain
criterion (predicate)
 Then operate to merge the obtained regions that are similar according to this same predicate, in
one single region

Matlab commands:
Allows for the segmentation of image I by the division/merging method.
 a: is the minimum size of image blocks during the operation of
J = splitmerge(I, a, division. It is a number 2 to the power of: 1, 2, 4, 8, 16, ...
@predicate);
 @predicate : is a function that contains the criteria of
division/fusion
 the obtained matrix J is a label matrix.

Matlab Functions:
functionflag = predicate(region) Go to the 'predicate' function and change the
sd = std2(region); parameters following by the desired values:
m = mean2(region);  ecart_typestandard deviation of the region to
ecart_type = 20; %%%
val_min = 0; %%%
segmenter
val_max = 200; %%%  val_minminimum value of the gray level
flag = (sd > ecart_type) ... in the region to be segmented
& (m > val_min) & (m < val_max);  val_max:maximum value of the gray level
in the region to be segmented

4
Manipulation:
1.Read and display the image in grayscale.'medtest.png', assign it to the variableI
2. Apply the divide/conquer algorithm,
 by dividing the image into blocks of minimal size2
 according to the predicates :
standard deviation = 20

minimum gray value = 0


maximum gray value = 200
 Display the segmentation result
 commenter
3.Change the minimum division size:2 , 2 , 2 , 2
 commenter
4.For a minimum division size =2 change the values of the predicates, and comment.

5
histogram thresholding
I = imread('medtest.png');
show image(I); figure, show histogram(I);
thres = multithresh(I, 4);
Is = imquantize(I, thres);
Irgb = label2rgb(Is);
figure;
show image(Irgb)

k-means
I = imread('medtest.png');
II=im2col(I,size(I));
[idx,C] = kmeans(II,4);
Is=col2im(idx,[1, 1],size(I));
Is=label2rgb(Is);

growing_region
% case1
I = im2double(imread('medtest.png'));
x=198; y=359;
J = regiongrowing(I,x,y,0.2);
figure, imshow(I+J);
% cas2
[x, y] = getpts; x=uint8(x); y=uint8(y);
J1 = regiongrowing(I,x,y,0.2);
figure, imshow(I+J+J1);
% case 3
figure(3), imshow(I);
Js=zeros(size(I)); %initial segmented image set to 0
for i=1:4
figure(3)
[x, y] = getpts; x=uint8(x); y=uint8(y);
sig=0.4; %écart type = 0.2; 0.3; 0.4, ... ;
J = regiongrowing(I, x, y, sig);
Js=Js+J*i; %label the segmented region J with i
label2rgb(Js);
figure(4), imshow(Js_rgb));
pause,
end

Division/fusion
I = (imread('medtest.png'));
J = splitmerge(I, 4, @predicate);
Jrgb=label2rgb(J);
figure, imshow(Jrgb);

functionflag = predicate(region)
sd = std2(region);
m = mean2(region);
ecart_type = 20;
val_min = 0;
val_max = 200;
(sd > ecart_type) & (m > val_min) & (m < val_max);
6

You might also like