Lab10 Image Segmentation1
Lab10 Image Segmentation1
Lab # 10
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.
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
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;
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:
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:
Second-order derivatives are generally computed using the Laplacian, which as follows:
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');
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.
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
Homework
Write a Matlab code to find the edges of the Lab10_4.jpg image using the Prewitt and
Canny method.
13