1. 程式人生 > >matlab 實現雙峰法全域性閾值處理

matlab 實現雙峰法全域性閾值處理

img=imread('C:\Users\Administrator\Desktop\lwx.JPG');
im=rgb2gray(img);
im_median=medfilt2(im);

% 直方圖 0-255
h = imhist(im_median);
% 求極大值 粗略的算了一下 
%IndMax=find(diff(sign(diff(count)))<0)+1;
[cnt,x] = findpeaks(h,'minpeakdistance',30,'minpeakheight',30);
%stem(x,cnt)

% 估算
n = length(x);
yu = x(1)+x(n)-2;
im_2bw = im2bw(im_median,yu/510);
subplot(131);imshow(im_2bw);
subplot(132);stem(x,cnt);
subplot(133);imshow(im_2bw);