0% found this document useful (0 votes)
90 views10 pages

MATLAB Source Codes Otsu Thresholding Method: All 'Angiogram1 - Gray - JPG'

The document contains MATLAB source code for image segmentation and edge detection algorithms including: 1) Otsu's thresholding method, adaptive thresholding, thresholding with image partitioning, and Otsu's method for uneven illumination. 2) Canny edge detection using different sigma parameter values. 3) Log of Gaussian, region growing, and split and merge segmentation methods. The region growing and split/merge methods make use of callback functions.

Uploaded by

Mohammed Almalki
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)
90 views10 pages

MATLAB Source Codes Otsu Thresholding Method: All 'Angiogram1 - Gray - JPG'

The document contains MATLAB source code for image segmentation and edge detection algorithms including: 1) Otsu's thresholding method, adaptive thresholding, thresholding with image partitioning, and Otsu's method for uneven illumination. 2) Canny edge detection using different sigma parameter values. 3) Log of Gaussian, region growing, and split and merge segmentation methods. The region growing and split/merge methods make use of callback functions.

Uploaded by

Mohammed Almalki
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/ 10

MATLAB Source Codes

Otsu Thresholding method

clc
clear all
x=imread('angiogram1_gray.jpg');
T=graythresh(x);
y=im2bw(x,T);
imshow(y)

Adaptive Thresholding Method Code

clear all
clc
x=imread('angiogram1_gray.jpg');
x1=im2single(x);
[m n]=size(x);
Err = 1;
T0=(max(max(x1))+min(min(x1)))/2
while Err > 0.0001,
u1=0;
u2=0;
cnt1=0;
cnt2=0;
for i=1:m
for j=1:n
if x1(i,j)<= T0
u1=u1+x1(i,j);
cnt1=cnt1+1;
else
u2=u2+x1(i,j);
cnt2=cnt2+1;
end
end
end
u1=u1/cnt1;
u2=u2/cnt2;
T=(u1+u2)/2
Err=abs(T-T0);
if Err > 0.0001
T0=T;
end
end
y=im2bw(x,T0);
figure(1);imshow(x);
figure(2);imshow(y)
Adaptive Threshold in the case for uneven illumination – Global threhold

clear all
clc
x=imread('barcode_uneven.bmp');
x=rgb2gray(x);
x1=im2single(x);
[m n]=size(x);
Err = 1;
T0=(max(max(x1))+min(min(x1)))/2
while Err > 0.0001,
u1=0;
u2=0;
cnt1=0;
cnt2=0;
for i=1:m
for j=1:n
if x1(i,j)<= T0
u1=u1+x1(i,j);
cnt1=cnt1+1;
else
u2=u2+x1(i,j);
cnt2=cnt2+1;
end
end
end
u1=u1/cnt1;
u2=u2/cnt2;
T=(u1+u2)/2
Err=abs(T-T0);
if Err > 0.0001
T0=T;
end
end
y=im2bw(x,T0);
figure(1);imshow(x);
figure(2);imshow(y)

Otsu Uneven Illumination

clc
clear all
x=imread('barcode_uneven.bmp');
x=rgb2gray(x);
T=graythresh(x);
y=im2bw(x,T);
imshow(y)
Using Image Partitioning for uneven illumination problem

Main Function

% threshold by partitioning the image


% using built in Block Processing command in
% Matlab
% This code also make use of the blkthresh
% function.
clc
clear all
x=imread('barcode_uneven.bmp');
x=rgb2gray(x);
thresh = @(a) blkthresh(a); %call another M file
y=blkproc(x,[32 32],thresh);
imshow(y)

Matlab M function in separate file

function res = blkthresh(a)


sigmaL=std2(a);
if sigmaL < 11
res=ones(size(a));
else
res=im2bw(a,graythresh(a));
end
Canny edge detector

% Canny edge detection


% studying the parameter of Canny
% especially the role of the sigma
clear all
clc
x=imread('building_gray.jpg');
y=edge(x,'canny'); % using default values
imshow(y)

Canny edge detector for different parameter values

% Canny edge detection


