0% found this document useful (0 votes)
2 views17 pages

Vision-Based Displacement Measurement System

The document outlines the MATLAB code for a vision-based displacement measurement system, including GUI functions and callbacks for user interactions. It provides instructions for setting up video input, defining world coordinates, and managing regions of interest (ROI) for measurement. Additionally, it includes functionality for automatic ROI designation and data processing for displacement measurement.

Uploaded by

namhoahoc1012
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

Vision-Based Displacement Measurement System

The document outlines the MATLAB code for a vision-based displacement measurement system, including GUI functions and callbacks for user interactions. It provides instructions for setting up video input, defining world coordinates, and managing regions of interest (ROI) for measurement. Additionally, it includes functionality for automatic ROI designation and data processing for displacement measurement.

Uploaded by

namhoahoc1012
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

%% Notice

% Contact information: Junhwa Lee ([Link]@[Link])


% This code is intended to provide a concept of the vision-based
displacement measurement system.
% Please follow the instruction shown in the youtube below.
% [Link]

%% --- GUI functions.


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


VisionDisplacementMethod

% Last Modified by GUIDE v2.5 21-Jan-2019 12:46:22

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @VisionDisplacementMethod_OpeningFcn, ...
'gui_OutputFcn', @VisionDisplacementMethod_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 VisionDisplacementMethod is made visible.


