Thử nghiệm với bộ lọc trung bình

Một phần của tài liệu Nghiên cứu một số kỹ thuật phục hồi ảnh và ứng dụng (Trang 52 - 73)

5. Ý nghĩa khoa học của đề tài

3.5.2 Thử nghiệm với bộ lọc trung bình

Ý tưởng của bộ lọc : Giá trị điểm ảnh lỗi sẽ được thay thế bằng giá trị trung bình lấy từ các pixels kế cận.

Ví dụ : Bằng cách xác định một mặt nạ, người ta giới hạn vùng liên quan cần tính toán giá trị trung bình cần tìm của pixel lỗi. Điểm ảnh bất thường ở trung tâm của cửa sổ có giá trị cao hơn các pixel kế cận. Có thể sử lỗi cho pixel này bằng cách thay giá trị trung bình của 8 pixel lân cận : 1/8(23+25+24+22+25+25+23+23)=23. 75 vào mức xám của nó. Vì vậy giá trị của điểm ảnh bị lỗi sẽ là 24 (được làm tròn).

Giải thuật

B1 : Tìm điểm ảnh bị lỗi trong xử lý

B2 : Chọn kích thước của cửa sổ(mặt nạ)(là một ma trận vuông nxn thường là chiều dài lẻ ví dụ 3x3, 5x5).

B4 : Thay giá trị điểm ảnh lỗi phát hiện được tính bằng trung bình vừa tính được

B5 : Dịch cửa sổ sang cột (hàng) kế tiếp B6 : Quay lại B3.

Các đoạn mã lệnh a. Chọn ảnh

function chonanh_Callback(hObject, eventdata, handles)

