Bài tập tham khảo xử lý ảnh số

11 6.1K 114
Bài tập tham khảo xử lý ảnh số

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Những bài tập về xử lý ảnh số có code và minh họa cụ thể với Matlab (môn học Xử lý ảnh - chương trình đào tạo thạc sỹ chuyên ngành Khoa học máy tính)

BÀITẬPXỬLÝẢNHSỐ 1) Lọcảnh(sửdụngphéptươngquan)trênmiềnkhônggian filtered=spatial_filter(image,mask) Trongđóimagelàảnhxámcầnlọc,masklàmặtnạlọc(kíchthướcmỗichiềulàsốlẻ), filteredlàảnhsaukhilọc. Bàilàm: functionloc_anh=spatial_filter(image,mask) clc;closeall; [row_image,colum_image]=size(image); [row_mask,colum_mask]=size(mask); fori=1:row_image+2*floor(row_mask/2) forj=1:colum_image+2*floor(colum_mask/2) loc_anh(i,j)=0; end end fori=1:row_image forj=1:colum_image loc_anh(i+floor(row_mask/2),j+floor(colum_mask/2))=image(i,j); end end fori=1:row_image forj=1:colum_image bientam=0; foru=1:row_mask forv=1:colum_mask bientam=bientam+loc_anh(ifloor(row_mask/2)+u, jfloor(colum_mask/2)+v)*mask(u,v); end end image(i,j)=bientam; end end loc_anh=image; end Thaotác:(tacómatrậnAlàảnhvàmlàmặtnạlọctùyý) >> A=[1 2 3 4 ; 5 6 7 8 ; 9 0 1 2 ] A = 1 2 3 4 5 6 7 8 9 0 1 2 >> m = [0 1 0 ; 1 1 1 ; 0 1 0] m = 0 1 0 1 -4 1 0 1 0 >> loc_anh = spatial_filter(A,m) loc_anh = 3 2 1 -5 -4 -10 -10 -19 -31 16 5 1 2) LọcmộtảnhbấtkỳbằnghàmđãviếtvàsosánhvớikếtquảlọcbằnghàmhỗtrợbởiMatlab(ví dụfspecial,imfilter,…). Bàilàm:CũngvớimatrậnA(ảnhA)tasửdụngvớihàmhỗtrợthìkếtquảvẫnkothayđổi. Nhưngviệcsửdụngcáchàmcósẵnsẽgiúptagiảmthờigianchoviệcviếthàmnhưởcâu1.Ví dụ:tasửdụngfspecial,imfilter,tacó: >>A=[1234;5678;9012] A= 1234 5678 9012 >>m=fspecial('laplacian',0) m= 010 141 010 >>imfilter(A,m) ans=3215 4101019 311651 3) Thựchiệnlọctrungvị,lọcmin,lọcmax,lọcmidpointchomộtảnhxámvớimặtnạlọccókích thướcnnbấtkỳ(nlẻvàn>1). Bàilàm:chọnn=3 function[locmax,locmin,trungvi,midpoint]=locminmax(image,n) clc;closeall; [row_image,colum_image]=size(image); fori=1:row_image+2*floor(n/2) forj=1:colum_image+2*floor(n/2) loc_anh(i,j)=0; end end fori=1:row_image forj=1:colum_image loc_anh(i+floor(n/2),j+floor(n/2))=image(i,j); end end forI=1:row_image forj=1:colum_image max=loc_anh(ifloor(n/2)+1,jfloor(n/2)+1); min=loc_anh(ifloor(n/2)+1,jfloor(n/2)+1); foru=1:n forv=1:n trung_vi(u,v)=loc_anh(ifloor(n/2)+u,jfloor(n/2)+v); ifloc_anh(ifloor(n/2)+u,jfloor(n/2)+v)>max max=loc_anh(ifloor(n/2)+u,jfloor(n/2)+v); end ifloc_anh(ifloor(n/2)+u,jfloor(n/2)+v)<min min=loc_anh(ifloor(n/2)+u,jfloor(n/2)+v); end end end vector=sort(reshape(trung_vi,1,n*n)); trungvi(i,j)=vector(ceil(n*n/2)); locmax(i,j)=max; locmin(i,j)=min; midpoint(i,j)=round((max+min)/2); end end end Thaotác: >>A=[1234;5678;9012] A= 1234 5678 9012 >>m=fspecial('laplacian',0) m= 010 141 010 >>[locmax,locmin,trungvi,midpoint]=locminmax(A,3) locmax= 6788 9988 9988 locmin= 0000 0000 0000 trungvi= 0230 1332 0110 midpoint= 3444 5544 5544 4) Lọctrungbìnhhìnhhọc,trungbìnhHarmonicvàtrungbìnhContraharmonicvớimặtnạlọccó kíchthước33. Bàilàm: function[hinhhoc,har,con]=loctrungbinh(image,q) clc;closeall; [row_image,colum_image]=size(image);n=3; fori=1:row_image+2*floor(n/2) forj=1:colum_image+2*floor(n/2) loc_anh(i,j)=0; end end fori=1:row_image forj=1:colum_image loc_anh(i+floor(n/2),j+floor(n/2))=image(i,j); end end fori=1:row_image forj=1:colum_image bientam=1; s=0; tu=0; mau=0; foru=1:n forv=1:n bientam=bientam+loc_anh(ifloor(n/2)+u, jfloor(n/2)+v); s=s+1/loc_anh(ifloor(n/2)+u,jfloor(n/2)+v); tu=tu+loc_anh(ifloor(n/2)+u,jfloor(n/2)+v)^(q+1); mau=mau+loc_anh(ifloor(n/2)+u,jfloor(n/2)+v)^q; end end hinhhoc(i,j)=bientam*(1/n^2); har(i,j)=n*n/s; con(i,j)=tu/mau; end end end Thaotác:>>A=[1234;5678;9012] A= 1234 5678 9012 >>[hinhhoc,har,con]=loctrungbinh(A,1) hinhhoc= 1.66672.77783.44442.5556 2.66673.88893.77782.8889 2.33333.22222.77782.1111 har= 0000 0000 0000 con= 4.71435.16675.93336.2727 6.39136.05885.54555.7200 7.10006.85716.41676.5556 5) Thựchiệncânbằnghistogramvớiđầuvàolàảnhxám. Bàilàm: functionbai5() clc;closeall; origin=imread('D:\Game\anh\Kayle.jpg'); gray=rgb2gray(origin); hist=histeq(gray); subplot(2,2,1),imshow(gray);title('gray'); subplot(2,2,2),imshow(hist);title('hist'); subplot(2,2,3),imhist(gray);title('histgray'); subplot(2,2,4),imhist(hist);title('histogram'); end 6) CắtngưỡngtựđộngdựatrênthuậttoáncắtngưỡngOtsu. Bàilàm: functionbai6() clc;closeall; origin=imread('D:\Game\anh\Kayle.jpg') subplot(1,2,1); imshow(origin),title('Orgin'); laynguong=graythresh(origin); BlackWhite=im2bw(origin,laynguong); subplot(1,2,2); imshow(BlackWhite),title('CatnguongtudongOtsu'); end 7) Hiểnthị8látmặtbitcủamộtảnhxám. Bàilàm: functionbai7() clc;closeall; origin=imread('D:\Game\anh\Kayle.jpg'); gray=rgb2gray(origin); subplot(3,3,1),imshow(gray),title('gray'); bit1=bitget(gray,1)*2^0; subplot(3,3,2),imshow(bit1,[]),title('bit1'); bit2=bitget(gray,2)*2^1; subplot(3,3,3),imshow(bit2,[]),title('bit2'); bit3=bitget(gray,3)*2^2; subplot(3,3,4),imshow(bit3,[]),title('bit3'); bit4=bitget(gray,4)*2^3; subplot(3,3,5),imshow(bit4,[]),title('bit4'); bit5=bitget(gray,5)*2^4; subplot(3,3,6),imshow(bit5,[]),title('bit5'); bit6=bitget(gray,6)*2^5; subplot(3,3,7),imshow(bit6,[]),title('bit6'); bit7=bitget(gray,7)*2^6; subplot(3,3,8),imshow(bit7,[]),title('bit7'); bit8=bitget(gray,8)*2^7; subplot(3,3,9),imshow(bit8,[]),title('bit8'); end 8) Thựchiệncácthaotácerosion,dilation,opening,closing. Bàilàm: functionbai8() clc;closeall; origin=imread('D:\Game\anh\Kayle.jpg'); gray=rgb2gray(origin); blackwhite=im2bw(gray,graythresh(gray)); erosion=imerode(blackwhite,[0,1,0;1,1,1;0,1,0]); subplot(3,2,1),imshow(origin),title('origin'); subplot(3,2,2),imshow(blackwhite),title('BlackWhite'); subplot(3,2,3),imshow(erosion),title('erosion'); dilation=imdilate(blackwhite,[0,1,0;1,1,1;0,1,0]); subplot(3,2,4),imshow(dilation),title('dilation'); closing=imclose(blackwhite,[0,1,0;1,1,1;0,1,0]); subplot(3,2,5),imshow(closing),title('closing'); opening=imopen(blackwhite,[0,1,0;1,1,1;0,1,0]); subplot(3,2,6),imshow(opening),title('opening'); end 9) ViếthàmMatlabthựchiệntuầntựcácchứcnăngsau: ● Đọcmộtảnhmàuvàobiếnoriginvàchuyểnsangảnhxám(gánvàobiếngray). ● Táchriêngtừngkênhmàu(R,G,B)củaảnhorigin. ● ThựchiệnlọctrungvịchotừngkênhmàuR,G,B. ● TạoảnhmàuRGBcótênrgb_filteredtừkếtquảlọc3kênhmàuởtrên. ● Thựchiệnlọctrungvịchoảnhxámgray,gánkếtquảvàobiếngray_filtered. ● Hiểnthịđộsaikháctạimỗiđiểmảnhgiữaảnhxámcủaảnhrgb_filteredvàgray_filtered. Bàilàm: functiond=bai9(file) clc;closeall; origin=imread('D:\Game\anh\Kayle.jpg'); subplot(3,2,1),imshow(origin),title('origin'); gray=rgb2gray(origin); subplot(3,2,2),imshow(gray),title('gray'); kenhR=origin(:,:,1); subplot(3,2,3),imshow(kenhR),title('Red'); kenhG=origin(:,:,2); subplot(3,2,4),imshow(kenhG),title('Green'); kenhB=origin(:,:,3); subplot(3,2,5),imshow(kenhB),title('Blue'); trungviR=medfilt2(kenhR,[33]); trungviG=medfilt2(kenhG,[33]); trungviB=medfilt2(kenhB,[33]); rgb_filtered=cat(3,trungviR,trungviG,trungviB); gray_filtered=medfilt2(gray,[33]); gray_RGB=rgb2gray(rgb_filtered); d=abs(gray_RGBgray_filtered); subplot(3,2,6),imshow(d,[]),title('saikhac'); end ViếtchươngtrìnhMatlabthựchiệnnhậndạngmộtđốitượngtùyý(1chữcái,chữsố,vậtthể bấtkỳ,…) Bàilàm: NhậnDạngKhuônMặt *Matlab2013cóhàmhỗtrợnhậndạngkhuônmặt.tacóthểsửdụnghàmhỗtrợnàynhưsau: functionbai10() %Example1:Facedetection % faceDetector=vision.CascadeObjectDetector();%Default:findsfaces Origin=imread('path_image\.jpg'); bboxes=step(faceDetector,Origin);%Detectfaces %Annotatedetectedfaces IFaces=insertObjectAnnotation(Origin,'rectangle',bboxes,'Face'); figure,imshow(IFaces),title('Detectedfaces'); end *Ýtưởngthựchiệnviếtchươngtrình(khôngsửdụnghàmhỗtrợ): Sơđồkhốichobàitoán Tạo1cơsởdữliệugồmnảnh,mỗiảnhđượcđặttêntheothứtựlàcácsónguyêntừ1đếnn đểtiệnchoviệcquảnlýtậpảnhnày(ntùyvàosốlượngảnh). Ảnhđượcđưavàotậpcơsởdữliệusẽđượcdùngđểhuấnluyện,từđâysửdụngcácphương pháptríchrútđặctrưngvàđưaravectođặctrưngchoviệcnhậndạng. [...]...Khi đưa ảnh cần nhận dạng vào ta thực hiện tính tọa độ hình chiếu của ảnh này và của cả những ảnh trong cơ sở dữ liệu đã có, rồi đem so sánh kết quả Thực hiện đo khoảng cách (khoảng cách Euclid) giữa tọa độ hình chiếu của ảnh cần kiểm tra với tọa độ hình chiếu của các ảnh trong cơ sở dữ liệu. Bức ảnh nào trong cơ sở dữ liệu có khoảng cách so với ảnh cần kiểm tra là ngắn nhất thì ta chọn . BÀITẬPXỬLÝ ẢNH SỐ 1) Lọc ảnh (sửdụngphéptươngquan)trênmiềnkhônggian filtered=spatial_filter(image,mask) Trongđóimagelà ảnh xámcầnlọc,masklàmặtnạlọc(kíchthướcmỗichiềulà số lẻ), filteredlà ảnh saukhilọc. Bài làm: functionloc_anh=spatial_filter(image,mask) clc;closeall; [row_image,colum_image]=size(image); [row_mask,colum_mask]=size(mask); fori=1:row_image+2*floor(row_mask/2) forj=1:colum_image+2*floor(colum_mask/2) loc_anh(i,j)=0; end end fori=1:row_image forj=1:colum_image loc_anh(i+floor(row_mask/2),j+floor(colum_mask/2))=image(i,j); end end fori=1:row_image forj=1:colum_image bientam=0; foru=1:row_mask forv=1:colum_mask bientam=bientam+loc_anh(ifloor(row_mask/2)+u, jfloor(colum_mask/2)+v)*mask(u,v); end end image(i,j)=bientam; end end loc_anh=image; end Thaotác:(tacómatrậnAlà ảnh vàmlàmặtnạlọctùyý) >>. NhậnDạngKhuônMặt *Matlab2013cóhàmhỗtrợnhậndạngkhuônmặt.tacóthểsửdụnghàmhỗtrợnàynhưsau: functionbai10() %Example1:Facedetection % faceDetector=vision.CascadeObjectDetector();%Default:findsfaces Origin=imread('path_image.jpg'); bboxes=step(faceDetector,Origin);%Detectfaces %Annotatedetectedfaces IFaces=insertObjectAnnotation(Origin,'rectangle',bboxes,'Face'); figure,imshow(IFaces),title('Detectedfaces'); end *Ýtưởngthựchiệnviếtchươngtrình(khôngsửdụnghàmhỗtrợ): Sơđồkhốicho bài toán Tạo1cơsởdữliệugồmn ảnh, mỗi ảnh đượcđặttêntheothứtựlàcácsónguyêntừ1đếnn đểtiệnchoviệcquản lý tập ảnh này(ntùyvào số lượng ảnh) . Ảnh đượcđưavào tập cơsởdữliệusẽđượcdùngđểhuấnluyện,từđâysửdụngcácphương pháptríchrútđặctrưngvàđưaravectođặctrưngchoviệcnhậndạng. Khiđưa ảnh cầnnhậndạngvàotathựchiệntínhtọađộhìnhchiếucủa ảnh nàyvàcủacảnhững ảnh trongcơsởdữliệuđãcó,rồiđemsosánhkếtquả. Thựchiệnđokhoảngcách(khoảngcáchEuclid)giữatọađộhìnhchiếucủa ảnh cầnkiểmtra vớitọađộhìnhchiếucủacác ảnh trongcơsởdữliệu.Bức ảnh nàotrongcơsởdữliệucókhoảngcách sovới ảnh cầnkiểmtralàngắnnhấtthìtachọn.

Ngày đăng: 14/03/2014, 23:41

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan