% 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
Trang 1ĐẠI HỌC QUỐC GIA THÀNH PHỐ HỒ CHÍ MINH
TRƯỜNG ĐẠI HỌC BÁCH KHOA KHOA ĐIỆN – ĐIỆN TỬ
Giáo viên giảng dạy: Đỗ Hồng Tuấn Sinh viên thực hiện:
Trang 2Yêu cầu:
Đề tài 1: Viết chương trình chọn ngưỡng (thresholding) dùng phương pháp chọn ngưỡng toàn cục cơ bản basic
global thresholding) và phương pháp Otsu Viết chương trình phân đoạn ảnh (image segmentation) dùng kết quả lấy ngưỡng ở trên So sánh kết quả của 2 phương pháp trên với 2 dạng ảnh: ảnh có nhiễu Gauss và ảnh không có nhiễu
Các bước thực hiện của 2 phương pháp:
Phương pháp chọn ngưỡng toàn cục cơ bản:
1 Chọn một giá trị khởi tạo cho T (nên chọn giá trị mang tính công thức,
ví dụ T = (min + max) / 2, T = giá trị trung bình, tránh dùng các giá trị mang tính định lượng thiết lập cứng)
2 Phân hoạch ảnh sử dụng T kết quả của bước này sẽ tạo ra 2 nhóm điểm ảnh: G1 chứa tất cả các điểm ảnh với giá trị (intensity) > T và G2 chứa các điểm ảnh với giá trị (intensity) <= T
3 Tính trung bình m1 và m2 của các điểm ảnh thuộc G1 và G2
4 Tính lại T dựa vào m1 và m2: T = (m1 + m2) / 2
5 Lặp lại bước 2 đến 4 cho tới khi nào giá trị chênh lệch giữa T củ và T mới là không đáng kể (nhỏ hơn một giá trị cho trước To) To thường được sử dụng là sai số từ các phép tính toán trong quá trình xử lý Trong trường hợp này To được chọn mặc định là 0, giá trị này có thể thay đổi
Phương phá Otsu:
Trước tiên, sau khi thống kê mức xám trên ảnh ban đầu, chúng ta sẽ nhận được một đồ thị biểu diễn mức xám có hai đỉnh, một đỉnh biểu diễn cho những vùng là đối tượng, đỉnh còn lại biểu diễn cho những vùng là nền của ảnh Theo Otsu, ngưỡng k* tốt nhất được chọn là giá trị mà tại đó nó làm cho sự chênh lệch 2
b
giữa hai đoạn trên đồ thị đạt cực đại Giá trị 2
b
được định nghĩa như sau:
2 2 2 2 1 1 2
) (
)
b a m m a m m
Thay mt a1m1 a2m2, a1 a2 1 , ta được:
2 2 1 2 1 2
) ( m m a
a
Trong đó m1 và m2 biểu diễn giá trị trung bình tương ứng với đoạn 1 và đoạn 2 , a1 và a2 là tần suất xuất hiện của m1 và m2 Tỷ lệ aj của diện tích đoạn j với tổng diện tích được tính như sau:
, 2 , 1
j p a
j
C i i
Trong đó pi là thương của số lần xuất hiện của mức xám thứ i và tổng số lần xuất hiện của tất cả các
1
0
I
i i
p
Trang 3
Với I biểu diễn tổng số những mức xám Thông thường, đối với ảnh văn bản, I có giá trị là 256
1
C ( C2) biểu diễn tập hợp tất cả những điểm có giá trị nhỏ hơn hoặc bằng (lớn hơn) ngưỡng k Chú ý rằng, giá trị trung bình mj được tính như sau:
j
C i
j i
m , 1 , 2 : mức xám trung bình trên đoạn j
b
Trang 4Để thực hiện đề tài này nhóm em sẽ sử dụng Matlab
Đầu tiên nhóm em xây dựng giao diện cho chương trình như sau:
Trang 5Ví dụ sau đây sẽ thực hiện phân đoạn ảnh dấu vân tay:
1).Từ màn hình giao diện ấn nút chọn ảnh:
2).Trong thư mục images chọn ảnh “fingerprint.tif”
3).Sau khi ấn nút Open ta sẽ thấy trên giao diện xuất hiện ảnh vừa chọn và histogram của nó 4).Tiếp theo ta ấn nút Tìm Ngưỡng và nút Phân đoạn ảnh, trên giao điện sẽ xuất hiện giá trị
ngưỡng T và kết quả phân đoạn ảnh theo ngưỡng T vừa tìm
Trang 6Trên màn hình ta thấy giá trị ngưỡng T tìm được theo phương pháp chọn ngưỡng toàn cục cơ bản là T
= 125 , và theo phương pháp Otsu là T = 126
Trang 7Do ảnh ví dụ ở đây không có nhiễu Gauss nên giả sử ta muốn tạo nhiễu Gauss cho ảnh ta thực hiện
bằng cách ấn vào nút Tạo nhiễu Gauss, các thông số của nhiễu Gauss ta có thể thay đổi hoặc để
nguyên theo mặc định:
Ảnh không có nhiễu :
Ảnh có nhiễu Gauss :
Tương tự, ta lập lại các bước như trên để được kết quả phân đoạn ảnh đối với ảnh có nhiễu Gauss
Ở ảnh có nhiễu Gauss ta thấy giá trị ngưỡng T tìm được ở hai phương pháp (phân ngưỡng toàn cục và Otsu) đề cho kết quả là T = 126
Trang 8Kết quả::
Trang 9Kết quả phân đoạn ảnh với nhiều ảnh khác nhau:
Ảnh được chọn “blobs.tif”
ngưỡng toàn cục
Kết quả theo phương pháp
Otsu
Ảnh có nhiễu
Trang 10Ảnh được chọn “tiger.tif”
Ảnh có nhiễu
Trang 11Ảnh được chọn “prak1.tif”
Otsu
Ảnh có nhiễu
Trang 12Ảnh được chọn “characters.tif”
Otsu
Ảnh có nhiễu
Trang 13Nhận xét:
Qua khảo sát các ảnh ta thấy đối với các ảnh bị ảnh hưởng bởi nhiều Gauss nếu đối tượng có tham xám nổ bật hơn so với nền ảnh thì cả hai phương pháp hoàn toàn có thể loại bỏ được nhiễu Gauss ra khỏi đối tượng
Tuy nhiên đối với ảnh bị nhiễu có độ tương phản thấp thì phương pháp Otsu phân đoạn ảnh cho kết quả tốt hơn
Phu lục Code:
Chương trình phân đoạn ảnh “segmentation.m”
s = size(image);
I = image(:);
for i=1:length(I)
if I(i) > T
I(i) = ;
else
I(i) = ;
end end
imshow = reshape(I,s);
Chương trình tìm ngưỡng dựa trên kĩ thuật Otsu “otsu.m”
function k = otsu(img)
I = im2uint8(img(:));
L = 256;
counts = imhist(I,L);
p = counts / sum(counts);
P1 = cumsum(p);
m = cumsum(p .* (1:L)');
m_g = m( end );
sigma_b_squared = (m_g * P1 - m).^2 ./ (P1 .* (1 - P1));
maxval = max(sigma_b_squared);
k = find(sigma_b_squared == maxval);
Chương trình tìm ngưỡng toàn cục “globalthreshold”
T = 0.5 * (max(img(:)) - min(img(:)));
S=size(img);
numelem=S(1)*S(2);
ImgNext = double(img);
Tnext = -T0; while abs(T-Tnext) > T0 tmp = ImgNext > T;
zeros1 = sum(tmp(:));
zeros2 = numelem - zeros1;
G1 = tmp.*ImgNext;
G2 = (~tmp).*ImgNext;
u1 = sum(G1(:)) / zeros1;
u2 = sum(G2(:)) / zeros2;
Tnext = T;
T = 0.5 * (u1+u2);
end
threshold = round(T);
Trang 14Chương trình cho phần gia diện “DeTai1.m”
% DETAI1 M-file for DeTai1.fig
% DETAI1, by itself, creates a new DETAI1 or raises the existing
% singleton*
%
% H = DETAI1 returns the handle to a new DETAI1 or the handle to
% the existing singleton*
%
% DETAI1('CALLBACK',hObject,eventData,handles, ) calls the local
% function named CALLBACK in DETAI1.M with the given input arguments
%
% DETAI1('Property','Value', ) creates a new DETAI1 or raises the
% existing singleton* Starting from the left, property value pairs are
% applied to the GUI before DeTai1_OpeningFcn gets called An
% unrecognized property name or invalid value makes property application
% stop All inputs are passed to DeTai1_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 DeTai1
% Last Modified by GUIDE v2.5 02-Dec-2012 11:59:02
% Begin initialization code - DO NOT EDIT
gui_Singleton = ;
gui_State = struct('gui_Name', mfilename,
'gui_Singleton', gui_Singleton,
'gui_OpeningFcn', @DeTai1_OpeningFcn,
'gui_OutputFcn', @DeTai1_OutputFcn,
'gui_LayoutFcn', [] ,
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% - Executes just before DeTai1 is made visible
% 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 DeTai1 (see VARARGIN)
% Choose default command line output for DeTai1
handles.output = hObject;
Trang 15% Update handles structure
guidata(hObject, handles);
% UIWAIT makes DeTai1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% - Outputs from this function are returned to the command line
% 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 Chon_Anh
% hObject handle to Chon_Anh (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename,pathname] = uigetfile({'*.tif;*.bmp;*.gif;*.jpg'},'Select Image');
I = imread([pathname,filename]);
s = size(size(I));
if s(2) ==
I = rgb2gray(I);
end
gauss_set = ;
axes(handles.AnhGoc);
imshow(I);
axes(handles.histogram);
imhist(I);
assignin('base','I',I);
% hObject handle to T (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of T as text
% str2double(get(hObject,'String')) returns contents of T as a double
% - Executes during object creation, after setting all properties
% hObject handle to T (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows
% See ISPC and COMPUTER
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
Trang 16function T0_Callback(hObject, eventdata, handles)
% hObject handle to T0 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of T0 as text
% str2double(get(hObject,'String')) returns contents of T0 as a double
% - Executes during object creation, after setting all properties
% hObject handle to T0 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows
% See ISPC and COMPUTER
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% - Executes on button press in TinhNguongToanCuc
% hObject handle to TinhNguongToanCuc (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
T0 = str2num(get(handles.T0,'String'));
if gauss_set ==
T = globalthreshold(I_noise,T0);
set(handles.T,'String',T);
assignin('base','T_noise',T);
else
T = globalthreshold(I,T0);
set(handles.T,'String',T);
assignin('base','T',T);
end
% hObject handle to T (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of T as text
% str2double(get(hObject,'String')) returns contents of T as a double
% - Executes during object creation, after setting all properties
% hObject handle to T (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
Trang 17% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% - Executes on button press in PhanDoan1
% hObject handle to PhanDoan1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if gauss_set == ;
I1 = segmentation(I_noise,T);
axes(handles.Toancuc_gauss);
imshow(I1,[]);
else
I1 = segmentation(I,T);
axes(handles.Toancuc);
imshow(I1,[]);
end
% - Executes on button press in Gauss
% hObject handle to Gauss (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
m = str2num(get(handles.mean,'String'));
v = str2num(get(handles.variance,'String'));
I_noise = imnoise(I,'gaussian',m,v);
axes(handles.AnhGoc);
imshow(I_noise);
axes(handles.histogram);
imhist(I_noise);
assignin('base','I_noise',I_noise);
gauss_set = ;
set(handles.text8,'String','Anh co nhieu Gauss');
set(handles.text5,'String','Histogram co nhieu Gauss');
% hObject handle to mean (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of mean as text
% str2double(get(hObject,'String')) returns contents of mean as a double
% - Executes during object creation, after setting all properties
Trang 18% hObject handle to mean (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows
% See ISPC and COMPUTER
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% hObject handle to variance (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of variance as text
% str2double(get(hObject,'String')) returns contents of variance as a double
% - Executes during object creation, after setting all properties
% hObject handle to variance (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows
% See ISPC and COMPUTER
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% - Executes on button press in exit
% hObject handle to exit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close
% - Executes on button press in Ostu_tim
% hObject handle to Ostu_tim (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if gauss_set ==
k_otsu = otsu(I_noise);
set(handles.k_otsu,'String',k_otsu);
assignin('base','k_noise',k_otsu);
else
k_otsu = otsu(I);
set(handles.k_otsu,'String',k_otsu);
assignin('base','k',k_otsu);
Trang 19% - Executes on button press in Otsu_phandoan
% hObject handle to Otsu_phandoan (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if gauss_set ==
I2 = segmentation(I_noise,k_otsu);
axes(handles.Otsu_gauss);
imshow(I2,[]);
else
I2 = segmentation(I,k_otsu);
axes(handles.Otsu);
imshow(I2,[]);
end
% hObject handle to k_otsu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of k_otsu as text
% str2double(get(hObject,'String')) returns contents of k_otsu as a double
% - Executes during object creation, after setting all properties
% hObject handle to k_otsu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows
% See ISPC and COMPUTER
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% - If Enable == 'on', executes on mouse press in 5 pixel border
% - Otherwise, executes on mouse press in 5 pixel border or over T
% hObject handle to T (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)