can some help me please with Otsu thresholding matlab code in which the dimensions, width, height, of the output image is equal to the orignal image (input image). Thanks in advance.

15 commentaires

I=imread('1 (4).png');
figure(1),imshow(I);
figure(2),imhist(I);
n=imhist(I);
N=sum(n);
max=0;
for i=1:256
P(i)=n(i)/N;
end
for T=2:255
w0=sum(P(1:T));
w1=sum(P(T+1:256));
u0=dot([0:T-1],P(1:T))/w0;
u1=dot([T:255],P(T+1:256))/w1;
sigma=w0*w1*((u1-u0)^2);
if sigma>max
max=sigma;
threshold=T-1;
end
end
bw=im2bw(I,threshold/255);
figure(3),imshow(bw);
i applied this code it gives the accurate segmentation but the problem is it can change the width, height and dimension of the output image. i want Otsu threshold code in which the width, height and dimension remain same with the input image. Thanks in advance.
Hope you will help me with that sir.
No, in that code, bw is certain to be exactly the same height and width as the original image, I. However, bw will be black and white (2D) instead of color (3D). If you need the exact same output dimensions then
I_out = repmat( im2uint8(bw), [1 1 3]);
Hello Sir. whether i can use this command i try it but did not work same problem.
1(1).pngThis is the output image which i get but if you check the both image properties its dimension, width and height is different i want it same for furthur investigation.
The deprecated im2bw() does not change the number of rows and columns in the image. What is the size you say it's giving and what size do you think it should be?
but i am working on otsu algorithm only
it looks to me as if you are using saveas or print or getframe on the displayed image and then complaining that it is not the same size as the original .
Good guess Walter - that's probably it. When I use "whos" to look at the I and bw variable:
Name Size Bytes Class Attributes
bw 256x320 81920 logical
Name Size Bytes Class Attributes
I 256x320x3 245760 uint8
You can see that they're both 256 by 320 - the same lateral size (rows and columns), NOT different.
Arshad, WHY do you say they're of a different size? How did you check that? Please supply proof.
its look perfect in matlab but when i save the output image from matlab and then check both images properties its different even i am not using save as.
when i save the output image from matlab. There is a white margin around the image.
how are you saving the output image ?
i found my mistake instead of using imwrite() command i save images directly . when i used imwrite() command it save image with original dimension .. Thank you Walter Roberson. Thank you so much..

Connectez-vous pour commenter.

Réponses (1)

Hello Arshad, otsu thresolding techniqie is very popular thresholding approach in image segmentation domain. I have posted the following code from Gonzalez book (Digital Image Processing using Matlab), requested you to refer the book for detail explanation.
function [T,SM]=otsuthresh(h);
h=h/sum(h);
h=h(:);
i=(1:numel(h))';
P1=cumsum(h);
m=cumsum(i.*h);
mG=m(end);
sigSquared=((mG*P1-m).^2)./(P1.*(1-P1)+eps);
maxSigsq=max(sigSquared);
T=mean(find(sigSquared==maxSigsq));
T=(T-1)/(numel(h)-1);
SM=maxSigsq/(sum(((i-mG).^2).*h)+eps);
end

Commenté :

le 24 Nov 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by