You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
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.
2 views (last 30 days)
Show older comments
15 Comments
Arshad Ali
on 22 Nov 2018
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);
Arshad Ali
on 22 Nov 2018
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.
Walter Roberson
on 22 Nov 2018
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]);
Arshad Ali
on 23 Nov 2018
Hello Sir. whether i can use this command i try it but did not work same problem.
Arshad Ali
on 23 Nov 2018
This 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.
Image Analyst
on 23 Nov 2018
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?
Walter Roberson
on 23 Nov 2018
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 .
Image Analyst
on 23 Nov 2018
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.
Arshad Ali
on 24 Nov 2018
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.
Arshad Ali
on 24 Nov 2018
when i save the output image from matlab. There is a white margin around the image.
Arshad Ali
on 24 Nov 2018
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..
Answers (1)
KALYAN ACHARJYA
on 22 Nov 2018
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
See Also
Tags
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)