% Bc. Michal Kubiš
% Fakulta elektrotechniky a komunikačních technologií na VUT v Brně
% Vedoucí: Ing. Petr Fedra
% 
% 21. 5. 2010
% založeno na algoritmu Petera Kovesiho

function varargout = FingerScan(varargin)


% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @FingerScan_OpeningFcn, ...
                   'gui_OutputFcn',  @FingerScan_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 FingerScan is made visible.
function FingerScan_OpeningFcn(hObject, eventdata, handles, varargin)
% 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 FingerScan (see VARARGIN)

% Choose default command line output for FingerScan
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = FingerScan_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 btnScaningRun.
function btnScaningRun_Callback(hObject, eventdata, handles)
% hObject    handle to btnScaningRun (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.vid = videoinput('winvideo', 1, 'YUY2_640x480');
axes(handles.axsFingerScan);
handles.vidRes = get(handles.vid, 'VideoResolution');
handles.nBands = get(handles.vid, 'NumberOfBands');
handles.hImage = image( zeros(handles.vidRes(2), handles.vidRes(1), handles.nBands) );
preview(handles.vid, handles.hImage);
guidata(hObject, handles);

% --- Executes on button press in btnTakeFinger.
function btnTakeFinger_Callback(hObject, eventdata, handles)
% hObject    handle to btnTakeFinger (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

data = ycbcr2rgb(getsnapshot(handles.vid));
handles.fingerprint = rgb2gray(data);
%fingerprint = imadjust(fingerprint,[0.01 0.99],[]);
%handles.fingerprint = histeq(fingerprint);
handles.fingerprint = imrotate(handles.fingerprint,270,'bilinear');
axes(handles.axsFingerScan);
imshow(handles.fingerprint);
set(handles.panFingerScan,'Title','Sejmutý otisk');
guidata(hObject, handles);


% --- Executes on button press in btnSaveFingerprint.
function btnSaveFingerprint_Callback(hObject, eventdata, handles)
% hObject    handle to btnSaveFingerprint (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%imsave(handles.fingerprint);


% --- Executes on button press in btnAdaptEqual.
function btnAdaptEqual_Callback(hObject, eventdata, handles)
% hObject    handle to btnAdaptEqual (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

handles.I = adapthisteq(handles.fingerprint);
axes(handles.axsFingerScan);
imshow(handles.I);
set(handles.panFingerScan,'Title','Otisk prstu po adaptivní ekvalizaci histogramu');
guidata(hObject, handles);

% --- Executes on button press in btnCrop.
function btnCrop_Callback(hObject, eventdata, handles)
% hObject    handle to btnCrop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

axes(handles.axsFingerScan);
set(handles.panFingerScan,'Title','Oříznout zájmovou část otisku');
handles.I = imcrop(handles.I);

axes(handles.axsFingerScan);
imshow(handles.I);
set(handles.panFingerScan,'Title','Oříznutý otisk');
guidata(hObject, handles);


% --- Executes on button press in btnRidgeExtract.
function btnRidgeExtract_Callback(hObject, eventdata, handles)
% hObject    handle to btnRidgeExtract (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

handles.J = adaptivethreshold(handles.I,8,0,1);
handles.G = immultiply((handles.I)*1.5,uint8(handles.J));

axes(handles.axsFingerScan);
imshow(handles.G);
set(handles.panFingerScan,'Title','Zvýrazněné papilární linie');
guidata(hObject, handles);

function edtSaveName_Callback(hObject, eventdata, handles)
% hObject    handle to edtSaveName (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 edtSaveName as text
%        str2double(get(hObject,'String')) returns contents of edtSaveName as a double

name = get(handles.edtSaveName, 'String');
name_orig = [name,'_orig.tif'];
name_ridges = [name,'_ridges.tif'];
name_ridges_uint8 = [name,'_ridges_uint8.tif'];
name_combine = [name,'_combine.tif'];
imwrite(handles.I,name_orig,'tif');
imwrite(handles.J,name_ridges,'tif');
imwrite(uint8(handles.J),name_ridges_uint8,'tif');
imwrite(handles.G,name_combine,'tif');
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function edtSaveName_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edtSaveName (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

function bw=adaptivethreshold(IM,ws,C,tm)

if (nargin<3)
    error('Zadejte vechny parametry funkce');
elseif (nargin==3)
    tm=0;
elseif (tm~=0 && tm~=1)
    error('Hodnota tm musí být 0 nebo 1.');
end

IM=mat2gray(IM);

if tm==0
    mIM=imfilter(IM,fspecial('average',ws),'replicate');
else
    mIM=medfilt2(IM,[ws ws]);
end
sIM=mIM-IM-C;
bw=im2bw(sIM,0);
bw=imcomplement(bw);
