baocao

22
ĐỀ TÀI : NGHIÊN CỨU THUẬT TOÁN PHÁT HIỆN BIÊN CANNY NHÓM 10 – D09CNTT4 Vũ Bình Long Nguyễn Thành Công Nguyễn Thành Trung Lê Tuấn Thành Phạm Việt Ly Nội dung 1. Giới thiệu thuật toán Canny 2. Thay đổi các tham số

Upload: vu-binh-long

Post on 28-Oct-2014

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: baocao

ĐỀ TÀI :

NGHIÊN CỨU THUẬT TOÁN PHÁT HIỆN BIÊN CANNY

NHÓM 10 – D09CNTT4

Vũ Bình Long

Nguyễn Thành Công

Nguyễn Thành Trung

Lê Tuấn Thành

Phạm Việt Ly

Nội dung

1. Giới thiệu thuật toán Canny

2. Thay đổi các tham số

3. Các ứng dụng hướng nghiên cứu tương lai

Page 2: baocao

1. Giới thiệu thuật toán Canny

Phương pháp này do John Canny ở phòng thí nghiệm MIT khởi xướng vàonăm 1986. Canny đã đưa một tập hợp các ràng buộc mà một phương pháp phát hiện biên phải đạt được. Ông đã trình bày một phương pháp tối ưu nhất để thực hiện được các ràng buộc đó. Và phương pháp này được gọi là phương pháp Canny.

Ý tưởng thuật toán: Ý tưởng của phương pháp này là định vị đúng vị trí bằngcách cực tiểu hoá phương sai δ2 của vị trí các điểm cắt "Zero" hoặc hạn chế số điểmcực trị cục bộ để chỉ tạo ra một đường bao.Các ràng buộc mà phương pháp phát hiện biên Canny đã thực hiện được đó là:mức lỗi, định vị và hiệu suất. Trong đó:- Mức lỗi: có ý nghĩa là một phương pháp phát hiện biên chỉ và phải tìm tất cảcác biên, không biên nào được tìm bị lỗi.- Định vị: Điều này nói đến độ chênh lệch cấp xám giữa các điểm trên cùngmột biên phải càng nhỏ càng tốt.- Hiệu suất: là làm sao cho khi tách biên không được nhận ra nhiều biên trongkhi chỉ có một biên tồn tại.Ràng buộc mức lỗi và định vị được dùng để đánh giá các phương pháp pháthiện biên. Còn ràng buộc về hiệu suất thì tương đương với mức lỗi dương.Canny đã giả thiết rằng nhiễu trong ảnh tuân theo phân bố Gauss và đồng thờiông cũng cho rằng một phương pháp phát hiện biên thực chất là một bộ lọc nhânxoắn có khả năng làm mịn nhiễu và định vị được cạnh. Vấn đề là tìm một bộ lọc sao cho thoả mãn tối ưu nhất các ràng buộc ở trên.

Hoạt động của thuật toán

Thuật toán được tiến hành qua bốn bước cơ bản sau:- Đọc ảnh I cần xử lý: tiến hành làm trơn ảnh bằng cách nhân xoắn ảnh với bộlọc Gauss.- Đạo hàm bậc nhất kết quả trên theo hai hướng x và y:Điều này tương ứng với việc nhân xoắn ảnh kết quả ở bước 1 theo hai hướng

Page 3: baocao

(x và y) với một bộ lọc dựa trên đạo hàm bậc nhất (các bộ lọc của kỹ thuật Gradient).Từ đó đưa ra kết quả đạo hàm ảnh sau khi tiến hành nhân xoắn với Gauss.- Cho ảnh kết quả ở bước trên tiến hành “Non-maximum Suppression” Nghĩalà loại bỏ bớt các điểm cạnh (loại bỏ bớt nhiễu), chỉ giữ lại điểm có mức xám cao.- Tiến hành thực hiện áp dụng ngưỡng (ngưỡng cao và ngưỡng thấp) để loạibỏ một số cạnh xấu.

2.Thay đổi các tham số

Ảnh ban đầu

Sử dụng độ lệch chuẩn là 1, ngưỡng trên mà dưới là 255 và 1, ta được

Giảm ngưỡng về 150, ta được ảnh

Page 4: baocao

Độ lệch chuẩn 2 và ngưỡng 100

4. Các ứng dụng và hướng nghiên cứu tương lai

Thuật toán canny ứng dụng rất rộng, có thể dùng C, C++, Java.. để phát triển các ứng dụng phát hiện biên của ảnh.

Dùng java, có thể phát triển một ứng dụng nhỏ, bằng flash, tích hợp trên trình duyệt

Page 5: baocao

Ta có thể phát triển một ứng dụng , nhận dạng biên của ảnh trong video với java.

Page 6: baocao
Page 7: baocao

PHỤ LỤC

CODE :

split.m

function [ b ] = split( a,f)%SPLIT This function split at a threshold f%add your method heren=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 endend

errdiffu.m

function [ b ] = errdiffu( a ,f)%ERRDIFU % add your method heren=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(':'); endend 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

Page 8: baocao

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 hereb = 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 endendend

incrbrightness.m

function [ b ] = incrbrightness( a,c )

Page 9: baocao

%INCRBRIGHTNESS this function increase brightness of the whole picture a% by c. %add your method heren=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 endend

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(':'); endendfor 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; endend for k=1:256 fprintf('%d ', h(k));endend

histbal.m

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

Page 10: baocao

% 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);endfor i = 1 : h for j = 1 : w b(i, j) = his(b(i, j) + 1); endendend

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;endf = round(max(fg)); switch(func) case 1 b = split(a,f); case 2 b = errdiffu(a,f); case 3 b = errdiffu2d(a,f); end

Page 11: baocao

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 %initb = a;[h w d] = size(b);med = zeros(1 , wi * he); % Xac dinh tam va pham viif 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 chapfor 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

Page 12: baocao

endendend

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 %initb = a;[h w d] = size(b);med = zeros(1 , wi * he);mid = round( (wi * he) / 2 + 0.1); % Xac dinh tam va pham viif 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 chapfor 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;

Page 13: baocao

for k = t + 1 : wi*he; if med(t) > med(k); temp = med(t); med(t) = med (k); med(k) = temp; end end end %check if abs(a(i, j) - med(mid)) > f; b(i, j) = med(mid); end end endendend

convolution.m

function [ b ] = convolution( a , c )%CONVOLUTION this function calculate the convolution of matrix a with%matrix c %do your calculation hereb = a;[h w d] = size(b);[he wi] = size(c); % Xac dinh tam va pham viif 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 chapfor i = 1 : h; for j = 1 : w; if (i <= up) || (j <= left) || (i > h - down) || (j > w - right); b(i, j) = 0;

Page 14: baocao

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 endendend

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

Page 15: baocao

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= 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_x(r,c) = derivative_x(r,c) + ORIGINAL_IMAGE(reference_row+yyy-1, reference_colum+yyy_col-1)*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);

Page 16: baocao

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_col-1)*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)

Page 17: baocao

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)) non_max(r,c)=0; 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 endend 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)

Page 18: baocao

if (post_hysteresis(r,c)>0) if(post_hysteresis(r,c)==2) if( 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)==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');

Page 19: baocao