function varargout = NXTScannerViewer(varargin)
% NXTSCANNERVIEWER M-file for NXTScannerViewer.fig
% NXTSCANNERVIEWER, by itself, creates a new NXTSCANNERVIEWER or raises the existing
% singleton*.
%
% H = NXTSCANNERVIEWER returns the handle to a new NXTSCANNERVIEWER or the handle to
% the existing singleton*.
%
% NXTSCANNERVIEWER('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in NXTSCANNERVIEWER.M with the given input arguments.
%
% NXTSCANNERVIEWER('Property','Value',...) creates a new NXTSCANNERVIEWER or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before NXTScannerViewer_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to NXTScannerViewer_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 NXTScannerViewer
% Last Modified by GUIDE v2.5 28-Nov-2008 15:34:42
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @NXTScannerViewer_OpeningFcn, ...
'gui_OutputFcn', @NXTScannerViewer_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 NXTScannerViewer is made visible.
function NXTScannerViewer_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 NXTScannerViewer (see VARARGIN)
% Choose default command line output for NXTScannerViewer
handles.output = hObject;
% This sets up the initial plot - only do when we are invisible
% so window can get raised using NXTScannerViewer.
if strcmp(get(hObject,'Visible'),'off')
set(handles.text1, 'Visible', 'on')
set(handles.text1, 'String', 'Push Scan for Scanning...')
set(handles.pushbutton1, 'Enable', 'on')
end
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes NXTScannerViewer wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = NXTScannerViewer_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 pushbutton1.(SCAN)
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% get nxtusb object
nuObj = nxtusb;
if nuObj.CurrentState ~= 3
uiwait(warndlg('Cannot find NXT. Check the NXT-USB connection.', [get(handles.figure1,'Name') '...']))
return;
end
% scanData = handles.scanData;
scanData = uint8(zeros(255, 255));
if nuObj.CurrentState == 3
% Scan Process
set(hObject, 'Enable', 'off')
set(handles.pushbutton2, 'Enable', 'off')
set(handles.pushbutton3, 'Enable', 'off')
set(handles.pushbutton4, 'Enable', 'off')
% set(handles.pushbutton5, 'Enable', 'off')
uiwait(msgbox('Push OK just to Start Scanning', [get(handles.figure1,'Name') '...'], 'modal'))
pause(0.06)
set(handles.text1, 'Visible', 'on')
set(handles.text1, 'String', ['Connected to [' nuObj.Name ']...'])
set(handles.text2, 'Visible', 'on')
set(handles.text3, 'Visible', 'on')
set(handles.slider1, 'Visible', 'off')
set(handles.text4, 'Visible', 'off')
set(handles.edit1, 'Visible', 'off')
set(handles.slider2, 'Visible', 'off')
set(handles.text5, 'Visible', 'off')
set(handles.edit2, 'Visible', 'off')
% main function <<nxtscanner program>>
row = 1;
data1 = [];
data2 = [];
holdFlag = 0;
breakFlag = 0;
storeFlag = 0;
axes(handles.axes1)
%imObj = imshow(uint8(255*ones(256, 549)));
t0 = clock;
while ~breakFlag
tnow= etime(clock, t0);
rowStr = sprintf('%d row(s)\n', row);
% format MM:SS
tnowStr = sprintf('%d:%d', floor(tnow / 60), floor(rem(tnow, 60)));
set(handles.text3, 'String', [rowStr, tnowStr])
% pause(0.06)
[len, buf] = read(nuObj, 'uint16', 32);
if len == 64 && length(buf) == 32
% disp(['holdFlag: ', num2str(holdFlag)])
% ret.buf
% ret.directFlag
% ret.elpFlag
[ret, holdFlag, breakFlag] = extractData(buf, holdFlag);
if ~isempty(ret.buf)
if ~ret.directFlag
% disp FROM
% FROM
data2 = [ret.buf, data2];
% data stored
storeFlag = 1;
if ret.elpFlag
data2 = uint8(747 - data2);
% disp(['data2len = ', num2str(length(data2))])
scanData(row, 1:length(data2)) = data2;
data2 = [];
storeFlag = 0;
imshow(scanData), drawnow
row = row + 2;
end
% disp(['FROM datanum increase: ', num2str(length(buf))])
else
% TO
% disp TO
data1 = [data1, ret.buf];
% data stored
storeFlag = 1;
if ret.elpFlag
data1 = uint8(747 - data1);
% disp(['data1len = ', num2str(length(data1))])
scanData(row, 1:length(data1)) = data1;
data1 = [];
storeFlag = 0;
imshow(scanData), drawnow
row = row + 2;
end
% disp(['TO datanum increase: ', num2str(length(buf))])
end
% disp(['breakFlag = ', num2str(breakFlag)])
%%%%%%%%%%disp(['row = ', num2str(row)])
if row > 257
disp('row > 257 so ABORT!')
breakFlag = 1;
end
else
% The data was empty
continue;
end
else
delete(nuObj);
clear nuObj
uiwait(errordlg('Data read error. Close NXT-USB interface', ...
[get(handles.figure1,'Name') '...']))
return;
end
end
% update last data increment the 'row' pointer to the scan data
if storeFlag
disp('consider last data')
if ~isempty(data1)
data1 = uint8(747 - data1);
scanData(row, 1:length(data1)) = data1;
end
if ~isempty(data2)
data2 = uint8(747 - data2);
scanData(row, 1:length(data2)) = data2;
end
end
% truncate unnecessary lines
scanData = scanData(1:row, :);
set(handles.text1, 'String', ['Scanning end [' nuObj.Name ']...'])
% apply brightness/contrast level to show on Axes
brLvl = get(handles.slider1,'Value');
cnLvl = get(handles.slider2, 'Value');
scanData = scanData + brLvl;
scanData = scanData * ((100 + cnLvl) / 100);
imshow(scanData)
set(handles.slider1, 'Visible', 'on')
set(handles.text4, 'Visible', 'on')
set(handles.edit1, 'Visible', 'on')
set(handles.slider2, 'Visible', 'on')
set(handles.text5, 'Visible', 'on')
set(handles.edit2, 'Visible', 'on')
else
uiwait(errordlg('Cannot open NXT-USB interface', [get(handles.figure1,'Name') '...']))
end
delete(nuObj);
clear nuObj
handles.scanData = scanData;
% Update handles structure
guidata(hObject, handles);
set(hObject, 'Enable', 'on')
set(handles.pushbutton2, 'Enable', 'on')
set(handles.pushbutton3, 'Enable', 'on')
set(handles.pushbutton4, 'Enable', 'on')
% set(handles.pushbutton5, 'Enable', 'on')
% --- Extracting data(a helper function for pushbutton1_Callback
function [ret, holdFlag, breakFlag] = extractData(buf, holdFlag)
% ret.buf
% ret.directFlag
% ret.elpFlag
% disp('###### extractData START ######')
a = buf(1);
dataNum = buf(2);
validFlag = bitget(a, 1);
directFlag = bitget(a, 2);
endFlag = bitget(a, 3);
startFlag = bitget(a, 4);
elpFlag = bitget(a, 5);
breakFlag = 0;
if validFlag
if startFlag
holdFlag = 1;
end
if endFlag
breakFlag = 1;
end
else
buf = [];
return;
end
if holdFlag
if directFlag
buf = buf(3:dataNum+2);
% disp('forward data!!')
else
% reverse the element order
buf = buf(dataNum + 2:-1:3);
% disp('backward data!!')
end
else
buf = [];
end
ret.buf = buf;
ret.directFlag = directFlag;
ret.elpFlag = elpFlag;
% disp('###### extractData END ######')
% --- Executes on button press in pushbutton2.(LOAD)
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (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('*.mat', 'Select a MAT-file');
if ~isequal(filename, 0) && ~isequal(pathname, 0)
file = fullfile(pathname, filename);
S = load(file, 'scanData');
if isfield(S, 'scanData')
scanData = S.scanData;
handles.scanData = scanData;
% apply brightness/contrast level to show on Axes
brLvl = get(handles.slider1,'Value');
cnLvl = get(handles.slider2, 'Value');
scanData = scanData + brLvl;
scanData = scanData * ((100 + cnLvl) / 100);
axes(handles.axes1)
imshow(scanData)
set(handles.slider1, 'Visible', 'on')
set(handles.edit1, 'Visible', 'on')
set(handles.text4, 'Visible', 'on')
set(handles.slider2, 'Visible', 'on')
set(handles.edit2, 'Visible', 'on')
set(handles.text5, 'Visible', 'on')
% Update handles structure
guidata(hObject, handles);
else
uiwait(errordlg('No data loaded from the MAT-file.', [get(handles.figure1,'Name') '...']))
end
end
% --- Executes on button press in pushbutton3.(SAVE)
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~isfield(handles, 'scanData')
uiwait(warndlg('Scanned data not found', [get(handles.figure1,'Name') '...']))
return;
end
scanData = handles.scanData;
if ~isnumeric(scanData)
uiwait(warndlg('Scanned data must be numerical', [get(handles.figure1,'Name') '...']))
return;
end
% apply brightness/contrast level to save as a MAT-file
brLvl = get(handles.slider1,'Value');
cnLvl = get(handles.slider2, 'Value');
scanData = scanData + brLvl;
scanData = scanData * ((100 + cnLvl) / 100);
uisave('scanData', 'scanData');
% --- Executes on button press in pushbutton4.(IP)
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if ~isfield(handles, 'scanData')
uiwait(warndlg('Scanned data not found', [get(handles.figure1,'Name') '...']))
return;
end
scanData = handles.scanData;
if ~isnumeric(scanData)
uiwait(warndlg('Scanned data must be numerical', [get(handles.figure1,'Name') '...']))
return;
end
% apply brightness/contrast level to save as a MAT-file
brLvl = get(handles.slider1,'Value');
cnLvl = get(handles.slider2, 'Value');
scanData = scanData + brLvl;
scanData = scanData * ((100 + cnLvl) / 100);
%%%%%%%%%%%%%%%%%% Image Processing algorithm START %%%%%%%%%%%%%%%%%%
wbh = waitbar(0,'Please wait...');
% get image size
[xs,ys] = size(scanData);
% get max value
mx = max(scanData(1:2:end));
% get mim value
mn = min(scanData(1:2:end));
waitbar(10 / 100, wbh)
% adjust contrast and gamma control
scanData = imadjust(scanData,[double(mn)/255 double(mx)/255],[0 1],1);
waitbar(20 / 100, wbh)
% 2 times downsizing
I1 = scanData(1:2:end,:);
% resize(2 times up sampling)
I1 = imresize(I1,[xs,ys],'lanczos3');
scanData2 = I1;
waitbar(30 / 100, wbh)
% create kernel for tapering
k = ones(3,6);
k = k/(size(k,1)*size(k,2));
waitbar(40 / 100, wbh)
% tapering processing
scanData2 = edgetaper(scanData2,k);
waitbar(50 / 100, wbh)
% edge processing
a = edge(scanData2,'sobel',0.10); % GbW
waitbar(60 / 100, wbh)
% do not process deconvolution for outer frame
se = strel('rect',[2 5]);
waitbar(70 / 100, wbh)
% In addition, do not process deconvolution for the extreme edge
b = imdilate(a,se);
waitbar(80 / 100, wbh)
b = ~b;
b([1:3 end-[0:2]],:) = 0;
b(:,[1:3 end-[0:2]]) = 0;
waitbar(90 / 100, wbh)
% blind deconvolution processing
[J,PSF] = deconvblind(scanData2,k,22,uint8(0),double(b),uint8(255));
waitbar(100 / 100, wbh)
close(wbh)
figure, imshow(scanData) % Figure.1
figure, imshow(scanData2) % Figure.2
figure, imshow(J) % Figure.3
%%%%%%%%%%%%%%%%%% Image Processing algorithm END %%%%%%%%%%%%%%%%%%
% Update handles structure
% guidata(hObject, handles);
% --- Executes on button press in pushbutton5.(EXIT)
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'], ...
['Close ' get(handles.figure1,'Name') '...'], ...
'Yes','No','Yes');
if strcmp(selection,'No')
return;
end
delete(handles.figure1)
% close all figures
close all
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% The value of slider2 is needed to calculate scanData even at the slider1 callback
cnLvl = get(handles.slider2, 'Value');
% Update the value to edit1
brLvl = get(hObject, 'Value');
set(handles.edit1, 'String', num2str(round(brLvl)))
if ~isfield(handles, 'scanData')
uiwait(warndlg('Scanned data not found', [get(handles.figure1,'Name') '...']))
return;
end
scanData = handles.scanData;
if ~isnumeric(scanData)
uiwait(warndlg('Scanned data must be numerical', [get(handles.figure1,'Name') '...']))
return;
end
% apply brightness/contrast level to show on Axes
scanData = scanData + brLvl;
scanData = scanData * ((100 + cnLvl) / 100);
axes(handles.axes1)
imshow(scanData)
% Update handles structure
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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 slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% The value of slider1 is needed to calculate scanData even at the slider2 callback
brLvl = get(handles.slider1,'Value');
% Update the value to edit2
cnLvl = get(hObject,'Value');
set(handles.edit2, 'String', num2str(round(cnLvl)))
if ~isfield(handles, 'scanData')
uiwait(warndlg('Scanned data not found', [get(handles.figure1,'Name') '...']))
return;
end
scanData = handles.scanData;
if ~isnumeric(scanData)
uiwait(warndlg('Scanned data must be numerical', [get(handles.figure1,'Name') '...']))
return;
end
% apply brightness/contrast level to show on Axes
scanData = scanData + brLvl;
scanData = scanData * ((100 + cnLvl) / 100);
axes(handles.axes1)
imshow(scanData)
% Update handles structure
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (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 edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (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 when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'], ...
['Close ' get(handles.figure1,'Name') '...'], ...
'Yes','No','Yes');
if strcmp(selection,'No')
return;
end
delete(hObject);
% close all figures
close all