Matlab影像處理(五)邊緣偵測Sobel濾波
%% sobel邊緣濾波
close all; clear; clc;
img = double(rgb2gray(imread('Color_lena512.bmp')));
[row, col] = size(img);
Hck = [-1 -2 -1; 0 0 0; 1 2 1;];
Vck = [-1 0 1; -2 0 2; -1 0 1;];
Hfeature = zeros(row, col); Vfeature = zeros(row, col); addfeature = zeros(row, col);
H=0; V=0; s=zeros(1, row.*col);
for i=1:row-2
for j=1:col-2
for ii=1:3
for jj=1:3
H = H + img(i+ii-1, j+jj-1).*Hck(ii, jj);
V = V + img(i+ii-1, j+jj-1).*Vck(ii, jj);
end
end
Hfeature(i+1, j+1) = H;
Vfeature(i+1, j+1) = V;
H=0; V=0;
end
end
count = 0;
for i=1:row
for j=1:col
count = count + 1;
addfeature(i, j) = abs(Hfeature(i, j)) + abs(Vfeature(i, j));
s(1, count) = addfeature(i, j);
end
end
s=sort(s); min=s(1, 1); max=s(1, row.*col); outputimg = zeros(row, col);
n =@(x) ( (x-min)./(max-min) ).*255;
for i=1:row
for j=1:col
outputimg(i, j) = n(addfeature(i, j));
end
end
subplot(1, 2, 1), imshow(uint8(img)), title('原圖')
subplot(1, 2, 2), imshow(uint8(outputimg)), title('sobel濾波')
留言
張貼留言