MATLAB Source Codes Otsu Thresholding Method: All 'Angiogram1 - Gray - JPG'
MATLAB Source Codes Otsu Thresholding Method: All 'Angiogram1 - Gray - JPG'
clc
clear all
x=imread('angiogram1_gray.jpg');
T=graythresh(x);
y=im2bw(x,T);
imshow(y)
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)
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
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
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));
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
% 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.
% 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
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