matlab2021a
根据LK光流提取算法,得到视频前后两帧图像的光流,假设,
X(t),Y(t)
表示t时刻光流场的X分量和Y分量;
那么晃动计算公为:
其中R为光流场这个图片的变长。
function varargout = tops(varargin)
% TOPS MATLAB code for tops.fig
% TOPS, by itself, creates a new TOPS or raises the existing
% singleton*.
%
% H = TOPS returns the handle to a new TOPS or the handle to
% the existing singleton*.
%
% TOPS('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TOPS.M with the given input arguments.
%
% TOPS('Property','Value',...) creates a new TOPS or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before tops_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to tops_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 tops
% Last Modified by GUIDE v2.5 22-Dec-2018 15:47:17
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @tops_OpeningFcn, ...
'gui_OutputFcn', @tops_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 tops is made visible.
function tops_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 tops (see VARARGIN)
% Choose default command line output for tops
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes tops wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = tops_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.
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)
parameter;
NAME=0;
[filename filepath]=uigetfile('*.*','请选择文件');
SamplePath1 =[filepath,'\']; %存储图像的路径
fileExt = '*.jpg'; %待读取图像的后缀名
%获取所有路径
files = dir(fullfile(SamplePath1,fileExt));
for kk = 1:length(files)
fileName = strcat(SamplePath1,files(kk).name);
I0{kk} = imread(fileName);
end
axes(handles.axes1);
imshow(I0{1});
% --- Executes on button press in pushbutton2.
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)
parameter;
global dist;%
dist=[];
global x_;%
global y_;%
global hor_;%
global ver_;%
global R;%
global blkSize;%
for kk = 1:length(files)-2
kk
[R,C,K] = size(I0{kk});
%分块
blkSize = floor(R/50);
R1 = blkSize*floor(R/blkSize);
C1 = blkSize*floor(C/blkSize);
I1 = double(rgb2gray(I0{kk}));
I2 = double(rgb2gray(I0{kk+1}));
I1s = imresize(I1,[C1,R1]);
I2s = imresize(I2,[C1,R1]);
[row,col] = size(I1s);
blkr = row/blkSize;
blkc = col/blkSize;
%光流算法
%hor为水平运动矢量,ver为垂直运动矢量
[hor,ver] = func_Soptflow(I1s,I2s,blkSize);
x =(1:1:blkc);
y =(1:1:blkr);
hor_{kk} = hor;
ver_{kk} = ver;
%晃动指标
if kk>=2
dist(kk) = sqrt(mean(mean((hor_{kk}-hor_{kk-1}).^2 + (ver_{kk}-ver_{kk-1}).^2)))/floor(R/blkSize)/sqrt(2);
else
% dist(kk) = 0;
end
axes(handles.axes1);
imshow(uint8(I0{kk}));
axes(handles.axes2);
quiver(x,y,hor,ver);
axis([0,R/blkSize,0,R/blkSize]);
set(gca,'ydir','reverse');
axes(handles.axes3);
if kk>=2
plot([0:kk-2],[dist(2:end)],'b-o');
end
xlim([0,length(files)-1]);
xlabel('time (s)');
ylabel('Sloshing Index');
x_{kk} = x;
y_{kk} = y;
pause(0.001);
end
% --- Executes on button press in pushbutton3.
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)
parameter;
global dist;%
times = [1:length(files)-2]';
data1 = [dist'];
%构建数据组
data = [times,data1];
[m, n] = size(data);
data_cell = mat2cell(data, ones(m,1), ones(n,1));
title = {'Frame','Sloshing Index'};
result = [title;data_cell];
NAME = NAME+1;
%保存到excel中
s = xlswrite(['Save',num2str(NAME),'.xls'], result);
msgbox('Save Success');
% --- Executes on button press in pushbutton4.
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)
parameter;
global x_;%
global y_;%
global hor_;%
global ver_;%
global R;%
global blkSize;%
global dist;%
%以下全部是保存图片语句
for kk = 1:length(files)-2
figure(4);
quiver(x_{kk},y_{kk},hor_{kk},ver_{kk});
axis([0,R/blkSize,0,R/blkSize]);
set(gca,'ydir','reverse');
saveas(gcf,['flow_images/flow_images',num2str(kk),'.jpg'])
end
close(figure(4))
tt = 0:length(dist);
figure(5);
plot(tt(1:end-2),dist(2:end),'b-o');
xlim([0,length(files)-1]);
xlabel('time (s)');
ylabel('Sloshing Index');
saveas(gcf,'sloshing_images/sloshing_images.jpg')
close(figure(5))
msgbox('Save Success');
% --- 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)
clc;
clear;
close all;
A09-55