[filename,pathname]=uigetfile({'*.tif;*.bmp;*.gif;*.jpg;*.png'},'pick an

image file');

S=imread([pathname,filename]); handles.axes = S;

t=rgb2gray(S);

subplot(handles.axes1); imshow(t),title('ANH GOC'); guidata(hObject,handles);

b. Ảnh bị nhiễu muối-tiêu

function anhhuhai_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

g=imnoise(t, 'salt & pepper', 0.07); figure% cong nhieu muoi tieu vao anh f

imshow(g),title('anh hu hai muoi tieu voi d=0,07'); guidata(hObject,handles);

c. Hàm lọc trung bình

function loctrungbinh_Callback(hObject, eventdata, handles)

t=rgb2gray(f);

g=imnoise(t, 'salt & pepper', 0.07);%cong nhieu muoi tieu vao anh

f2 = medfilt2(g, [m n]); figure

imshow(f2),title('loc nhieu voi bo loc trung binh (diem giua) voi ham

medfilt2 ');

d. Lọc cấp độ thấp với mặt nạ (3×3)

function locdiemgiua_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

g=imnoise(t, 'salt & pepper', 0.07); m = 3; n = 3;

w = fspecial('average', [m, n]); f1 = imfilter(g, w);

figure

imshow(f1),title('loc nhieu voi bo loc trung binh cap do thap voi ma tran

3x3');

e. Lọc cấp độ thấp với mặt nạ(5×5)

function locdiemgiua5_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

g=imnoise(t, 'salt & pepper', 0.07); e = fspecial('average', [5,5]);

f1 = imfilter(g, e); figure

imshow(f1),title('loc nhieu voi bo loc trung binh cap do thap voi ma tran

f. Lọc theo thứ tự với mặt nạ 3×3

function locthutu3_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

g=imnoise(t, 'salt & pepper', 0.07); f3 = ordfilt2(g,3,[0 1 0;1 1 1;0 1 0]); figure

imshow(f3),title('loc theo tu tu voi ham ordfilt2 voi mat la 3x3');

g. Lọc theo thứ tự với mặt nạ 5×5

function locthutu5_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

g=imnoise(t, 'salt & pepper', 0.07);

f4 = ordfilt2(g,5,[0 0 1 0 0;0 0 1 0 0;1 1 1 1 1;0 0 1 0 0;0 0 1 0 0]);%loc theo thu tu voi mat la 5x5

figure

imshow(f4),title('loc theo tu tu voi ham ordfilt2 voi mat la 5x5');

h. Ảnh bị nhiễu Gauss

function anhhuhaigauss_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

g=imnoise(t, 'gauss');% cong nhieu muoi tieu vao anh f

figure

imshow(g),title('Anh hu hai Gauss'); guidata(hObject,handles);

f=handles.axes; t=rgb2gray(f);

g=imnoise(t,'Gauss'); a3=fspecial ('average'); tg3=imfilter (g, a3) ;

figure,imshow(tg3);title('Anh loc trung binh voi mat la 3x3');

j. Lọc trung bình mặt nạ 5×5

function Loctrungbinhgauss_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

g=imnoise(t,'Gauss');

a5=fspecial ('average', [5, 5]) ; tg5=imfilter (g, a5) ;

figure,imshow(tg5);title('Anh Loc Trung Binh mat la 5x5');

k. Lọc thích nghi mặt nạ 5×5

function loctuongthich5_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

t2=imnoise (t, 'Gauss', 0, 0.005) ; t2w=wiener2 (t2, [5, 5]) ;

figure, imshow (t2w) ;title('Anh loc tuong thich voi mat la 5x5');

l. Lọc thích nghi mặt nạ 7×7

function loctuongthickgauss7_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

t2=imnoise (t, 'Gauss', 0, 0.005) ; t2w=wiener2 (t2, [7, 7]) ;

figure, imshow (t2w) ;title('Anh loc tuong thich voi mat la 7x7');

3.6 Kết quả khi chạy chƣơng trình 3.6.1 Chọn ảnh

3.6.3 Lọc trung bình mặt nạ 3×3

3.6.4 Lọc trung bình mặt nạ 5×5

3.6.6 Lọc thích nghi mặt nạ 7×7

3..6.7 Lọc thích nghi mặt nạ 9×9

3.6.9 Lọc trung bình

3.6.10 Lọc cấp độ thấp 3×3

3.6.12 Lọc thứ tự 3×3

KẾT LUẬN VÀ HƢỚNG PHÁT TRIỂN

Những kết quả nghiên cứu của luận văn cho phep rut ra các kết luận sau:

Về mặt nghiên cứu lý thuyết: Đề tài đã nghiên cứu, tìm hiểu và trình bày một cách tổng quan về xử lý ảnh, phục hồi ảnh và các kỹ thuật lọc ảnh nhằm phục hồi lại ảnh gần giống nhât với ảnh gốc ảnh trước khi bị biến dạng.

Luận văn cũng đã trình bày một số thuật toán xử lý ảnh.

Về mặt ứng dụng: Luận văn đã trình bày tổng quan về phần mềm Matlab và các ứng dụng của nó trong xử lý ảnh, từ đó đã cái đặt được một số thuật toán xử lý ảnh, lọc ảnh với mục đích phục hồi lại ảnh gốc sao cho ảnh phục hồi được gần giống nhất so với ảnh gốc.

Trong thời gian làm đề tài với sự hạn chế về thời gian, tài liệu, cơ sở vật chất cũng như hạn chế về kiến thức đòi hỏi bản thân em phải cố gắng tìm tòi và nhiệt tình trong công việc nghiên cứu đề tài và cuối cùng đề tài đã hoàn thành trọn vẹn. Đó là kết quả của một thời gian dài nỗ lực nghiên cứu của bản thân dưới sự hướng dẫn tận tình của giáo viên hướng dẫn nên đề tài đã hoàn thành đúng thời hạn.

Tuy nhiên trong quá trình làm việc em không thể tránh khỏi sai sót. Mong sự chỉ bảo của thầy cô và đóng góp ý kiến của bạn bè để đề tài ngày một hoàn thiện hơn.

Hƣớng phát trển của luận văn: Nghiên cứu và tìm hiểu các kỹ thuật phân vùng ảnh nhằm giúp cho các Y, Bác sĩ trong ngành y tế chẩn đoán bệnh được chính xác hơn, để chữa trị kịp thời cho người bệnh.

TÀI LIỆU THAM KHẢO

[1]. Đỗ Năng Toàn, Phạm việt Bình, “Giáo trình xử lý ảnh”, Đại học Thái Nguyên, 2007.

[2]. Hồ Văn Sung, “Thực hành xử lý số tín hiệu trên máy tính PC với MATLAB”, Nxb. Khoa học và kỹ thuật, 2005.

[3]. Hồ văn Sung, “Xử lý ảnh số lý thuyết và thực hành”, Nxb. Khoa học và kỹ thuật, 2009.

[4]. Nguyễn Quang Hoan, “Giáo trình xử lý ảnh”, Học viện công nghệ bưu chính viễn thông, 2006.

[5]. http://thuthuat.fvn.vn/2011/03/giao-trinh-matlab.html, 2013.

[6]. http://www.mathworks.com/, 2013.

[7]. http://www.mathworks.com/help/wavelet/compression.html, 2013.

[8]. Matlab, MATLAB _TV, nxb. ebooks team, 2011.

[9]. Nguyễn đăng Bình, “Giáo trình xử lý ảnh số”, Trường Đại Học Khoa Học Huế, 2011.

[10]. SCM2511, An Introduction To Digital Image Processing With MATLAB, tài liệu Internet, 2013 .

PHỤ LỤC 1.Thƣ mục chƣơng trình

2.Chƣơng trình

function varargout = sang(varargin)

% SANG M-file for SANG.fig

% SANG , by itself, creates a new SANG or raises the existing % singleton*.

%

% H = SANG returns the handle to a new SANG or the handle to % the existing singleton*.

%

% SANG('CALLBACK',hObject,eventData,handles,...) calls the local

%

% SANG('Property','Value',...) creates a new SANG or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before sang_OpeningFcn gets called. An

% unrecognized property name or invalid value makes property application % stop. All inputs are passed to tien_OpeningFcn via varargin.

%

% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one

% instance to run (singleton)". %

% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help sang % Last Modified by GUIDE v2.5 20-Apr-2013 13:21:56 % Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ...

'gui_OpeningFcn', @tien_OpeningFcn, ... 'gui_OutputFcn', @tien_OutputFcn, ... 'gui_LayoutFcn', [] , ...

'gui_Callback', []); if nargin && ischar(varargin{1})

gui_State.gui_Callback = str2func(varargin{1}); end

else

gui_mainfcn(gui_State, varargin{:}); end

% End initialization code - DO NOT EDIT % --- Executes just before tien is made visible.

function sang_OpeningFcn(hObject, eventdata, handles, varargin)

handles.axes = 0; t=imread('1.png'); v=imread('2.jpg'); h=imread('4.jpg'); subplot(handles.logo); imshow(v); subplot(handles.axes5); imshow(t); subplot(handles.axes1);

imshow(h);title('Dai Hoc Thai Nguyen')

% This function has no output args, see OutputFcn. % hObject handle to figure

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to phuong (see VARARGIN) % Choose default command line output for tien

handles.output = hObject;

% Update handles structure

guidata(hObject, handles);

% UIWAIT makes sang wait for user response (see UIRESUME) % uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.

function varargout = sang_OutputFcn(hObject, eventdata, handles)

% varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure

varargout{1} = handles.output;

% --- Executes on button press in chonanh.

function chonanh_Callback(hObject, eventdata, handles)

[filename,pathname]=uigetfile({'*.tif;*.bmp;*.gif;*.jpg;*.png'},'pick an image file'); S=imread([pathname,filename]); handles.axes = S; t=rgb2gray(S); subplot(handles.axes1); %subplot(handles.axes1);

imshow(t),title('ANH GOC'); guidata(hObject,handles);

% hObject handle to chonanh (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in anhhuhai.

function anhhuhai_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

figure% cong nhieu muoi tieu vao anh f

imshow(g),title('Anh hu hai muoi tieu voi d=0,07 - 7%'); guidata(hObject,handles);

% hObject handle to anhhuhai (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in loctrungbinh.

function loctrungbinh_Callback(hObject, eventdata, handles)

m=3; n=3; f=handles.axes; t=rgb2gray(f);

g=imnoise(t, 'salt & pepper', 0.07);%cong nhieu muoi tieu vao anh

f2 = medfilt2(g, [m n]); figure

imshow(f2),title('loc nhieu voi bo loc trung binh (diem giua) voi ham medfilt2 '); guidata(hObject,handles);

% hObject handle to loctrungbinh (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in locdiemgiua.

function locdiemgiua_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

g=imnoise(t, 'salt & pepper', 0.07); m = 3; n = 3;

w = fspecial('average', [m, n]); f1 = imfilter(g, w);

figure

imshow(f1),title('loc nhieu voi bo loc trung binh cap do thap voi ma tran 3x3'); guidata(hObject,handles);

% hObject handle to locdiemgiua (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in locdiemgiua5.

function locdiemgiua5_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

g=imnoise(t, 'salt & pepper', 0.07); e = fspecial('average', [5,5]);

f1 = imfilter(g, e); figure

imshow(f1),title('loc nhieu voi bo loc trung binh cap do thap voi ma tran 5x5'); guidata(hObject,handles);

% hObject handle to locdiemgiua5 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in close.

function close_Callback(hObject, eventdata, handles)

close();

% hObject handle to close (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in locthutu3.

f=handles.axes; t=rgb2gray(f);

g=imnoise(t, 'salt & pepper', 0.07); f3 = ordfilt2(g,3,[0 1 0;1 1 1;0 1 0]); figure

imshow(f3),title('loc theo tu tu voi ham ordfilt2 voi mat la 3x3');

% hObject handle to locthutu3 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in locthutu5.

function locthutu5_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

g=imnoise(t, 'salt & pepper', 0.07);

f4 = ordfilt2(g,5,[0 0 1 0 0;0 0 1 0 0;1 1 1 1 1;0 0 1 0 0;0 0 1 0 0]);%loc theo thu tu voi mat la 5x5

figure

imshow(f4),title('loc theo tu tu voi ham ordfilt2 voi mat la 5x5');

% hObject handle to locthutu5 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in loctuongthich5.

function loctuongthich5_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

t2=imnoise (t, 'Gauss', 0, 0.005) ; t2w=wiener2 (t2, [5, 5]) ;

figure, imshow (t2w) ;title('Anh loc tuong thich voi mat la 5x5');

% hObject handle to loctuongthich5 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in Loctrungbinhgauss.

function Loctrungbinhgauss_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

g=imnoise(t,'Gauss');

a5=fspecial('average', [5, 5]) ; tg5=imfilter (g, a5) ;

figure,imshow(tg5);title('Anh Loc Trung Binh mat la 5x5');

% hObject handle to Loctrungbinhgauss (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in loctuongthickgauss7.

function loctuongthickgauss7_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

t2=imnoise (t, 'Gauss', 0, 0.005) ; t2w=wiener2 (t2, [7, 7]) ;

figure, imshow (t2w) ;title('Anh loc tuong thich voi mat la 7x7');

% hObject handle to loctuongthickgauss7 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in anhhuhaigauss.

f=handles.axes; t=rgb2gray(f);

g=imnoise(t, 'gauss');% cong nhieu muoi tieu vao anh f

figure

imshow(g),title('Anh hu hai Gauss'); guidata(hObject,handles);

% hObject handle to anhhuhaigauss (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in loctrungbinh3.

function loctrungbinh3_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

g=imnoise(t,'Gauss'); a3=fspecial ('average'); tg3=imfilter (g, a3) ;

figure,imshow(tg3);title('Anh loc trung binh voi mat la 3x3');

% hObject handle to loctrungbinh3 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % --- Executes on button press in loctuongthichgauss9.

function loctuongthichgauss9_Callback(hObject, eventdata, handles)

f=handles.axes; t=rgb2gray(f);

t2=imnoise (t, 'Gauss', 0, 0.005) ; t2w=wiener2 (t2, [9, 9]) ;

% hObject handle to loctuongthichgauss9 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)

Một phần của tài liệu Nghiên cứu một số kỹ thuật phục hồi ảnh và ứng dụng (Trang 52 - 73)