% studying the parameter of Canny
% especially the role of the sigma
clear all
clc
x=imread('angiogram1_gray.jpg');
y1=edge(x,'canny',[],2); % sigma = 2
figure(1);imshow(y1)
y2=edge(x,'canny',[],3); % sigma = 3
figure(2);imshow(y2)
y3=edge(x,'canny',[],4); % sigma = 4
figure(3);imshow(y3)
y4=edge(x,'canny',[],4); % sigma = 6
figure(4);imshow(y4)
y5=edge(x,'canny',[],10); % sigma = 10
figure(5);imshow(y5)
y6=edge(x,'canny',[],0.2); % sigma = 0.2
figure(6);imshow(y6)
y7=edge(x,'canny',[],0.7); % sigma = 10
figure(7);imshow(y7)
y8=edge(x,'canny',[],1); % sigma = 1 - Default values
figure(8);imshow(y8)
Log of Gaussian

clear all
x=imread('building_gray.jpg');
x=im2single(x);
[m,n]=size(x);
h=fspecial('gaussian',15,1.9);
laph=fspecial('log',17,2);

laph=laph-mean2(laph);
y1=imfilter(x,laph,'replicate');
%yy=(y1- mean2(y1));
yy=y1.^0.7;
imshow(yy+0.5);
e = false(m,n);
% finding zero crossing
% Look for the zero crossings: +-, -+ and their transposes
% We arbitrarily choose the edge to be the negative point
thresh=0.065*max(max(y1));
rr = 2:m-1; cc=2:n-1;
[rx,cx] = find( y1(rr,cc) < 0 & y1(rr,cc+1) > 0 ...
& abs( y1(rr,cc)-y1(rr,cc+1) ) > thresh ); % [- +]
e((rx+1) + cx*m) = 1;
[rx,cx] = find( y1(rr,cc-1) > 0 & y1(rr,cc) < 0 ...
& abs( y1(rr,cc-1)-y1(rr,cc) ) > thresh ); % [+ -]
e((rx+1) + cx*m) = 1;
[rx,cx] = find( y1(rr,cc) < 0 & y1(rr+1,cc) > 0 ...
& abs( y1(rr,cc)-y1(rr+1,cc) ) > thresh); % [- +]'
e((rx+1) + cx*m) = 1;
[rx,cx] = find( y1(rr-1,cc) > 0 & y1(rr,cc) < 0 ...
& abs( y1(rr-1,cc)-y1(rr,cc) ) > thresh); % [+ -]'
e((rx+1) + cx*m) = 1;

% Most likely this covers all of the cases. Just check to see if
there
% are any points where the LoG was precisely zero:
[rz,cz] = find( y1(rr,cc)==0 );
if ~isempty(rz)
% Look for the zero crossings: +0-, -0+ and their transposes
% The edge lies on the Zero point
zero = (rz+1) + cz*m; % Linear index for zero points
zz = find(y1(zero-1) < 0 & y1(zero+1) > 0 ...
& abs( y1(zero-1)-y1(zero+1) ) > 2*thresh); % [- 0
+]'
e(zero(zz)) = 1;
zz = find(y1(zero-1) > 0 & y1(zero+1) < 0 ...
& abs( y1(zero-1)-y1(zero+1) ) > 2*thresh); % [+ 0 -
]'
e(zero(zz)) = 1;
zz = find(y1(zero-m) < 0 & y1(zero+m) > 0 ...
& abs( y1(zero-m)-y1(zero+m) ) > 2*thresh); % [- 0
+]
e(zero(zz)) = 1;
zz = find(y1(zero-m) > 0 & y1(zero+m) < 0 ...
& abs( y1(zero-m)-y1(zero+m) ) > 2*thresh); % [+ 0 -
]
e(zero(zz)) = 1;
end
figure(2);
imshow(e)
Region Growing method

Main calling function


