Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 19

TI : NGHIN CU THUT TON PHT HIN BIN CANNY

NHM 10 D09CNTT4 V Bnh Long Nguyn Thnh Cng Nguyn Thnh Trung L Tun Thnh Phm Vit Ly

Ni dung 1. Gii thiu thut ton Canny 2. Thay i cc tham s 3. Cc ng dng hng nghin cu tng lai

1. Gii thiu thut ton Canny Phng php ny do John Canny phng th nghim MIT khi xng vo nm 1986. Canny a mt tp hp cc rng buc m mt phng php pht hin bin phi t c. ng trnh by mt phng php ti u nht thc hin c cc rng buc . V phng php ny c gi l phng php Canny. tng thut ton: tng ca phng php ny l nh v ng v tr bng cch cc tiu ho phng sai 2 ca v tr cc im ct "Zero" hoc hn ch s im cc tr cc b ch to ra mt ng bao. Cc rng buc m phng php pht hin bin Canny thc hin c l: mc li, nh v v hiu sut. Trong : - Mc li: c ngha l mt phng php pht hin bin ch v phi tm tt c cc bin, khng bin no c tm b li. - nh v: iu ny ni n chnh lch cp xm gia cc im trn cng mt bin phi cng nh cng tt. - Hiu sut: l lm sao cho khi tch bin khng c nhn ra nhiu bin trong khi ch c mt bin tn ti. Rng buc mc li v nh v c dng nh gi cc phng php pht hin bin. Cn rng buc v hiu sut th tng ng vi mc li dng. Canny gi thit rng nhiu trong nh tun theo phn b Gauss v ng thi ng cng cho rng mt phng php pht hin bin thc cht l mt b lc nhn xon c kh nng lm mn nhiu v nh v c cnh. Vn l tm mt b lc sao cho tho mn ti u nht cc rng buc trn. Hot ng ca thut ton Thut ton c tin hnh qua bn bc c bn sau: - c nh I cn x l: tin hnh lm trn nh bng cch nhn xon nh vi b lc Gauss. - o hm bc nht kt qu trn theo hai hng x v y: iu ny tng ng vi vic nhn xon nh kt qu bc 1 theo hai hng

(x v y) vi mt b lc da trn o hm bc nht (cc b lc ca k thut Gradient). T a ra kt qu o hm nh sau khi tin hnh nhn xon vi Gauss. - Cho nh kt qu bc trn tin hnh Non-maximum Suppression Ngha l loi b bt cc im cnh (loi b bt nhiu), ch gi li im c mc xm cao. - Tin hnh thc hin p dng ngng (ngng cao v ngng thp) loi b mt s cnh xu. 2.Thay i cc tham s nh ban u

S dng lch chun l 1, ngng trn m di l 255 v 1, ta c

Gim ngng v 150, ta c nh

lch chun 2 v ngng 100

4. Cc ng dng v hng nghin cu tng lai Thut ton canny ng dng rt rng, c th dng C, C++, Java.. pht trin cc ng dng pht hin bin ca nh. Dng java, c th pht trin mt ng dng nh, bng flash, tch hp trn trnh duyt

Ta c th pht trin mt ng dng , nhn dng bin ca nh trong video vi java.

PH LC
CODE : split.m
function [ b ] = split( a,f) %SPLIT This function split at a threshold f %add your method here n=input('Nhap vao n:'); m=input('Nhap vao m:'); f=input('Nhap vao f:'); for i=1:n for j=1:m a(i,j)=input(':'); if a(i,j)>=f b(i,j)=255; else b(i,j)=0; end end end

errdiffu.m
function [ b ] = errdiffu( a ,f) %ERRDIFU % add your method n=input('Nhap vao m=input('Nhap vao f=input('Nhap vao here n:'); m:'); f:');

for i=1:n for j=1:m a(i,j)=input(':'); end end for i=1:n for j=1:m if a(i,j)<f && j<m b(i,j)=0; a(i,j+1)=a(i,j+1)+a(i,j); else if a(i,j)<f && j==m b(i,j)=0; a(i,j)=a(i,j); end

end if a(i,j)>=f && j<m b(i,j)=255; a(i,j+1)=a(i,j+1)- 255 + a(i,j); else if a(i,j)>=f && j==m b(i,j)=255; a(i,j)=a(i,j); end end end

end end

