Вы находитесь на странице: 1из 14

Digital Image processing Lab

Islamic University – Gaza


Engineering Faculty
Department of Computer Engineering
2013
EELE 5110: Digital Image processing Lab
Eng. Ahmed M. Ayash

Lab # 10

Image Segmentation

April 27, 2013


1. Objectives
 To understand image segmentation.
 To understand object detection.
 Implement a Cell Detection Using Image Segmentation

2. Theory
Image segmentation
Segmentation subdivides an image into its constituent regions or objects that have similar
features (Intensity, Histogram, mean, variance, Energy, Texture, ...etc.) according to a set
of predefined criteria
Most segmentations algorithms we will consider are based on one of two basic properties
of intensity values:

Discontinuity
The strategy is to partition an image based on abrupt changes in intensity
 Detection of gray level discontinuities:
– Point detection
– Line detection
– Edge detection
• Gradient operators
• LoG: Laplacian of Gaussian

Similarity
The strategy is to partition an image into regions that are similar according to a set of
predefined criteria.

In our Lab we will focus on Detection of gray level discontinuities.

1
Goal of Image analysis
• Extracting information from an image
– Step1: segment the image ---> objects or regions
– Step2: describe and represent the segmented regions in a form suitable for
computer processing
– Step3: image recognition and interpretation

2.1 Point, Line and Edge Detection


The most common way to look for discontinuities is to run a mask through the image.
The response R of mask (Spatial filter) at any point in the image is given by:

Where zi is the intensity of the pixel associated with mask coefficient wi

2.1.1 Point Detection


The detection of isolate points embedded in constant or nearly constant areas is detected
as:

Fig1: A mask for point detection

2
where T is a nonnegative threshold. Point detection is implemented in matlab using
function imfilter. If T is given, the following command implements the point detection
approach:
g = abs(imfilter(double(f),w)) >= T;

where f is the input image, w is an appropriate point-detection mask, and g is the


resulting image.

Example1:
%%example1.m
clc;
clear all;
close all;
f = imread('Lab10_1.jpg');
w=[-1,-1,-1;-1,8,-1;-1,-1,-1];
g=abs(imfilter(double(f),w));
T=max(g(:));
g=g>=T; %(T given threshold)

subplot(121),imshow(f),title('original image');
subplot(122),imshow(g),title('Result of point detection');

Output:

3
2.1.2 Line Detection
The next level of complexity is to try to detect lines. Masks for lines of different
directions:

Fig2: Line detection masks

Notes:
o These filter masks would respond more strongly to lines.
o Note that the coefficients in each mask sum to zero, indicating a zero response from
the masks in areas of constant gray level.
o If we are interested in detecting lines in a specified direction (e.g. vertical), we could
use the mask associated with that direction and threshold its output.
o If interested in lines of any directions run all 4 masks and select the highest response.
o These filters respond strongly to lines of one pixel thick of the designated direction
and correspond closest to the direction defined by the mask.

Let R1, R2, R3, R4 denotes the responses of the masks in Fig2, from left to right.
If at a certain point in the image, |Ri|>|Rj|, for all j ≠ i, that point is said to be more likely
associated with a line in the direction of mask i.

Example2:
%%example2.m
clc;
clear all;
close all;
f = imread('Lab10_2.jpg');
w=[2,-1,-1;-1,2,-1;-1,-1,2]; %%-45
g1=imfilter(double(f),w);

g=abs(g1);
gtop=g1(1:120,1:120);
gtop=pixeldup(gtop,4);%%Duplicates pixels of an image in both
directions.

4
gbot=g1(end-119:end,end-119:end);
gbot=pixeldup(gbot,4);

T=max(g(:));
gg=g>=T;

