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.
1 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/196086/image.png)
15 个评论
Arshad Ali
2018-11-22
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
2018-11-22
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
2018-11-22
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
2018-11-23
Hello Sir. whether i can use this command i try it but did not work same problem.
Arshad Ali
2018-11-23
![1(1).png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/196326/1(1).png)
Image Analyst
2018-11-23
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
2018-11-23
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
2018-11-23
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
2018-11-24
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
2018-11-24
when i save the output image from matlab. There is a white margin around the image.
Arshad Ali
2018-11-24
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..
回答(1 个)
KALYAN ACHARJYA
2018-11-22
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)