clear all
clc
x=imread('brain1 MRI.jpg');
S=zeros(size(x));
S(263:265,413:415)=ones(3,3);
T=25;
g=uint8(regiongrow(x,S,T));
y=x;
xc(:,:,1) = x;
xc(:,:,2)= x;
xc(:,:,3)= x;
y(g==1)=220;xc(:,:,1)=y; % creating red color
y(g==1)=0;xc(:,:,2)=y; % for the region growing
y(g==1)=0;xc(:,:,3)=y; % superimposing onto the original
imshow(xc)

Calling function REGIONGROW – must be in separate filename

function [g, NR, SI, TI] = regiongrow(f,S,T)


%REGIONGROW - perform segmentation by region growing.
% [G, NR, SI, TI] = REGIONGROW(F, S, T). S can be an array
(the same
% size as F) with a 1 at the coordinates of every seed point
and 0
% elsewhere. S can also be a single seed value. Similarly, T
can be an
% array (the same size as F) containing a threshold value for
each pixel
% in F. T can also be a scalar, i which case it becomes a
global
% threshold.
% On the output, G is the result of region growing, with each
region
% labeled by a different integer. NR is the number of regions.
SI is the
% final seed image consisting of the pixels in F that
satisfied the
% threshold test.

f=double(f);
%If S is a scalar, obtain the seed image.
if numel(S)==1
SI =f==S;
S1 = S;
else
% S in an array. Eliminate duplicate, connected seed
locaions
% to reduce the number of loop executions in the following
sections of
% code
SI=bwmorph(S, 'shrink',Inf);
J=find(SI);
S1=f(J); % Array of seed values.
end
TI=false(size(f));
for K = 1:length(S1)
seedvalue=S1(K);
S=abs(f - seedvalue) <= T;
TI = TI|S;
end
% Use function imreconstruct with SI as the marker image to
% obtain the regions corresponding to each seed in S. Function
bwlabel
% assigns a different integer to each connected region.
[g, NH] = bwlabel(imreconstruct(SI, TI));

Region Splitting and Merging Code

Main calling function

clear all
clc
x=imread('brain1 MRI.jpg');
g=splitmerge(x,4,@predicate);
gg=zeros(size(g));
g3=zeros(size(g));
[r c]=find(g == 0);
for i=1:length(r)
gg(r(i),c(i))= 1;
end
%y=double(x).*gg;
L=graythresh(x); % setting threshold value from the original
image
% using Otsu method
vy=uint8(gg).*x; % AND-ing the result with the original image
v=im2bw(vy,L); % converting to binary number by thresholding
the
% result using the computed threshold value.
y=x;
xc(:,:,1) = x;
xc(:,:,2)= x;
xc(:,:,3)= x;
y(v>0)=220;xc(:,:,1)=y; % creating red color
y(v>0)=0;xc(:,:,2)=y; % for the region growing
y(v>0)=0;xc(:,:,3)=y; % superimposing onto the original
imshow(xc)
Calling function

function g = splitmerge(f, mindim, fun)


% SPLITMERGE Segment an image using a split and merge algorithm
% G = SPLITMERGE(F, MINDIM, @PREDICATE) segment image F by using a
% split-and-merge apporach based on quadtree decomposition. MINDIM (a
% positive integer power of 2) specifies the minimum dimension of the
% quadtree regions (subimages) allowed. If necessary, the program pads
% the input image with zeros to the nearest square size that is an
% integer power of 2. This guarantees that the algorithm used in the
% quadtree decomposition will be able to split the image down to blocks
% of size 1-by-1. The result is cropped back to the original size of the
% input image. In the output, G, each connected region is labeled with a
% different integer.
%
% Note that in the fnction call we use @PREDICATE for the value of fun.
% PREDICATE is a function in the MATLAB path, provided by the user. Its
% syntax is
% FLAG = PREDICATE(REGION) which must return TRUE if the pixels in
% REGION satisfy the predicate defined by the code in the function;
% otherwise, the value of FLAG must be FALSE.
%
% The following simple example of function PREDICATE sets FLAG to TRUE if
% the intensities of the pixels in REGION have a standard deviation that
% exceeds 10, and their mean intensity is between 0 and 125. Otherwise
% FLAG is set to false.
% function flag = predicate(region)
% sd=std2(region);
% m=mean2(region);
% flag=(sd > 10)& (m > 0) & (m < 125);
%
%pad image with zeros to guarantee that function qtdecomp will split
%regions down to size 1-by-1.
Q=2^nextpow2(max(size(f)));
[M N]=size(f);
f=padarray(f,[Q - M, Q - N], 'post');

