function fenceng;
img = imread('C:\Users\anyka001\Desktop\IMG_2696.JPG');
[rows cols colors] = size(img);
img_gray = zeros(rows,cols);
img_gray = uint8(img_gray); %把申请的空数组的double转换成uint8
for x=1:rows
for y=1:cols
sum = 0;
for k=1:3
sum=sum+img(x,y,k)/3;
end
img_gray(x,y)=sum;
end
end
imshow(img_gray);
[row col] = size(img_gray);
img_bit =zeros(row,col);
img_bit = uint8(img_bit);
for bit=1:8
threshold = 2^bit;
for x=1:row
for y=1:col
if img_gray(x,y)< threshold && img_gray(x,y)>=(threshold/2) %采用阈值判定的办法 卓个PIxel进行判断
img_bit(x,y) = img_gray(x,y);
else
img_bit(x,y) = 0;
end
end
end
figure,imshow(img_bit);
end