视频抖动程度检测基于LK光流算法的视频图像序列抖动程度计算matlab仿真

Posted fpga和matlab

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了视频抖动程度检测基于LK光流算法的视频图像序列抖动程度计算matlab仿真相关的知识,希望对你有一定的参考价值。

1.软件版本

matlab2021a

2.算法原理概述

根据LK光流提取算法,得到视频前后两帧图像的光流,假设,

X(t),Y(t)

表示t时刻光流场的X分量和Y分量;

那么晃动计算公为:

其中R为光流场这个图片的变长。

3.部分源码

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(varargin1)
    gui_State.gui_Callback = str2func(varargin1);
end

if nargout
    [varargout1: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
varargout1 = 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); 
    I0kk     = imread(fileName);
end
axes(handles.axes1);
imshow(I01);

% --- 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(I0kk);
    %分块
    blkSize    = floor(R/50);    
    R1         = blkSize*floor(R/blkSize);
    C1         = blkSize*floor(C/blkSize);
    
    I1         = double(rgb2gray(I0kk));
    I2         = double(rgb2gray(I0kk+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(I0kk));

    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;

4.仿真结论

 

 A09-55

开发者涨薪指南 48位大咖的思考法则、工作方式、逻辑体系

以上是关于视频抖动程度检测基于LK光流算法的视频图像序列抖动程度计算matlab仿真的主要内容,如果未能解决你的问题,请参考以下文章

Opencv - 如何区分抖动和平移?

常用视频目标跟踪算法仿真对比:帧间差分法,背景差分法,光流法,Meanshift,Camshift

实现抖音视频抖动效果---OpenCV-Python开发指南(53)

运动目标检测——光流法与opencv代码实现

视频稳定的最佳算法

基于 OpenCV 图像的光流场