% Perform splitting first.


S=qtdecomp(f, @split_test, mindim, fun);

% Now merge by looking at each quadregion and setting all its elements to 1
% if the block satisfies the predicate.

% Get the size of the largest block. Use full because S is sparse.
Lmax = full(max(S(:)));
% Set the output image initially to all zeros. The MARKER array is used
% later to establish connectivity.
g=zeros(size(f));
MARKER = zeros(size(f));
% Begin the merging stage
for K = 1:Lmax
[vals, r, c]=qtgetblk(f,S,K);
if ~isempty(vals)
% check the predicate for each of the regions of the size K-by-K
% with coodinates given by vectors r and c
for I = 1:length(r)
xlow=r(I);ylow=c(I);
xhigh=xlow+K-1;yhigh=ylow+K-1;
region=f(xlow:xhigh,ylow:yhigh);
flag=feval(fun,region);
if flag
g(xlow:xhigh,ylow:yhigh)=1;
MARKER(xlow,ylow)=1;
end
end
end
end
Calling function

g(xlow:xhigh,ylow:yhigh)=1;
MARKER(xlow,ylow)=1;
end
end
end
end
% Finally, obtain each connected region and label it with a different
% integer value using function bwlabel.
g=bwlabel(imreconstruct(MARKER,g));
% crop and exit
g=g(1:M,1:N);

%---------------------------------------------------------------------%
function v = split_test(B, mindim, fun)
% THIS FUNCTION IS A PART OF FUNCTION SPLIT-MERGE. IT DEERMINES WHETHER
% QUADREGIONS ARE SPLIT. The function returns in v logical 1s (TRUE) for
% the blocks that should be split and logical 0s (FALSE) for those that
% should not.

% Quadregion B, passed by dtdecomp, is the current decomposition of the


% image into k blocks of size m-by-m.

%k is the number of regions in B at this point in the procedure.


k=size(B,3);

% Perform the split test onn each block. If the predicate funcion (fun)
% returns TRUE, the region is split, so we set the appropriate element of v
% to TRUE. Else, the appropriate elements of v is set to FALSE.
v(1:k)=false;
for I=1:k
quadregion=B(:,:,I);
if size(quadregion,1) <= mindim
v(I)=false;
continue
end
flag=feval(fun, quadregion);
if flag
v(I)=true;
end
end

Predicate function – separate file


function flag = predicate(region)
sd=std2(region);
m=mean2(region);
flag=(sd > 1) & ((m>0) & (m < 205));
end
Watershed transform

Main calling function

clear all
clc
x=imread('f15_gray.jpg');
option=1;
g=watshed(x,option);
imshow(g)

Calling function

function g = watshed(f,option)
% WATSHED Compute and Display the watershed line for the given Image F
% based on OPTION.
% G = WATSHED (F) takes an image f, compute its gradient or external
% marking, perform the Matlab watershed function and display the
% watershed line superimposing on the original image F.
% if OPTION = 1 then gradient method will be used
% if OPTION = 2 then external marking will be used
% if ~isempty(option)
% option = 2; %default
% end
h=fspecial('sobel');
fd=double(f);
wf=sqrt(imfilter(fd,h,'replicate').^2 + imfilter(fd,h','replicate').^2);
if option == 1
wfsmooth=imclose(imopen(wf,ones(3,3)),ones(3,3));
wshed=watershed(wfsmooth);
wr2 = wshed == 0;
g = f;
g(wr2) = 255;
else
wfsh = watershed(wf);
wr = wfsh == 0;
im = imextendedmin(f,140); % establsihing internal markers
fim = f;
fim(im) = 200; % solely dor displaying
tem = imextendedmax(f,18); % computing the
% external markers
g2 = imimposemin(wf, im|tem);
L2 = watershed(g2);
g = f;
g(L2 == 0) = 255;
end
end

You might also like