function varargout = image2spreadsheet(varargin)
% IMAGE2SPREADSHEET allows the user to browse through several pictures in
% one folder and collect pixel coordinates from each picture.
% Use the arrows to navigate through the pictures and press 'space' to
% collect coordinates.
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @image2spreadsheet_OpeningFcn, ...
'gui_OutputFcn', @image2spreadsheet_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 image2spreadsheet is made visible.
function image2spreadsheet_OpeningFcn(hObject, eventdata, handles, varargin)
delete actxlicense.m
%Get several properties and set them to desire
ActSheet = get(handles.activex1,'ActiveSheet'); % Get handle from ActiveSheet
SheetProtection = get(ActSheet,'Protection'); set(SheetProtection,'Enabled',0); % Disable protection
cells = get(ActSheet,'Cells'); set(cells,'RowHeight',15,'ColumnWidth',5,'HorizontalAlignment',-4108,'VerticalAlignment',-4108,'locked',0); cells.ClearContents %Adjust cell size
CellsBorders = get(cells,'borders'); set(CellsBorders,'LineStyle',1); % Adjust borders
% Set 'header' cells
Select(Range(ActSheet,'A1:A2')); % Get cells A1:A2
selection = get(handles.activex1,'Selection'); set(selection,'HorizontalAlignment',-4131,'Value',{'X';'Y'},'locked',1); % Write X,Y and lock cells
CellsFont = get(selection,'Font'); set(CellsFont,'Bold',1); % Set Bold font type
CellInterior = get(selection,'Interior'); set(CellInterior,'ColorIndex',15); % Set color
set(SheetProtection,'Enabled',1); % Enable protection to protect 'header' cells
Select(Range(ActSheet,'B1')); % Select first cell
handles.output = hObject;
guidata(hObject, handles);
%% --- Outputs from this function are returned to the command line.
function varargout = image2spreadsheet_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
%% --- Executes on button press in LoadDir.
function LoadDir_Callback(hObject, eventdata, handles)
% Get a directory with jpg images
global ImageDir %Define default directory
PathName = uigetdir(ImageDir,'Select directory with images'); % Get directory
if ~PathName, return, end % If 'Cancel' during selection: return
ImageDir = PathName; % Update default directory
[stat, handles.files] = fileattrib([ImageDir '\*.jpg']); %Get all the jpg files
if ~stat % If there are no jpg files: return
set(handles.text1,'string','No jpg files found in this directory');
return
end
set(handles.text1,'string',ImageDir); % Show the directory
handles.files([handles.files.hidden]) = []; % Eliminate hidden files
fig = imread(handles.files(1).Name); % Get first image
handles.CurrentFigure = 1; % CurrentFigure = 1
image(fig,'Parent',handles.axes1); % Display image
set(gca,'xticklabel',[]); set(gca,'yticklabel',[]); % Take out axis
guidata(handles.figure1, handles);
%% --- Executes on key press with focus on figure1 and none of its controls.
function figure1_KeyPressFcn(hObject, eventdata, handles)
% Manages interaction between figure and activex control
CurChar = double(get(hObject,'CurrentCharacter')); % Get which key was pressed
if isempty(CurChar) || isempty(get(handles.text1,'string')) || ...
strcmp(get(handles.text1,'string'),'No jpg files found in this directory')
return;
end
ActCell = get(handles.activex1,'ActiveCell'); % Get active cell
ActColumn = get(ActCell,'Column'); % Get active col
if ActColumn == 1
errordlg('Select column where to write output values','Error');
return;
end
switch CurChar % Test which key was pressed
case 29 % right arrow: get next figure
handles.CurrentFigure = handles.CurrentFigure + 1; % update CurrentFigure
if handles.CurrentFigure >= length(handles.files) % if it's the last figure, start from beginning
handles.CurrentFigure = 1;
end
case 28 % left arrow: get previous figure
handles.CurrentFigure = handles.CurrentFigure - 1; % update CurrentFigure
if handles.CurrentFigure == 0 % If it's first figure, go to the last one
handles.CurrentFigure = length(handles.files);
end
case 32 % space: start ginput and add coordinates in table
ActSheet = get(handles.activex1,'ActiveSheet'); % get active sheet
ActCell = get(handles.activex1,'ActiveCell'); % get active cell
ActColumn= get(ActCell,'Column'); % get active row
[x y] = ginput(1); % get coordinates
if isempty(x) % return if no coordinates
return
end
CellStart = nn2an(1,ActColumn); % calculate top cell
CellEnd = nn2an(2,ActColumn); % calculate bottom cell
Select(Range(ActSheet,[CellStart ':' CellEnd])); % Select cells
selection = get(handles.activex1,'Selection'); % get selection
set(selection,'Value',{x;y}); % write values in cells
CellNext = nn2an(1,ActColumn+1); % calculate next cell
Select(Range(ActSheet,CellNext)); % select next cell
return;
otherwise
return;
end
% Update picture
file = handles.files(handles.CurrentFigure).Name; % get the file name
fig = imread(file); image(fig); % read and plot image
set(gca,'xticklabel',[]); set(gca,'yticklabel',[]); % clear axis
guidata(handles.figure1, handles);
function cr = nn2an(r,c)
%% Numerical coordinates to Cell names
t = [floor((c - 1)/26) + 64 rem(c - 1, 26) + 65];
if(t(1)<65), t(1) = []; end
cr = [char(t) num2str(r)];