errdiffu2d.m
function [ b ] = errdiffu2d( a,f) %ERRDIFU2D % add your method here b = a; [h w d] = size(b); for i = 1 : h ; for j = 1 : w; if b(i, j) < f; if (j ~= w) && (i ~= h) && (i ~= 1) && (j ~= 1); b(i, j + 1) = b(i, j + 1) + round (7 / 16 * b(i, j)); b(i + 1, j - 1) = b(i + 1, j - 1) + round (3 / 16 * b(i, j)); b(i + 1, j) = b(i + 1, j) + round (5 / 16 * b(i, j)); b(i + 1, j + 1) = b(i + 1, j + 1) + round (1 / 16 * b(i, j)); end b(i, j) = 0; else if (j ~= w) && (i ~= h) && (i ~= 1) && (j ~= 1); b(i, j + 1) = b(i, j + 1) - round (7 / 16 * (255 - b(i, j))); b(i + 1, j - 1) = b(i + 1, j - 1) - round (3 / 16 * (255 b(i, j))); b(i + 1, j) = b(i + 1, j) - round (5 / 16 * (255 - b(i, j))); b(i + 1, j + 1) = b(i + 1, j + 1) - round (1 / 16 * (255 b(i, j))); end b(i, j) = 255; end end end end

incrbrightness.m
function [ b ] = incrbrightness( a,c )

%INCRBRIGHTNESS this function increase brightness of the whole picture a % by c. %add your method here n=input('Nhap vao n:'); m=input('Nhap vao m:'); c=input('Nhap vao c:'); for i=1:n for j=1:m a(i,j)=input(':'); if a(i,j)+c > 255 b(i,j)=255; else b(i,j)=a(i,j)+c; end end end

hist.m
function [ h ] = hist( a ) %HIST calculate histogram of a n=input('Nhap vao n:'); m=input('Nhap vao m:'); for i=1:n for j=1:m a(i,j)=input(':'); end end for t=1:256 h(t)=0; end for i=1:n for j=1:m h(a(i,j)+1)=h(a(i,j)+1)+1; end end for k=1:256 fprintf('%d ', h(k)); end end

histbal.m
function [ b ] = histbal( a ) %MYHISTBAL balance the histogram of a

% Process your image here [h w d] = size(b); his = hist(b); no_level = 256; tb = (h * w) / no_level; tg = 0; for i = 1 : 256 tg = tg + his(i); his(i) = max(0, round(tg/tb) - 1); end for i = 1 : h for j = 1 : w b(i, j) = his(b(i, j) + 1); end end end

autosplit.m
function [ b ] = autosplit( a ,func) %AUTOSPLIT select the threadhold f and process it accordingly % Calculate f here [he wi d] = size(a); h = hist(a); t = zeros(1,256); m = zeros(1,256); fg = zeros(1,256); t(1) = h(1) ; e = 0; for i = 2 : 256 t(i) = t(i-1) + h(i); e = e + (i - 1) * h(i); m(i) = 1 / t(i) * e; end for i = 1 : 256 fg(i) = t(i) / (wi * he - t(i)) * ( m(i) - m(256) )^2; end f = round(max(fg)); switch(func) case 1 b = split(a,f); case 2 b = errdiffu(a,f); case 3 b = errdiffu2d(a,f); end

meanfilter.m
function [ b ] = meanfilter( a, he , wi, f ) %MEANFILTER this function calculate the image after a mean filter at %threadhold f with window size hxw %add your code here %init b = a; [h w d] = size(b); med = zeros(1 , wi * he); % Xac dinh tam va pham vi if rem(he , 2) tx = (he + 1) / 2; up = he - tx; down = up; else tx = he / 2; up = he - tx - 1; down = he - up - 1; end if rem(wi , 2) ty = (wi + 1) / 2; left = wi - ty; right = left; else ty = wi / 2; left = wi - ty - 1; right = wi - left - 1; end %nhan chap for i = 1 : h; for j = 1 : w; if (i <= up) || (j <= left) || (i > h - down) || (j > w - right); b(i, j) = 0; else % med count = 0; for m = -up : down; for n = - left : right; count = count + 1; med(count) = a(i + m , j + n); end end %tinh trung binh avg = round( mean(med) ); %check if abs(a(i, j) - avg) > f; b(i, j) = avg; end

end

end end

end

medianfilter.m
function [ b ] = medianfilter( a, he , wi, f ) %MEDIANFILTER this function calculate the image after a median filter at %threadhold f with window size h x w %add your code here %init b = a; [h w d] = size(b); med = zeros(1 , wi * he); mid = round( (wi * he) / 2 + 0.1); % Xac dinh tam va pham vi if rem(he , 2) tx = (he + 1) / 2; up = he - tx; down = up; else tx = he / 2; up = he - tx - 1; down = he - up - 1; end if rem(wi , 2) ty = (wi + 1) / 2; left = wi - ty; right = left; else ty = wi / 2; left = wi - ty - 1; right = wi - left - 1; end %nhan chap for i = 1 : h; for j = 1 : w; if (i <= up) || (j <= left) || (i > h - down) || (j > w - right); b(i, j) = 0; else % med count = 0; for m = -up : down; for n = - left : right; count = count + 1; med(count) = a(i + m , j + n); end end %sap xep for t = 1 : wi * he;

end

for k = t + 1 : wi*he; if med(t) > med(k); temp = med(t); med(t) = med (k); med(k) = temp; end end

end end end end

%check if abs(a(i, j) - med(mid)) > f; b(i, j) = med(mid); end

convolution.m
function [ b ] = convolution( a , c ) %CONVOLUTION this function calculate the convolution of matrix a with %matrix c %do your calculation here b = a; [h w d] = size(b); [he wi] = size(c); % Xac dinh tam va pham vi if rem(he , 2) tx = (he + 1) / 2; up = he - tx; down = up; else tx = he / 2; up = he - tx - 1; down = he - up - 1; end if rem(wi , 2) ty = (wi + 1) / 2; left = wi - ty; right = left; else ty = wi / 2; left = wi - ty - 1; right = wi - left - 1; end %nhan chap for i = 1 : h; for j = 1 : w; if (i <= up) || (j <= left) || (i > h - down) || (j > w - right); b(i, j) = 0;

else

res = 0; for m = -up : down; for n = - left : right; res = res + a(i + m, j + n) * c(tx + m, ty + n); end end b(i, j) = res;

end end end

end

Canny.m
function canny_edges(max_hysteresis_thresh, min_hysteresis_thresh,sigma); %%INIT VARIABLES: %%Read in image ORIGINAL_IMAGE=imread('c:\lenna.pgm'); %%Convert to double: ORIGINAL_IMAGE=im2double(ORIGINAL_IMAGE); %%Save height and width [H,W]=size(ORIGINAL_IMAGE); %%Derivatives in x and y derivative_x=zeros(H,W); derivative_y=zeros(H,W); %%Gaussian kernel size_of_kernel = 6*sigma+1; adjust= ceil(size_of_kernel/2) Y_GAUSSIAN=zeros(size_of_kernel,size_of_kernel); X_GAUSSIAN=zeros(size_of_kernel,size_of_kernel); %%Create gaussian kernels for both x and y directions based on the sigma %%that was given. for i=1:size_of_kernel for iiii=1:size_of_kernel

Y_GAUSSIAN(i,iiii) = -( (i-((size_of_kernel-1)/2)-1)/( 2* pi * sigma^3 ) ) * exp ( - ( (i-((size_of_kernel-1)/2)-1)^2 + (iiii((size_of_kernel-1)/2)-1)^2 )/ (2*sigma^2) ); end end for i=1:size_of_kernel for iiii=1:size_of_kernel X_GAUSSIAN(i,iiii) = -( (iiii-((size_of_kernel-1)/2)-1)/( 2* pi * sigma^3 ) ) * exp ( - ( (i-((size_of_kernel-1)/2)-1)^2 + (iiii((size_of_kernel-1)/2)-1)^2 )/ (2*sigma^2) ); end end GRADIENT = zeros(H,W);

non_max = zeros(H,W); post_hysteresis = zeros(H,W); %%Image Derivatives: for r=1+ceil(size_of_kernel/2):H-ceil(size_of_kernel/2) for c=1+ceil(size_of_kernel/2):W-ceil(size_of_kernel/2) reference_row= reference_colum= r-ceil(size_of_kernel/2); c-ceil(size_of_kernel/2);

for yyy=1:size_of_kernel for yyy_col=1:size_of_kernel derivative_x(r,c) = derivative_x(r,c) + ORIGINAL_IMAGE(reference_row+yyy-1, reference_colum+yyy_col1)*X_GAUSSIAN(yyy,yyy_col); end end end end for r=1+ceil(size_of_kernel/2):H-ceil(size_of_kernel/2) for c=1+ceil(size_of_kernel/2):W-ceil(size_of_kernel/2) reference_row= r-ceil(size_of_kernel/2);

reference_colum=

c-ceil(size_of_kernel/2);

for yyy=1:size_of_kernel for yyy_col=1:size_of_kernel derivative_y(r,c) = derivative_y(r,c) + ORIGINAL_IMAGE(reference_row+yyy-1, reference_colum+yyy_col1)*Y_GAUSSIAN(yyy,yyy_col); end end end end %%Compute the gradient magnitufde based on derivatives in x and y: for r=1+ceil(size_of_kernel/2):H-ceil(size_of_kernel/2) for c=1+ceil(size_of_kernel/2):W-ceil(size_of_kernel/2) GRADIENT(r,c) = sqrt (derivative_x(r,c)^2 + derivative_y(r,c)^2 ); end end %%Perform Non maximum suppression: non_max = GRADIENT; for r=1+ceil(size_of_kernel/2):H-ceil(size_of_kernel/2) for c=1+ceil(size_of_kernel/2):W-ceil(size_of_kernel/2) %%quantize: if (derivative_x(r,c) == 0) tangent = 5; else tangent = (derivative_y(r,c)/derivative_x(r,c)); end if (-0.4142<tangent & tangent<=0.4142) if(GRADIENT(r,c)<GRADIENT(r,c+1) | GRADIENT(r,c)<GRADIENT(r,c-1)) non_max(r,c)=0; end end if (0.4142<tangent & tangent<=2.4142)

if(GRADIENT(r,c)<GRADIENT(r-1,c+1) | GRADIENT(r,c)<GRADIENT(r+1,c-1)) non_max(r,c)=0; end end if ( abs(tangent) >2.4142) if(GRADIENT(r,c)<GRADIENT(r-1,c) | GRADIENT(r,c)<GRADIENT(r+1,c)) end end if (-2.4142<tangent & tangent<= -0.4142) if(GRADIENT(r,c)<GRADIENT(r-1,c-1) | GRADIENT(r,c)<GRADIENT(r+1,c+1)) non_max(r,c)=0; end end end end post_hysteresis = non_max; for r=1+ceil(size_of_kernel/2):H-ceil(size_of_kernel/2) for c=1+ceil(size_of_kernel/2):W-ceil(size_of_kernel/2) if(post_hysteresis(r,c)>=max_hysteresis_thresh) post_hysteresis(r,c)=1; end if(post_hysteresis(r,c)<max_hysteresis_thresh & post_hysteresis(r,c)>=min_hysteresis_thresh) post_hysteresis(r,c)=2; end if(post_hysteresis(r,c)<min_hysteresis_thresh) post_hysteresis(r,c)=0; end end vvvv = 1; while (vvvv == 1) vvvv = 0; for r=1+ceil(size_of_kernel/2):H-ceil(size_of_kernel/2) for c=1+ceil(size_of_kernel/2):W-ceil(size_of_kernel/2) end non_max(r,c)=0;

if (post_hysteresis(r,c)>0) if(post_hysteresis(r,c)==2) if( post_hysteresis(r-1,c-1)==1 | post_hysteresis(r1,c)==1 | post_hysteresis(r-1,c+1)==1 | post_hysteresis(r,c-1)==1 | post_hysteresis(r,c+1)==1 | post_hysteresis(r+1,c-1)==1 | post_hysteresis(r+1,c)==1 | post_hysteresis(r+1,c+1)==1 ) post_hysteresis(r,c)=1; vvvv == 1; end end end end end end for r=1+ceil(size_of_kernel/2):H-ceil(size_of_kernel/2) for c=1+ceil(size_of_kernel/2):W-ceil(size_of_kernel/2) if(post_hysteresis(r,c)==2) post_hysteresis(r,c)==0; end end end imwrite(ORIGINAL_IMAGE,'C:\Documents and Settings\User\Desktop\Results\original_image.bmp'); imwrite(derivative_x,'C:\Documents and Settings\User\Desktop\Results\derivative_x.bmp'); imwrite(derivative_y,'C:\Documents and Settings\User\Desktop\Results\derivative_y.bmp'); imwrite(GRADIENT,'C:\Documents and Settings\User\Desktop\Results\gradient.bmp'); imwrite(non_max,'C:\Documents and Settings\User\Desktop\Results\non_max_supr.bmp'); imwrite(post_hysteresis,'C:\Documents and Settings\User\Desktop\Results\original_image.bmp');

You might also like