|
主函数:
- global ImageGray
- hist = imhist(ImageGray)./(size(ImageGray,1)*size(ImageGray,2)); % 直方图
- for i=1:256
- fit(i) = image_segmentation_ostu(hist,i); % Ostu
- end
- [maxfit,thre] = max(fit);
- bw = ImageGray > thre; % 二值化操作
复制代码 子函数:
- function y = image_segmentation_ostu( hist, thre )
- % 最大类间方差法
- Lmax = 256;
- y = 0;
- p1 = 0; p2=0; p3 =0; p4 =0; p5=0;
- for i=1:Lmax
- p1 = p1+i*hist(i);
- if i<thre
- p2 = p2 + hist(i);
- else
- p3 = p3 + hist(i);
- end
- end
- for i=1:Lmax
- if i<thre
- p4 = p4 + i*hist(i)/p2;
- else
- p5 = p5 + i*hist(i)/p3;
- end
- end
- y = p2*(p4-p1)*(p4-p1);
- y = y+p3*(p5-p1)*(p5-p1);
复制代码
|
|