Plot the s_b_squared with the imahe histogram
조회 수: 2(최근 30일)
표시 이전 댓글
Hello!
So I have an image,and I would like to implement image segmentation on it. To be more specific, I'm performing Otsu's thresholding in order to create a binary image. In the process, I figured a way to show where the Otsu's threshold is present in the histogram of the image and have done it with success. Yet when I wanted to mathematically prove,but always based on MATLAB implementation, that the point that I've found is the right one using the formula of s_b_squared, I got a very different implementation of the threshold point.
Below I'll leave to you, both the code that I've written so far, the image that I want to process and the produced outcomes:
clear
clc
%Ερώτημα Α)
%Αποτύπωση των εικόνων και των παραγόμενων, από την κατωφλιοποίηση εικόνων
figure(1)
img_1=imread('BP1.jpg');
img_2=imread('BP2.jpg');
level1=graythresh(img_1);
level2=graythresh(img_2);
BW1=imbinarize(img_1,level1);
BW2=imbinarize(img_2,level2);
subplot(1,2,1)
imshowpair(img_1,BW1,'montage')
title("Αποτέλεσμα της κατωφλιοποίησης για την εικόνα 1")
subplot(1,2,2)
imshowpair(img_2,BW2,'montage');
title("Αποτέλεσμα της κατωφλιοποίησης για την εικόνα 2")
figure(2) %Ιστόγραμμα με υπόδειξη του σημείου κατωφλίου
subplot(1,2,1)
imhist(img_1)
hold on
plot(256*[level1 level1],ylim,'r','LineWidth',2);
legend({'Ιστόγραμμα','Κατώγλι κατά Otsu'},'Location','northwest')
title("Εμφάνιση του σημείου κατωφλιοποίησης,κατά Otsu,στην εικόνα 1")
hold off
subplot(1,2,2)
imhist(img_2)
hold on
plot(256*[level2 level2],ylim,'r','LineWidth',2); %Κανονικοποίηση του level1 ως προς το ιστόγραμμα
legend({'Ιστόγραμμα','Κατώγλι κατά Otsu'},'Location','northwest')
title("Εμφάνιση του σημείου κατωφλιοποίησης κατά,Otsu,στην εικόνα 2")
hold off
figure(3)
counts=imhist(img_1); %Ανάθεση του ιστογράμματος σε μια μεταβλητή
L=length(counts); %Εναπόθεση της έκτασης της μεταβλητής counts στην μεταβλητή L
p=counts/sum(counts); %Υπολογισμός της πιθανότητας
Propability_1=cumsum(p); %Υπολογισμός της πιθανότητας της κλάσης 1
%Για τον υπολογισμό της μέσης έντασης μέχρι το επίπεδο του κατωφλίου
%(average intensity)
m_k=cumsum(p.*(1:L));
%Για τον υπολογισμό της μέσης έντασης ολόκληρης της εικόνας (global mean)
m_g=m_k(end);
sigma_b_squared=(((m_g * Propability_1) - m_k).^2) ./ (Propability_1 .* (1-Propability_1));
yyaxis left
plot(counts);
ylabel('Ιστόγραμμα')
yyaxis right
plot(sigma_b_squared);
ylabel('\sigma_B^2');
xlim([1 256])
[~,k] = max(sigma_b_squared);
hold on
plot([k,k],'LineWidth',5)
hold off
Alsoo this is a page that I've found to help me implement the final part:https://blogs.mathworks.com/steve/2016/06/14/image-binarization-otsus-method/
Can you please help me?
Thank you in advance
P.S. There's another image that I want to process, so don't mind the second one
댓글 수: 0
답변(0개)
참고 항목
범주
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!