clear all
%一、图像的预处理,读入彩色图像并将其灰度化
img=imread('预处理道路.jpg');%读入JPG彩色图像文件
%img=imnoise(img,'salt & pepper',0.02); %加噪声 0.02为参数表示噪声的大小
slope = zeros(341, 341);
sub_img = zeros(5, 5);
for m=2: 340
for n=2: 340
sub_img = img(m-1: m+1, n-1: n+1);
imgsize = size(sub_img);
N = imgsize(1);
distance = zeros(1, N^4);
difference = zeros(1, N^4);
i = 1;
for x1 = 1 : N
for y1 = 1 : N
for x2 = 1 : N
for y2 = 1 : N
dis = sqrt((x2-x1)^2 + (y2-y1)^2);
if dis ~= 0
distance(i) = dis;
difference(i) = abs(sub_img(x2,y2)-sub_img(x1,y1));
i = i + 1;
end
end
end
end
end
distance = distance(1:i-1);
difference = difference(1:i-1);
arr_size = size(distance);
arr_size = arr_size(2);
sr = zeros(1, arr_size);
pn = zeros(1, arr_size);
di_sum = zeros(1, arr_size);
di = zeros(1, arr_size);
[distance, pos]=sort(distance);
difference = difference(pos);
count = 1;
for i = 1 : arr_size
sr(count) = distance(i);
pn(count) = pn(count) + 1;
di_sum(count) = di_sum(count) + difference(i);
if i ~= arr_size
if sr(count) ~= distance(i+1)
count = count + 1;
end
end
end
sr1 = sr(1:count);
pn1 = pn(1:count);
di_sum1 = di_sum(1:count);
di1 = di(1:count);
pn1 = pn1 / 2;
di1 = di_sum1 ./ pn1;
% log(sr) ~ log(di)
log_sr = log2(sr1);
log_di = log2(di1);
p = polyfit(log_sr, log_di, 1);
slope(m, n) = p(2);
end
end
disp(slope);
results = zeros(341, 341);
for m=2: 340
for n=2: 340
%if slope(m, n) > 2.9 && slope(m, n) < 5.9
if slope(m, n) < 4.9
results(m, n) = 0;
else
results(m, n) = 255;
end
end
end
results = uint8(results);
figure,imshow(results);
% for m=2: 340
% for n=2: 340
% if img(m, n) < 110
% results(m, n) = 0;
% else
% results(m, n) = 255;
% end
% end
% end
% results = uint8(results);
% figure,imshow(results);
% img_2=medfilt2(results);
% figure,imshow(img_2);
% title('中值滤波后图像');