function varargout = gyration(varargin)
% GYRATION M-file for gyration.fig
%      GYRATION, by itself, creates a new GYRATION or raises the existing
%      singleton*.
%
%      H = GYRATION returns the handle to a new GYRATION or the handle to
%      the existing singleton*.
%
%      GYRATION('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GYRATION.M with the given input arguments.
%
%      GYRATION('Property','Value',...) creates a new GYRATION or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before gyration_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to gyration_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

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help gyration

% Last Modified by GUIDE v2.5 01-Jun-2006 16:42:21

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gyration_OpeningFcn, ...
                   'gui_OutputFcn',  @gyration_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 gyration is made visible.
function gyration_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 gyration (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = gyration_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 pushOK.
function pushOK_Callback(hObject, eventdata, handles)
% hObject    handle to pushOK (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
temp = uigetdir();
set(handles.editPath,'String',temp);
file = dir([temp '\*.jpg']);
file = sortrows(strvcat(file.name));
%[ind name] = picsort(file);
set(handles.listPics,'String',file);
picname = [get(handles.editPath,'String') '\' file(1,:)];
pic = imread(picname);
handles.names = file;
guidata(hObject,handles);
%handles.indexes = ind;
%guidata(hObject,handles);
%handles.pathname = name;
%guidata(hObject,handles);
figure(1);
image(pic);
figure(1);

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


% --- Executes during object creation, after setting all properties.
function editPath_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editPath (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end




% --- Executes on selection change in listPics.
function listPics_Callback(hObject, eventdata, handles)
% hObject    handle to listPics (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listPics contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listPics


% --- Executes during object creation, after setting all properties.
function listPics_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listPics (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


function [ind name] = picsort(file)
a = size(file);
for i = 1:a(1)
    for j = 1:a(2)
        if file(i,j) == '.'
            ind1 = j;
        end
        if file(i,j) == '_'
            ind2 = j;
        end
    end
    temp_n=str2num(file(i,(ind2+1):(ind1-1)));
    temp(i) = temp_n;
    b = file(i,1:ind2);
end
ind = [max(temp) min(temp)];
name = b;



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


% --- Executes during object creation, after setting all properties.
function xinit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xinit (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 xend_Callback(hObject, eventdata, handles)
% hObject    handle to xend (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 xend as text
%        str2double(get(hObject,'String')) returns contents of xend as a double


% --- Executes during object creation, after setting all properties.
function xend_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xend (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 yinit_Callback(hObject, eventdata, handles)
% hObject    handle to yinit (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 yinit as text
%        str2double(get(hObject,'String')) returns contents of yinit as a double


% --- Executes during object creation, after setting all properties.
function yinit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to yinit (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 yend_Callback(hObject, eventdata, handles)
% hObject    handle to yend (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 yend as text
%        str2double(get(hObject,'String')) returns contents of yend as a double


% --- Executes during object creation, after setting all properties.
function yend_CreateFcn(hObject, eventdata, handles)
% hObject    handle to yend (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 pushGyr.
function pushGyr_Callback(hObject, eventdata, handles)
% hObject    handle to pushGyr (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
bead = str2num(get(handles.editBead,'String'));
tres = 150;
path = [get(handles.editPath,'String') '\'];
files = handles.names;
%tempind = handles.indexes;
%ind(1) = tempind(2);
%ind(2) = tempind(1);
coord = [str2num(get(handles.xinit,'String')) str2num(get(handles.xend,'String')) str2num(get(handles.yinit,'String')) str2num(get(handles.yend,'String'))];
info = picconv(bead, tres, path, coord, files);
ratio = str2num(get(handles.editPlate,'String'))/((coord(2)-coord(1))+coord(4)-coord(3))*2;
info(:,3)=info(:,3)*ratio;
info(:,4)=info(:,4)*ratio;
assignin('base','result',info);

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


% --- Executes during object creation, after setting all properties.
function editBead_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editBead (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 answer = picconv(bead,tres,path,coord,files)
x0 = (coord(2)-coord(1))/2 + coord(1);
y0 = (coord(4)-coord(3))/2 + coord(3);
r_x = x0-coord(1)-4;
r_y = y0-coord(3)-4;
counter = 0;
leng = size(files);
time = 0;
for k=1:leng(1)
    sec = str2num(files(k,15:16));
    min = str2num(files(k,13:14));
    hrs = str2num(files(k,11:12));
    time=hrs*3600+min*60+sec;
    counter = counter+1;
    picname = [path files(k,:)];
    pic = imread(picname);
    Progress = [num2str(counter) '/' num2str(leng(1))]
pic3 = rgb2gray(pic);
dim=size(pic3);
for i=1:dim(1)
    for j=1:dim(2)
        temp = ((i-y0)/r_y)^2+((j-x0)/r_x)^2;
        if temp > 1
            pic3(i,j)=0;
        end
    end
end
%figure(counter);
%image(pic3);
flag = 0;
counter2 = 0;
while flag == 0
    bwpic=pic3>(tres+counter2);
    [L num]=bwlabel(bwpic,4);
    if num > bead
        counter2 = counter2+5;
    else
        flag = 1;
    end
end
%bwpic=pic3>tres;
%pic3=pic3.*bwpic;
%figure(counter);
%image(pic3);
%if num > bead
%    error('Too many points found. Reduce sensitivity');
%    answer = [0 0];
%    return;
%end
%figure(2);
%image(pic);
%hold on;
b=0;
for i=1:num
    [r c] = find(L == i);
    a = [r c];
    temp=size(a);
    x=0;
    y=0;
    for j=1:temp(1)
        x = x + a(j,1);
        y = y + a(j,2);
    end
    x = x/temp(1);
    y = y/temp(1);
    b(i,1) = x;
    b(i,2) = y;
%    plot(y,x,'o');
end
bcm = sum(b)/num;
%plot(bcm(2),bcm(1),'x');
temp = 0;
dtemp = 0;
for i=1:num
    temp2 = (b(i,1)-bcm(1))^2 + (b(i,2)-bcm(2))^2;
    temp = temp + temp2;
end
temp = sqrt(temp/num);
dtemp = sqrt((1+1/num)/num);
if bead > num
    dtemp = sqrt(dtemp(1)^2+(sqrt((bead-num)/num)*10)^2);
end
dtemp;
tempans(k,1) = k;
tempans(k,2) = time;
tempans(k,3) = temp;
tempans(k,4) = dtemp;
tempans(k,5) = num;
tempans(k,6) = tres+counter2;

[tempans(counter,1) tempans(counter,2)];
[uint16(tempans(counter,3)) uint16(tempans(counter,4)) uint16(tempans(counter,5))];
end
tempans(:,2)=tempans(:,2)-tempans(1,2);
answer=tempans;



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


% --- Executes during object creation, after setting all properties.
function editPlate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editPlate (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end





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


% --- Executes during object creation, after setting all properties.
function editVar_CreateFcn(hObject, eventdata, handles)
% hObject    handle to editVar (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 pushDispIm.
function pushDispIm_Callback(hObject, eventdata, handles)
% hObject    handle to pushDispIm (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ind = get(handles.listPics,'Value');
names = get(handles.listPics,'String');
name = [get(handles.editPath,'String') '\' names(ind,:)];
image(imread(name));



% --- Executes on button press in pushBeadID.
function pushBeadID_Callback(hObject, eventdata, handles)
% hObject    handle to pushBeadID (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ind = get(handles.listPics,'Value');
names = get(handles.listPics,'String');
picname = [get(handles.editPath,'String') '\' names(ind,:)];
bead = str2num(get(handles.editBead,'String'));
tres = 150;
%path = [get(handles.editPath,'String') '\' handles.pathname];
%tempind = handles.indexes;
%ind(1) = tempind(2);
%ind(2) = tempind(1);
coord = [str2num(get(handles.xinit,'String')) str2num(get(handles.xend,'String')) str2num(get(handles.yinit,'String')) str2num(get(handles.yend,'String'))];
x0 = (coord(2)-coord(1))/2 + coord(1);
y0 = (coord(4)-coord(3))/2 + coord(3);
r_x = x0-coord(1)-4;
r_y = y0-coord(3)-4;
pic = imread(picname);
pic3 = rgb2gray(pic);
dim = size(pic3);
for i=1:dim(1)
    for j=1:dim(2)
        temp = ((i-y0)/r_y)^2+((j-x0)/r_x)^2;
        if temp > 1
            pic3(i,j)=0;
        end
    end
end
image(pic);
hold on;
flag = 0;
counter2 = 0;
while flag == 0
    bwpic=pic3>(tres+counter2);
    [L num]=bwlabel(bwpic,4);
    if num > bead
        counter2 = counter2+5;
    else
        flag = 1;
    end
end
b=0;
for i=1:num
    [r c] = find(L == i);
    a = [r c];
    temp=size(a);
    x=0;
    y=0;
    for j=1:temp(1)
        x = x + a(j,1);
        y = y + a(j,2);
    end
    x = x/temp(1);
    y = y/temp(1);
    b(i,1) = x;
    b(i,2) = y;
    plot(y,x,'o');
end
rectangle('Position',[coord(1) coord(3) coord(2)-coord(1) coord(4)-coord(3)],'Curvature',[1 1],'LineWidth',2,'EdgeColor','green');



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


% --- Executes during object creation, after setting all properties.
function edit10_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit10 (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 pushbutton5.
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)
print;


