Question

write a matlab code to apply Otsu's segmentation after getting infromtion from function imread

write a matlab code to apply Otsu's segmentation after getting infromtion from function imread
0 0
Add a comment Improve this question Transcribed image text
Answer #1

close all;clear all;clc
%%================================================================================================
I=imread('img.jpg'); % Read the Image
figure(1),imshow(I); % display the Original Image
figure(2),imhist(I); % display the Histogram
%%=================================================================================================
n=imhist(I); % Compute the histogram
N=sum(n); % sum the values of all the histogram values
max=0; %initialize maximum to zero
%%================================================================================================
for i=1:256
P(i)=n(i)/N; %Computing the probability of each intensity level
end
%%================================================================================================
for T=2:255 % step through all thresholds from 2 to 255
w0=sum(P(1:T)); % Probability of class 1 (separated by threshold)
w1=sum(P(T+1:256)); %probability of class2 (separated by threshold)
u0=dot([0:T-1],P(1:T))/w0; % class mean u0
u1=dot([T:255],P(T+1:256))/w1; % class mean u1
sigma=w0*w1*((u1-u0)^2); % compute sigma i.e variance(between class)
if sigma>max % compare sigma with maximum
max=sigma; % update the value of max i.e max=sigma
threshold=T-1; % desired threshold corresponds to maximum variance of between class
end
end
%%====================================================================================================
bw=im2bw(I,threshold/255); % Convert to Binary Image
figure(3),imshow(bw); % Display the Binary Image
%%====================================================================================================

Add a comment
Know the answer?
Add Answer to:
write a matlab code to apply Otsu's segmentation after getting infromtion from function imread
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT