图像二值化 php im2bw,图像二值化-MATLAB实现
1.圖像二值化代碼:
A=imread('hw1.chips1.GIF'); %讀取到一張圖片
%thresh = graythresh(A); %自動確定二值化閾值
I2 = im2bw(A,120/255); %對圖像二值化
figure();
subplot(1,2,1);
imshow(A); %顯示二值化之前的圖片
title('原圖');
subplot(1,2,2);
imshow(I2); %顯示二值化之后的圖片
title('二值化');
2.求目標和背景均值和方差
oriImg = imread('hw1.chips1.GIF');
grayImg = oriImg;
%grayImg = rgb2gray(oriImg); %轉(zhuǎn)換為灰度圖
grayImg = double(grayImg);
[m, n] = size(oriImg);
foreCount = 0;
foreTotal = 0;
backCount = 0;
backTotal = 0;
for x = 1:m
for y = 1:n
if(grayImg(x,y) >= 128)
foreCount = foreCount + 1;
foreTotal = foreTotal + grayImg(x,y);
else
backCount = backCount + 1;
backTotal = backTotal + grayImg(x,y);
end
end
end
backMean = backTotal / backCount;
foreMean = foreTotal / foreCount;
backSqu = 0;
foreSqu = 0;
for x =1:m
for y = 1:n
if(grayImg(x,y) >= 128)
foreSqu = foreSqu + (grayImg(x,y) - foreMean)^2;
else
backSqu = backSqu + (grayImg(x,y) - backMean)^2;
end
end
end
backSqu = sqrt(backSqu / backTotal);
foreSqu = sqrt(foreSqu / foreTotal);
disp(strcat('背景均值', num2str(backMean)));
disp(strcat('背景標準差', num2str(backSqu)));
disp(strcat('前景均值', num2str(foreMean)));
disp(strcat('前景標準差', num2str(foreSqu)));
3.求目標和背景高斯分布(雙峰直方圖)
x = 0:0.1:256;
y1 = gaussmf(x,[backSqu^2 backMean]);
plot(x, y1);
hold on;
y2 = gaussmf(x,[foreSqu^2 foreMean]);
plot(x, y2);
title('高斯分布');
總結
以上是生活随笔為你收集整理的图像二值化 php im2bw,图像二值化-MATLAB实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (tac linux)
- 下一篇: 安卓图标修改器(安卓图标修改)