디지털 이미지 처리 - LOG 연산 자 와 CANNY 연산 자 가장자리 추출 (matlab)
9475 단어 디지털 이미지 처리
code: 독립 가성 고 스 백 소음
function [distImg,endtImg] = add_noise(origImg,u)
origImg = im2double(origImg);
distImg = imnoise(origImg, 'gaussian', 0, u);
endtImg = distImg.*255;
end
code: 그림 에 고 스 필터 링
function g=my_gaussian(src,n,k)
n1=(n+1)/2;
[m,l]=size(src);
b=zeros(n,n);
I=zeros(m,l);
g=zeros(m,l);
img=zeros(m,l);
[Ay, Ax ,dim ] = size(src);
if dim>1
src = rgb2gray(src);
end
for i=1:n
for j=1:n
b(i,j)=(exp(-((i-n1)^2 +(j-n1)^2)/(2*k)))/(2*pi*k);
end
end
b = b/sum(b(:));
I=double(src);
img=conv2(I,b,'same');
g=uint8(img);
end
code: LOG 연산 자 이미지 처리 및 결과 표시
function [thrr] =log_img(noise,n,k,thr)
[filename, pathname] = uigetfile({'*.jpg'; '*.bmp'; '*.gif'}, ' ');
if filename == 0
return;
end
imgsrc = imread([pathname, filename]);
[y, x, dim] = size(imgsrc); %
if dim>1
imgsrc = rgb2gray(imgsrc);
end
if (x~=256 && y~=256) % 8bit 256*256
imgsrc = im2uint8(imgsrc);
imgsrc=imresize(imgsrc,[256,256]);
end
[showing_noise, img_noise]= add_noise(imgsrc,noise);
img= my_gaussian(img_noise,n,k);
[img_log,thrr]=edge(img,'log',thr);
figure(1)
imshow(imgsrc);%
figure(2)
imshow(showing_noise);
figure(3)
imshow(img);%
figure(4)
imshow(img_log);
end
code: CANNY 연산 자 실현
function [ m, theta, sector, canny1, canny2, bin] = canny_step( src, lowTh)
[Ay, Ax, dim ] = size(src);
if dim>1
src = rgb2gray(src);
end
src = double(src);
m = zeros(Ay, Ax);
theta = zeros(Ay, Ax);
sector = zeros(Ay, Ax);
canny1 = zeros(Ay, Ax);
canny2 = zeros(Ay, Ax);
bin = zeros(Ay, Ax);
%
for y = 1:(Ay-1)
for x = 1:(Ax-1)
gx = src(y, x) + src(y+1, x) - src(y, x+1) - src(y+1, x+1);
gy = -src(y, x) + src(y+1, x) - src(y, x+1) + src(y+1, x+1);
m(y,x) = (gx^2+gy^2)^0.5 ;
theta(y,x) = atand(gx/gy) ;
tem = theta(y,x);
if (tem<67.5)&&(tem>22.5)
sector(y,x) = 0;
elseif (tem<22.5)&&(tem>-22.5)
sector(y,x) = 3;
elseif (tem-67.5)
sector(y,x) = 2;
else
sector(y,x) = 1;
end
end
end
%
for y = 2:(Ay-1)
for x = 2:(Ax-1)
if 0 == sector(y,x) % -
if ( m(y,x)>m(y-1,x+1) )&&( m(y,x)>m(y+1,x-1) )
canny1(y,x) = m(y,x);
else
canny1(y,x) = 0;
end
elseif 1 == sector(y,x) %
if ( m(y,x)>m(y-1,x) )&&( m(y,x)>m(y+1,x) )
canny1(y,x) = m(y,x);
else
canny1(y,x) = 0;
end
elseif 2 == sector(y,x) % -
if ( m(y,x)>m(y-1,x-1) )&&( m(y,x)>m(y+1,x+1) )
canny1(y,x) = m(y,x);
else
canny1(y,x) = 0;
end
elseif 3 == sector(y,x) %
if ( m(y,x)>m(y,x+1) )&&( m(y,x)>m(y,x-1) )
canny1(y,x) = m(y,x);
else
canny1(y,x) = 0;
end
end
end
end
%
ratio = 2;
for y = 2:(Ay-1)
for x = 2:(Ax-1)
if canny1(y,x)ratio*lowTh
canny2(y,x) = canny1(y,x);
bin(y,x) = 1;
continue;
else
tem =[canny1(y-1,x-1), canny1(y-1,x), canny1(y-1,x+1);
canny1(y,x-1), canny1(y,x), canny1(y,x+1);
canny1(y+1,x-1), canny1(y+1,x), canny1(y+1,x+1)];
temMax = max(tem);
if temMax(1) > ratio*lowTh
canny2(y,x) = temMax(1);
bin(y,x) = 1;
continue;
else
canny2(y,x) = 0;
bin(y,x) = 0;
continue;
end
end
end
end
end
code: CANNY 이미지 처리 및 결과 전시
function canny_plotimg(noise,n,thr,k)
[filename, pathname] = uigetfile({'*.jpg'; '*.bmp'; '*.gif'}, ' ');
if filename == 0
return;
end
imgsrc = imread([pathname, filename]);
[y, x, dim] = size(imgsrc);
if dim>1
imgsrc = rgb2gray(imgsrc);
end
if (x~=256 && y~=256) % 8bit 256*256
imgsrc = im2uint8(imgsrc);
imgsrc=imresize(imgsrc,[256,256]);
end
[showing_noise, img_noise]= add_noise(imgsrc,noise);
img= my_gaussian(img_noise,n,k);
[m theta sector canny1 canny2 bin] = canny1step(img, thr);
figure(1)
imshow(imgsrc);%
figure(2)
imshow(showing_noise);
figure(3)
imshow(img);%
figure(4)
imshow(uint8(m));%
figure(5)
imshow(uint8(canny1));%
figure(6)
imshow(uint8(canny2));%