subplot(121),imshow(f),title('original image');
subplot(122),imshow(g1,[]),title('g1:Result of processing with -45
detector');
figure
subplot(121),imshow(gtop,[]),title('Zoomed view of the top left region
of g1');
subplot(122),imshow(gbot,[]),title('Zoomed view of the bottom right
region of g1');
figure
subplot(121),imshow(g,[]),title('Absolute value of g1');
subplot(122),imshow(gg),title('Final Result');

Output:

5
2.1.3 Edge Detection using Function Edge
What is an edge?
A set of connected pixels that lie on the boundary between two regions.

Edge detection is the most common approach for detecting meaningful discontinuities.
Edge is detected by:
 First‐order derivative (gradient operator)
 Second‐order derivative (laplacian operator)

6
The gradient of a 2-D function f(x,y) is defined as:

The magnitude of this vector is

Second-order derivatives are generally computed using the Laplacian, which as follows:

Two general criteria for edge detection:


 Find places where the first derivative of the intensity is greater in magnitude than a
specified threshold
 Find places where the second derivative of the intensity has a zero crossing.

Edge function
edge function Find edges in intensity image.

Syntax
[g, t] = edge(f, 'method', parameters)

Where f is the input image, method is one of the approches listed in Table1 below.
The parameters you can supply differ depending on the method you specify. And g is binary
output image, t is threshold value.
If you do not specify a method, edge uses the Sobel method.

Description
Edge takes an intensity or a binary image f as its input, and returns a binary image g of the same
size as f, with 1's where the function finds edges in f and 0's elsewhere.

7
Edge supports six different edge-finding methods:
The Sobel Finds edges using the Sobel approximation to the derivative.
It returns edges at those points where the gradient of I is maximum.
The Prewitt Finds edges using the Prewitt approximation to the derivative.
It returns edges at those points where the gradient of I is maximum.
The Roberts Finds edges using the Roberts approximation to the derivative.
It returns edges at those points where the gradient of I is maximum.
The Laplacian of Gaussian Finds edges by looking for zero crossings after filtering I with a Laplacian
of Gaussian filter.
The zero-cross Finds edges by looking for zero crossings after filtering I with a filter you
specify.
The Canny Finds edges by looking for local maxima of the gradient of I. The gradient
is calculated using the derivative of a Gaussian filter. The method uses two
thresholds, to detect strong and weak edges, and includes the weak edges
in the output only if they are connected to strong edges. This method is
therefore less likely than the others to be "fooled" by noise, and more
likely to detect true weak edges.

The most powerful edge-detection method that edge provides is the Canny method.

Example3:
%%example3.m
clc;
clear all;
close all;
f = imread('Lab10_3.gif');
[gv,t]=edge(f,'sobel','vertical');%%automatically threshold
subplot(121),imshow(f),title('original image');
subplot(122),imshow(gv),title('Results using vertical Sobel with
automatically t');

8
gv=edge(f,'sobel',0.15,'vertical'); %%specified threshold
gboth=edge(f,'sobel',0.15); %%ver and hor
figure
subplot(121),imshow(gv),title('Results using vertical Sobel with
specified t');
subplot(122),imshow(gboth),title('Results using ver and hor Sobel with
specified t');

w45=[-2 -1 0;-1 0 1;0 1 2];%%45


g45=imfilter(double(f),w45);
T=0.3*max(abs(g45(:)));
g45=g45>=T;
figure
subplot(121),imshow(g45),title('The edge oriented at 45 ');
ww45=[0 1 2;-1 0 1;-2 -1 0]; %%-45
gg45=imfilter(double(f),ww45);
T2=0.3*max(abs(gg45(:)));
gg45=gg45>=T2;
subplot(122),imshow(gg45),title('The edge oriented at -45 ');

Output:

9
2.2 Detecting a Cell Using Image Segmentation
An object can be easily detected in an image if the object has sufficient contrast from the
background. We use edge detection and basic morphology tools to detect a prostate
cancer cell.

Example4:
%%example4.m
clc;
clear all;
close all;
%%%%%Step 1: Read Image%%%%%
I = imread('Lab10_5.gif');
%%%%Step 2: Detect Entire Cell%%
BWs = edge(I, 'sobel', (graythresh(I) * .1)); %%graythresh computes a
threshold
%%%%%%Step 3: Fill Gaps%%%%
%%% line, length 3 pixels, angle 90 degrees
se90 = strel('line', 3, 90); % strel Create morphological structuring
element.
se0 = strel('line', 3, 0);%%% line, length 3, angle 0 degrees
%%%%%%%%Step 4: Dilate the Image%%%%%%%%%%%%%%%%%%
BWsdil = imdilate(BWs, [se90 se0]);
%%%%%%%%%%Step 5: Fill Interior Gaps%%%%
BWdfill = imfill(BWsdil, 'holes'); %%filling in the background from the
edge of the image.
%%%%%%%Step 6: Remove Connected Objects on Border%%%
BWnobord = imclearborder(BWdfill, 4);
%%%%%%%%%Step 7: Smooth the Object%%%%%%
seD = strel('diamond',1); %%R=1 is the distance from th structuring
element origin to the points of the diamond.
BWfinal = imerode(BWnobord,seD);
BWfinal = imerode(BWfinal,seD);
%%%%%Step 8: Displaying the Segmented Object%%%%%

11
BWoutline = bwperim(BWfinal); %%Find perimeter of objects in binary
image.

Segout = I;
Segout(BWoutline) = 255;

subplot(421),imshow(I),title('1.original image');
subplot(422),imshow(BWs),title('2.binary gradient mask using sobel');
subplot(423),imshow(BWsdil),title('3.dilated gradient mask');
subplot(424),imshow(BWdfill),title('4.binary image with filled holes');
subplot(425),imshow(BWnobord),title('5.cleared border image');
subplot(426),imshow(BWfinal),title('6.segmented image');
subplot(4,2,7:8),imshow(Segout),title('7.outlined original image');

Output:

11
Example4 Explanation:
Step 1: Read Image
Read in ' Lab10_5.gif', which is an image of a prostate cancer cell.

Step 2: Detect Entire Cell


Two cells are present in this image, but only one cell can be seen in its entirety. We detected this
cell. Another word for object detection is segmentation. The object to be segmented differs
greatly in contrast from the background image. Changes in contrast can be detected by operators
that calculate the gradient of an image. One way to calculate the gradient of an image is the
Sobel operator, which creates a binary mask using a user-specified threshold value. We
determine a threshold value using the graythresh function. To create the binary gradient mask,
we use the edge function.

Step 3: Fill Gaps


The binary gradient mask shows lines of high contrast in the image. These lines do not quite
delineate the outline of the object of interest. Compared to the original image, you can see gaps
in the lines surrounding the object in the gradient mask. These linear gaps will disappear if the
Sobel image is dilated using linear structuring elements, which we can create with the strel
function.

Step 4: Dilate the Image


The binary gradient mask is dilated using the vertical structuring element followed by the
horizontal structuring element. The imdilate function dilates the image.

Step 5: Fill Interior Gaps


The dilated gradient mask shows the outline of the cell quite nicely, but there are still holes in the
interior of the cell. To fill these holes we use the imfill function.

Step 6: Remove Connected Objects on Border


The cell of interest has been successfully segmented, but it is not the only object that has been
found. Any objects that are connected to the border of the image can be removed using the
imclearborder function. The connectivity in the imclearborder function was set to 4 to remove
diagonal connections.

Step 7: Smooth the Object


Finally, in order to make the segmented object look natural, we smooth the object by eroding the
image twice with a diamond structuring element. We create the diamond structuring element
using the strel function.

Step 8: Displaying the Segmented Object


An alternate method for displaying the segmented object would be to place an outline around the
segmented cell. The outline is created by the bwperim function.

12
4. Appendix

Pixeldup.m
function B = pixeldup(A, m, n)
%PIXELDUP Duplicates pixels of an image in both directions.
% B = PIXELDUP(A, M, N) duplicates each pixel of A, M times in the
% vertical direction and N times in the horizontal direction.
% Parameters M and N must be integers. If N is not included, it
% defaults to M.

% Check inputs.
if nargin < 2
error('At least two inputs are required.');
end
if nargin == 2
n = m;
end

% Generate a vector with elements 1:size(A, 1).


u = 1:size(A, 1);
% Duplicate each element of the vector m times.
m = round(m); % Protect against nonintergers.
u = u(ones(1, m), :);
u = u(:);

% Now repeat for the other direction.


v = 1:size(A, 2);
n = round(n);
v = v(ones(1, n), :);
v = v(:);
B = A(u, v);

Homework

Write a Matlab code to find the edges of the Lab10_4.jpg image using the Prewitt and
Canny method.

13

Вам также может понравиться