% Tachbien dua vao ki thuat Gradient anh %Tach bien anh mau RGB % Tao ham trong Mfile luu vo ten "colorgrad.m" function[VG,A,PPG]=colorgrad(f,dec,T) %Tinh toan vi phan theo phuong x va y cua ba anh thanh phan su dung toan tu %mat na sh=fspecial(dec);% ham tao ra mat na cua moi thuat toan(dec) sv=sh'; Rx=imfilter(double(f(:,:,1)),sh,'replicate'); Ry=imfilter(double(f(:,:,1)),sv,'replicate'); Gx=imfilter(double(f(:,:,2)),sh,'replicate'); Gy=imfilter(double(f(:,:,2)),sv,'replicate'); Bx=imfilter(double(f(:,:,3)),sh,'replicate'); By=imfilter(double(f(:,:,3)),sv,'replicate'); %Tinh cac thong so cua vec to gradient gxx=Rx.^2+ Gx.^2+ Bx.^2; gyy=Ry.^2+ Gy.^2+ By.^2; gxy=Rx.*Ry+ Gx.*Gy+ Bx.*By; A=0.5*(atan(2*gxy./(gxx-gyy+eps))); G1=0.5*((gxx+gyy)+(gxx-gyy).*cos(2*A)+2*gxy.*sin(2*A)); %Bay gio lap lai voi goc + pi/2. Sau do chon gia trongi cuc dai tai moi diem. A = A+pi/2; G2=0.5*((gxx+gyy)+(gxx-gyy).*cos(2*A)+2*gxy.*sin(2*A)); G1=G1.^0.5; G2=G2.^0.5; %Tinh VG bang cach lay gia tri cuc dai tai moi diem (x,y)va sau do chuan hoa % trong khoang [0 1] VG=mat2gray(max(G1,G2)); %Tinh toan cac gradient tren tung vung mau RG=sqrt(Rx.^2+Ry.^2); GG=sqrt(Gx.^2+Gy.^2); BG=sqrt(Bx.^2+By.^2); %Thuc hien viec tong hop bang cach cong cac ket qua rieng biet va chuan hoa %trong khoang [0 1] PPG=mat2gray(RG+GG+BG); if nargin==3 VG=(VG>T).*VG; PPG=(PPG>T).*PPG; end % Thuc hien code trong Command window close all; clear all; f=imread('football.jpg'); % Voi nguong T=0.125 T=0.125; figure(1); subplot(3,1,1); imshow(f); str={['RGB image, threshold T=' num2str(T)]}; title(str); [VG,A,PPG]=colorgrad(f,'sobel',T);% dung thuat toan sobel subplot(3,1,2); imshow(VG); title('mat na Sobel'); [VG,A,PPG]=colorgrad(f,'prewitt',T); % dung thuat toan prewitt subplot(3,1,3); imshow(VG); title('mat na Prewitt'); % Voi nguong T=0.5 T=0.5; figure(2); subplot(3,1,1); imshow(f); str={['RGB image, threshold T=' num2str(T)]}; title(str); [VG,A,PPG]=colorgrad(f,'sobel',T);% dung thuat toan sobel subplot(3,1,2); imshow(VG); title('mat na Sobel'); [VG,A,PPG]=colorgrad(f,'prewitt',T); % dung thuat toan prewitt subplot(3,1,3); imshow(VG); title('mat na Prewitt'); % TACHBIEN ANH XAM HOAC ANH DEN TRANG % Tao ham function trong Mfile luu voi ten "TRANGDEN.m" function B=TRANGDEN(f,dec,T); B=edge(f,dec); % Thuc hien code trong Command window f=imread('cameraman.tif'); figure; subplot(3,1,1);imshow(f); % nguong tachbien la T=0.125 T=0.125; B=TRANGDEN(f,'sobel',T); subplot(3,1,2),imshow(B); B=TRANGDEN(f,'prewitt',T); subplot(3,1,3),imshow(B); . % Tach bien dua vao ki thuat Gradient anh %Tach bien anh mau RGB % Tao ham trong Mfile luu vo ten "colorgrad.m". B=edge(f,dec); % Thuc hien code trong Command window f=imread('cameraman.tif'); figure; subplot(3,1,1);imshow(f); % nguong tach bien la T=0.125 T=0.125;