function VisionDisplacementMethod_OpeningFcn(hObject, ~, 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 VisionDisplacementMethod (see
VARARGIN)

% Choose default command line output for VisionDisplacementMethod


[Link] = hObject;

% Update handles structure


guidata(hObject, handles);

initialize_gui(hObject, handles);

% UIWAIT makes VisionDisplacementMethod wait for user response (see


UIRESUME)
% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = VisionDisplacementMethod_OutputFcn(~, ~, 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} = [Link];

%% pushbutton 1,2,3,4,5
function pushbutton1_Callback(hObject, ~, 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)

set([Link], 'String', 'Waiting for Create');


% Create&Set

% create videoinput object


if(isfield(handles, 'video'))
stop([Link]);
delete([Link]);
delete([Link]);
delete([Link]);
delete([Link]);
initialize_gui(hObject, handles);
handles=rmfield(handles,'video');
handles=rmfield(handles,'Xline');
handles=rmfield(handles,'Yline');
handles=rmfield(handles,'Aline');
if(isfield(handles, 'rect'))
delete([Link].hg1);
delete([Link].hg2);
delete([Link].hg3);
delete([Link].hg4);
delete([Link].P(1));
delete([Link].P(2));
delete([Link].P(3));
delete([Link].P(4));
delete([Link]);
handles=rmfield(handles,'rect');
handles=rmfield(handles,'Centroid');
end
end

% Define world coordinate in target


prompt = {'X1:','Y1:','X2:','Y2:','X3:','Y3:','X4:','Y4:'};
title = 'World Coordinate (mm unit)';
def = {'-35','35','35','35','-35','-35','35','-35'};
worldPoints = inputdlg(prompt,title,1,def,'on');
wp = reshape(str2double(worldPoints),2,[]);
worldAngle = atan2d(wp(1,:),wp(2,:));
set(handles.text_wc, 'String', ['World Coordinate : '...
'(' num2str(wp(1,1)) ',' num2str(wp(2,1))
'), '...
'(' num2str(wp(1,2)) ',' num2str(wp(2,2))
'), '...
'(' num2str(wp(1,3)) ',' num2str(wp(2,3))
'), '...
'(' num2str(wp(1,4)) ',' num2str(wp(2,4))
')'...
]);

% Select video format


hws = imaqhwinfo ;
hw = imaqhwinfo([Link]{end},1) ;
VideoFormat = [Link];
select = listdlg('ListString',VideoFormat,...
'SelectionMode','single',...
'PromptString','List of Video Formats available',...
'Name','Select Video Format');
set(handles.text_vf, 'String', ['Video Format : ' VideoFormat{select}]);
% set videoinput object
video = videoinput('winvideo',1,VideoFormat{select});
set(video,'FramesPerTrigger',inf);
set(video,'ReturnedColorSpace','grayscale');
set(video,'LoggingMode','memory');
triggerconfig(video,'immediate');
set(video,'FramesAcquiredFcnCount', 3);
set(video,'FramesAcquiredFcn', {@FramesAcquiredFcn,hObject});

% Preview UPDATE
vidRes = get(video, 'VideoResolution');
vidWin = image(zeros(vidRes(2), vidRes(1)),'Parent',[Link]);
preview(video,vidWin);

% XDataPlot UPDATE
t = 0:1/29.97:1/29.97*100-1/29.97;
x = zeros(1,100);
Xline = line(t,x,'Parent',[Link],'Color','r');
Yline = line(t,x,'Parent',[Link],'Color','b');
Aline = line(t,x,'Parent',[Link],'Color','k');

% Save Video Object


[Link] = video;
[Link] = Xline;
[Link] = Yline;
[Link] = Aline;
[Link] = wp';
[Link] = worldAngle;
guidata(hObject,handles)

set([Link], 'String', 'Create has been done');

function pushbutton2_Callback(hObject, ~, 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)

set([Link], 'String', 'Waiting for Prestep');

% Define world coordinate in target


prompt = {'Do you want automatical ROI designation? y or n'};
title = 'Select Prestep';
def = {'y'};
sel_roi = inputdlg(prompt,title,1,def,'on');
sel_roi = cell2mat(sel_roi);

if(isfield(handles,'rect'))
delete([Link].hg1);
delete([Link].hg2);
delete([Link].hg3);
delete([Link].hg4);
delete([Link].P(1));
delete([Link].P(2));
delete([Link].P(3));
delete([Link].P(4));
handles=rmfield(handles,'rect');
end

% setting ROI
hg = hggroup('Parent',[Link]);
if(sel_roi=='y')
% find ROI automatically
f = getsnapshot([Link]);
threshold = graythresh(f);
bw = im2bw(f,threshold);
F = regionprops(bw);
[~,idx] = sort([[Link]],'descend');
idx2 = [F(idx).Area]>1000;
F = (F(idx(idx2)));

% check roi is found well


for i=1:length(F)-3
s(i)=std([F(i:i+3).Area]);
end
[~,i] = min(s);

F = F(i:i+3);
temp = [[Link]];
temp = reshape(temp,2,4)';

[~,vidx] = sort(temp(:,2));
temp1 = temp(vidx(1:2),:);
[~,u1] = sort(temp1(:,1));
temp2 = temp(vidx(3:4),:);
[~,u2] = sort(temp2(:,1));

resize_factor = [1 0 0 0;0 1 0 0; -1/2 0 2 0; 0 -1/2 0 2];


roi = reshape([F(vidx([u1;u2+2])).BoundingBox],4,[])'*resize_factor;
roi = ceil(roi);

rect.hg1 = imrect(hg,roi(1,:));
rect.hg2 = imrect(hg,roi(2,:));
rect.hg3 = imrect(hg,roi(3,:));
rect.hg4 = imrect(hg,roi(4,:));

addNewPositionCallback(rect.hg1,@(pos) NewRoi(pos,hObject,1));
addNewPositionCallback(rect.hg2,@(pos) NewRoi(pos,hObject,2));
addNewPositionCallback(rect.hg3,@(pos) NewRoi(pos,hObject,3));
addNewPositionCallback(rect.hg4,@(pos) NewRoi(pos,hObject,4));

% Set numbers
roi2 = roi*[1 0; 0 1; 0 0; 0 0];

lbl = strtrim(cellstr(num2str((1:4)')));
rect.P(1) = text(roi2(1,1), roi2(1,2),
lbl(1),'color','k','EdgeColor','k','VerticalAlignment','top','BackgroundC
olor','w',...

'HorizontalAlignment','center','VerticalAlignment','middle','Parent',hand
[Link]);
rect.P(2) = text(roi2(2,1), roi2(2,2),
lbl(2),'color','k','EdgeColor','k','VerticalAlignment','top','BackgroundC
olor','w',...

'HorizontalAlignment','center','VerticalAlignment','middle','Parent',hand
[Link]);
rect.P(3) = text(roi2(3,1), roi2(3,2),
lbl(3),'color','k','EdgeColor','k','VerticalAlignment','top','BackgroundC
olor','w',...

'HorizontalAlignment','center','VerticalAlignment','middle','Parent',hand
[Link]);
rect.P(4) = text(roi2(4,1), roi2(4,2),
lbl(4),'color','k','EdgeColor','k','VerticalAlignment','top','BackgroundC
olor','w',...

'HorizontalAlignment','center','VerticalAlignment','middle','Parent',hand
[Link]);

% Extract Image Coordinate from 30 sequential images


h = waitbar(0,'Please wait...');
steps = 1;
tmp = zeros(1,8);
for step = 1:steps
f = getsnapshot([Link]);
tmp(step,:) = [getPoint(f,roi(1,:)) getPoint(f,roi(2,:))
getPoint(f,roi(3,:)) getPoint(f,roi(4,:))];
waitbar(step / steps)
end
close(h)
ic = reshape(mean(tmp),2,[])';

% plot centroid
if(isfield(handles,'Centroid'))
tmp = [Link];
[Link] =
line(ic(:,1),ic(:,2),'Marker','+','Color','r','Parent',[Link],'L
ineStyle','none');
delete(tmp);
else
[Link] =
line(ic(:,1),ic(:,2),'Marker','+','Color','r','Parent',[Link],'L
ineStyle','none');
end

% calculate Homography
H = fitgeotrans([Link],ic,'projective');
H = H.T';

% save data
[Link] = roi;
[Link].H = H;
[Link] = rect;
guidata(hObject,handles);
else
% setting ROI
rect.hg1 = imrect(hg);
rect.hg2 = imrect(hg);
rect.hg3 = imrect(hg);
rect.hg4 = imrect(hg);

roi(1,:) = ceil(getPosition(rect.hg1));
roi(2,:) = ceil(getPosition(rect.hg2));
roi(3,:) = ceil(getPosition(rect.hg3));
roi(4,:) = ceil(getPosition(rect.hg4));

addNewPositionCallback(rect.hg1,@(pos) NewRoi(pos,hObject,1));
addNewPositionCallback(rect.hg2,@(pos) NewRoi(pos,hObject,2));
addNewPositionCallback(rect.hg3,@(pos) NewRoi(pos,hObject,3));
addNewPositionCallback(rect.hg4,@(pos) NewRoi(pos,hObject,4));

% Set numbers
roi2 = roi*[1 0; 0 1; 0 0; 0 0];
% [1 0;0 1;1/8 0;0 1/8];
lbl = strtrim(cellstr(num2str((1:4)')));
rect.P(1) = text(roi2(1,1), roi2(1,2),
lbl(1),'color','k','EdgeColor','k','VerticalAlignment','top','BackgroundC
olor','w',...

'HorizontalAlignment','center','VerticalAlignment','middle','Parent',hand
[Link]);
rect.P(2) = text(roi2(2,1), roi2(2,2),
lbl(2),'color','k','EdgeColor','k','VerticalAlignment','top','BackgroundC
olor','w',...

'HorizontalAlignment','center','VerticalAlignment','middle','Parent',hand
[Link]);
rect.P(3) = text(roi2(3,1), roi2(3,2),
lbl(3),'color','k','EdgeColor','k','VerticalAlignment','top','BackgroundC
olor','w',...

'HorizontalAlignment','center','VerticalAlignment','middle','Parent',hand
[Link]);
rect.P(4) = text(roi2(4,1), roi2(4,2),
lbl(4),'color','k','EdgeColor','k','VerticalAlignment','top','BackgroundC
olor','w',...
'HorizontalAlignment','center','VerticalAlignment','middle','Parent',hand
[Link]);

% Extract Image Coordinate from 30 sequential images


h = waitbar(0,'Please wait...');
steps = 2;
tmp = zeros(1,8);
for step = 1:steps
f = getsnapshot([Link]);
tmp(step,:) = [getPoint(f,roi(1,:)) getPoint(f,roi(2,:))
getPoint(f,roi(3,:)) getPoint(f,roi(4,:))];
waitbar(step / steps)
end
close(h)
ic = reshape(mean(tmp),2,[])';

% plot centroid
if(isfield(handles,'Centroid'))
tmp = [Link];
[Link] =
line(ic(:,1),ic(:,2),'Marker','+','Color','r','Parent',[Link],'L
ineStyle','none');
delete(tmp);
else
[Link] =
line(ic(:,1),ic(:,2),'Marker','+','Color','r','Parent',[Link],'L
ineStyle','none');
end

% calculate Homography
H = fitgeotrans([Link],ic,'projective');
H = H.T';

% save data
[Link] = roi;
[Link].H = H;
[Link] = rect;
guidata(hObject,handles);
end

set([Link], 'String', 'Prestep has been done');

function pushbutton3_Callback(hObject, ~, 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)
set([Link], 'String', 'Waiting for Start');

[Link].t_start = datestr(now,30);
[Link] = [];
[Link] = [];
[Link] = [];
guidata(hObject,handles);

start([Link]);

set([Link], 'String', 'Start has been done');

function pushbutton4_Callback(hObject, ~, 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)
set([Link], 'String', 'Waiting for Stop');

[Link].t_end = datestr(now,30);
stop([Link]);
ic = [Link];
homo = [Link];
angle = [Link];
filename = [[Link].t_start '-' [Link].t_end];
uisave({'ic','homo','angle'},filename);

set([Link], 'String', 'Stop has been done');

function pushbutton5_Callback(hObject, ~, 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)
data = [Link];

% Time
fr = 29.97;
tout = 0:1/fr:length([Link](:,1))/fr-1/fr;
tout = tout';

% Plot
figure;hold on;
subplot(2,2,1);plot(tout,[Link](:,1));
title('X-homography1');
xlabel('time(sec)');
ylabel('displacement(mm)');

subplot(2,2,2);plot(tout,[Link](:,3));
title('X-homography2');
xlabel('time(sec)');
ylabel('displacement(mm)');

subplot(2,2,3);plot(tout,[Link](:,5));
title('X-homography3');
xlabel('time(sec)');
ylabel('displacement(mm)');

subplot(2,2,4);plot(tout,[Link](:,7));
title('X-homography4');
xlabel('time(sec)');
ylabel('displacement(mm)');
hold off;

function pushbutton6_Callback(hObject, eventdata, handles)


% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
data = [Link];

% Time
fr = 29.97;
tout = 0:1/fr:length([Link](:,1))/fr-1/fr;
tout = tout';

% Plot
figure;hold on;
subplot(2,2,1);plot(tout,[Link](:,2));
title('Y-homography1');
xlabel('time(sec)');
ylabel('displacement(mm)');

subplot(2,2,2);plot(tout,[Link](:,4));
title('Y-homography2');
xlabel('time(sec)');
ylabel('displacement(mm)');

subplot(2,2,3);plot(tout,[Link](:,6));
title('Y-homography3');
xlabel('time(sec)');
ylabel('displacement(mm)');

subplot(2,2,4);plot(tout,[Link](:,8));
title('Y-homography4');
xlabel('time(sec)');
ylabel('displacement(mm)');
hold off;

function pushbutton7_Callback(hObject, eventdata, handles)


% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
data = [Link];

% Time
fr = 29.97;
tout = 0:1/fr:length([Link](:,1))/fr-1/fr;
tout = tout';

% Plot
figure;hold on;
subplot(2,2,1);plot(tout,[Link](:,1));
title('Y-homography1');
xlabel('time(sec)');
ylabel('displacement(mm)');

subplot(2,2,2);plot(tout,[Link](:,2));
title('Y-homography2');
xlabel('time(sec)');
ylabel('displacement(mm)');

subplot(2,2,3);plot(tout,[Link](:,3));
title('Y-homography3');
xlabel('time(sec)');
ylabel('displacement(mm)');

subplot(2,2,4);plot(tout,[Link](:,4));
title('Y-homography4');
xlabel('time(sec)');
ylabel('displacement(mm)');
hold off;

%% edit Xmin Xmax Ymin Ymax Amin Amax

function Xmin_Callback(hObject, eventdata, handles)


% hObject handle to Xmin (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 Xmin as text


% str2double(get(hObject,'String')) returns contents of Xmin as a
double
X = str2double(get(hObject, 'String'));
if isnan(X)
set(hObject, 'String', -5);
errordlg('Input must be a number','Error');
end

if(get([Link],'value'))
Ymin = str2double(get([Link], 'String'));
Ymax = str2double(get([Link], 'String'));
set([Link],'YLimMode','manual','YLim',[Ymin Ymax]);
end
guidata(hObject,handles)

function Xmin_CreateFcn(hObject, eventdata, handles)


% hObject handle to Xmin (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 Xmax_Callback(hObject, eventdata, handles)


% hObject handle to Xmax (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 Xmax as text


% str2double(get(hObject,'String')) returns contents of Xmax as a
double
X = str2double(get(hObject, 'String'));
if isnan(X)
set(hObject, 'String', 5);
errordlg('Input must be a number','Error');
end

if(get([Link],'value'))
Ymin = str2double(get([Link], 'String'));
Ymax = str2double(get([Link], 'String'));
set([Link],'YLimMode','manual','YLim',[Ymin Ymax]);
end
guidata(hObject,handles)

function Xmax_CreateFcn(hObject, eventdata, handles)


% hObject handle to Xmax (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 Ymax_Callback(hObject, eventdata, handles)
% hObject handle to Ymax (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 Ymax as text


% str2double(get(hObject,'String')) returns contents of Ymax as a
double
X = str2double(get(hObject, 'String'));
if isnan(X)
set(hObject, 'String', 5);
errordlg('Input must be a number','Error');
end

if(get([Link],'value'))
Ymin = str2double(get([Link], 'String'));
Ymax = str2double(get([Link], 'String'));
set([Link],'YLimMode','manual','YLim',[Ymin Ymax]);
end

guidata(hObject,handles)

function Ymax_CreateFcn(hObject, eventdata, handles)


% hObject handle to Ymax (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 Ymin_Callback(hObject, eventdata, handles)


% hObject handle to Ymin (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 Ymin as text


% str2double(get(hObject,'String')) returns contents of Ymin as a
double
X = str2double(get(hObject, 'String'));
if isnan(X)
set(hObject, 'String', -5);
errordlg('Input must be a number','Error');
end

if(get([Link],'value'))
Ymin = str2double(get([Link], 'String'));
Ymax = str2double(get([Link], 'String'));
set([Link],'YLimMode','manual','YLim',[Ymin Ymax]);
end
guidata(hObject,handles)

function Ymin_CreateFcn(hObject, eventdata, handles)


% hObject handle to Ymin (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 Amax_Callback(hObject, eventdata, handles)


% hObject handle to Ymax (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 Ymax as text


% str2double(get(hObject,'String')) returns contents of Ymax as a
double
X = str2double(get(hObject, 'String'));
if isnan(X)
set(hObject, 'String', 5);
errordlg('Input must be a number','Error');
end

if(get([Link],'value'))
Ymin = str2double(get([Link], 'String'));
Ymax = str2double(get([Link], 'String'));
set([Link],'YLimMode','manual','YLim',[Ymin Ymax]);
end

guidata(hObject,handles)

function Amax_CreateFcn(hObject, eventdata, handles)


% hObject handle to Ymax (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 Amin_Callback(hObject, eventdata, handles)


% hObject handle to Ymin (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 Ymin as text


% str2double(get(hObject,'String')) returns contents of Ymin as a
double
X = str2double(get(hObject, 'String'));
if isnan(X)
set(hObject, 'String', -5);
errordlg('Input must be a number','Error');
end

if(get([Link],'value'))
Ymin = str2double(get([Link], 'String'));
Ymax = str2double(get([Link], 'String'));
set([Link],'YLimMode','manual','YLim',[Ymin Ymax]);
end
guidata(hObject,handles)

function Amin_CreateFcn(hObject, eventdata, handles)


% hObject handle to Ymin (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

%% uipanel 1,2,3,4,5,6
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
% hObject handle to the selected object in uipanel1
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none
was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
if(hObject==[Link])
[Link] = 0;
elseif (hObject==handles.X1)
[Link] = 1;
elseif(hObject==handles.X2)
[Link] = 2;
elseif(hObject == handles.X3)
[Link] = 3;
else
[Link] = 4;
end

guidata(hObject,handles);

function uipanel2_SelectionChangeFcn(hObject, eventdata, handles)


% hObject handle to the selected object in uipanel3
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none
was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
if(hObject==[Link])
[Link] = 0;
elseif (hObject==handles.Y1)
[Link] = 1;
elseif(hObject==handles.Y2)
[Link] = 2;
elseif(hObject == handles.Y3)
[Link] = 3;
else
[Link] = 4;
end

guidata(hObject,handles);

function uipanel3_SelectionChangeFcn(hObject, eventdata, handles)


% hObject handle to the selected object in uipanel3
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none
was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)

if(hObject==[Link])
[Link] = 0;
elseif (hObject==handles.A1)
[Link] = 1;
elseif(hObject==handles.A2)
[Link] = 2;
elseif(hObject == handles.A3)
[Link] = 3;
else
[Link] = 4;
end

guidata(hObject,handles);

function uipanel4_SelectionChangeFcn(hObject, eventdata, handles)


% hObject handle to the selected object in uipanel2
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none
was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
if (hObject==[Link])
set([Link],'YLimMode','auto');
set([Link],'Enable','off');
set([Link],'Enable','off');
else
set([Link],'Enable','on');
set([Link],'Enable','on');
Ymin = str2double(get([Link], 'String'));
Ymax = str2double(get([Link], 'String'));
set([Link],'YLimMode','manual','YLim',[Ymin Ymax]);
end

guidata(hObject,handles);

function uipanel5_SelectionChangeFcn(hObject, eventdata, handles)


% hObject handle to the selected object in uipanel4
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none
was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
if (hObject==[Link])
set([Link],'YLimMode','auto');
set([Link],'Enable','off');
set([Link],'Enable','off');
else
set([Link],'Enable','on');
set([Link],'Enable','on');
Ymin = str2double(get([Link], 'String'));
Ymax = str2double(get([Link], 'String'));
set([Link],'YLimMode','manual','YLim',[Ymin Ymax]);
end

guidata(hObject,handles);

function uipanel6_SelectionChangeFcn(hObject, eventdata, handles)


% hObject handle to the selected object in uipanel4
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none
was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
if (hObject==[Link])
set([Link],'YLimMode','auto');
set([Link],'Enable','off');
set([Link],'Enable','off');
else
set([Link],'Enable','on');
set([Link],'Enable','on');
Ymin = str2double(get([Link], 'String'));
Ymax = str2double(get([Link], 'String'));
set([Link],'YLimMode','manual','YLim',[Ymin Ymax]);
end

guidata(hObject,handles);

%% --- User defined functions

% Initialize GUI
function initialize_gui(~, handles)
% If the metricdata field is present and the reset flag is false, it
means
% we are we are just re-initializing a GUI by calling it from the cmd
line
% while it is up. So, bail out as we dont want to reset the data.

[Link] = 0;
[Link] = 0;
[Link] = 0;

% Update handles structure

[Link] = data;
guidata(handles.figure1, handles);

% When ROI changed


function NewRoi(pos,hObject,num)
handles = guidata(hObject);
[Link](num,:) = ceil(pos);
set([Link].P(num),'Position',pos*[1 0; 0 1; 0 0; 0 0]);
guidata(hObject,handles);

% Frames Acquired Function


function FramesAcquiredFcn(video,~,hObject)
[Link];
if([Link]>500)
% This means image capturing speed doesn't follow that of image
processing.
disp('TIMEOUT');
stop(video);
end

handles = guidata(hObject);
roi = [Link];
H = [Link].H;
angle1 = [Link];

frame = getdata(video,3) ;
for i=1:3
ic1 = getPoint(frame(:,:,i),roi(1,:));
ic2 = getPoint(frame(:,:,i),roi(2,:));
ic3 = getPoint(frame(:,:,i),roi(3,:));
ic4 = getPoint(frame(:,:,i),roi(4,:));
ic = [ic1;ic2;ic3;ic4]';
set([Link],'XData',ic(1,:),'YData',ic(2,:));

% displacement X Y
tmp = H\[ic;ones(1,4)];
homo = tmp./repmat(tmp(3,:),3,1);

% angle theta
tmp = homo(1:2,:)-repmat(mean(homo(1:2,:),2),1,4);
angle = atan2d(tmp(1,:),tmp(2,:));
angle = angle-angle1;
% Updata Data Plot axes
if(~[Link])
dataPlot(mean(homo(1,:)),[Link]);
else
dataPlot(homo(1,[Link]),[Link]);
end

if(~[Link])
dataPlot(mean(homo(2,:)),[Link]);
else
dataPlot(homo(2,[Link]),[Link]);
end

if(~[Link])
dataPlot(mean(angle),[Link]);
else
dataPlot(angle([Link]),[Link]);
end

% save data
[Link] = [[Link];reshape(ic,1,[])];
[Link] = [[Link];reshape(homo(1:2,:),1,[])];
[Link] = [[Link];angle];
end
guidata(hObject,handles);

% Getting a Point on Image Coordinatee


function ic = getPoint(f,ROI)

im = f(ROI(2):ROI(2)+ROI(4),ROI(1):ROI(1)+ROI(3)) ;
Threshold = graythresh(im);
bw = im2bw(im,Threshold);
[v,h] = find(bw == 1) ;
x = mean(h);
y = mean(v);
% Roi = moveRoi([x,y],ROI);

x = x+ROI(1);
y = y+ROI(2);
ic = [x y];

% Real Time Plot


function dataPlot(data,line)
Y = get(line, 'YData');
Y = [Y(2:end) data'];
set(line,'YData', Y);

% --------------------------------------------------------------------
function Untitled_1_Callback(hObject, eventdata, handles)
% hObject handle to Untitled_1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

You might also like