1 简介

卡尔曼滤波器是目标状态估计算法中常用的 滤波器,通过建立目标的状态模型并估计目标的运 动速度及加速度,可以对目标质心的未来点进行预 测, 从而缩小搜索区域, 克服由于目标被局部遮挡 时造成的跟踪丢失问题 。

基于卡尔曼滤波器的运动目标跟踪算法通常 有以下步骤 。

1)计算运动目标的特征信息。为了对运动目标进行跟踪, 首先用外接矩形对运动目标进行标 定 ,然后计算运动目标的质心和外接矩形的宽和高

2)用得到的特征信息初始化卡尔曼滤波器。 由于初始化时对目标的速度以及目标外接矩形的 变化速度未知 ,所以对于这几个量初始化为 0。

3)用卡尔曼滤波器对下一帧中对应的目标区 域进行预测, 当下一帧到来后 , 在预测区域内进行 目标匹配 。 

4)如果匹配, 则更新卡尔曼滤波器, 并记录下当前帧中的目标信息。 基于卡尔曼滤波器的运动目标跟踪算法模型 如图 1所示。下面将详细讨论其主要步骤。

卡尔曼滤波目标跟踪python例子 卡尔曼滤波 跟踪_卡尔曼滤波

2 部分代码

function varargout = MainTrack(varargin)
% MAINTRACK MATLAB code for MainTrack.fig
%      MAINTRACK, by itself, creates a new MAINTRACK or raises the existing
%      singleton*.
%
%      H = MAINTRACK returns the handle to a new MAINTRACK or the handle to
%      the existing singleton*.
%
 
    
    
%进行扑拓处理
    mask = imopen(mask, strel('rectangle', [3,3]));
    mask = imclose(mask, strel('rectangle', [15, 15]));
    mask = imfill(mask, 'holes');
    
%获取分析图像
    [~, centroids, bboxes] = obj.blobAnalyser.step(mask);
    
    
    %预测新的路径
    for i = 1:length(tracks)

    numAssignedTracks = size(assignments, 1);
    for i = 1:numAssignedTracks
        trackIdx = assignments(i, 1);
        detectionIdx = assignments(i, 2);
        centroid = centroids(detectionIdx, :);
uttongroup2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uibuttongroup2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
%set(handles.radioCam,'value','1');
%set(handles.radioVideo,'value','0');


% --- Executes on button press in snapShot.
function snapShot_Callback(hObject, eventdata, handles)
% hObject    handle to snapShot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

camSwitch=get(handles.radioCam,'value');%获取radioWecam的数字,1或者0

switch camSwitch
    case 1
        frame=snapshot(handles.cam);
        %delete(handles.h);
        axes(handles.main);
        imshow(frame);
    case 0;
        [fileName,pathName]=uigetfile({'*.avi';'*.mpeg';'*.mov'},'读入视频');
        if(fileName == 0)
            return;
        end
        str=[pathName,fileName];
        set(handles.videoPath,'String',str);
        set(handles.videoPath,'visible','on');
        % handles.video = vision.VideoFileReader(str);
        handles.video = VideoReader(str);
        
        set(handles.timeLimit,'String',num2str(handles.video.Duration));
        
        frame=readFrame(handles.video);
        axes(handles.main);
        imshow(frame);
end

handles.frame=frame;
set(handles.uiSelection,'Visible','on');
guidata(hObject,handles);%更新数据

% n=0.5;
% while n<get(handles.radioCam,'value')
%     frame=snapshot(cam);
%     imshow(frame);
%     drawnow;
% end
% while n>get(handles.radioCam,'value')
%     frame=step(handles.video);
%     imshow(frame);
%     drawnow;
% end


% --- Executes when selected object is changed in uiSelection.
function uiSelection_SelectionChangedFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uiSelection
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


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


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


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


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider


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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



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



% --- Executes during object creation, after setting all properties.
function saveName_CreateFcn(hObject, eventdata, handles)
% hObject    handle to saveName (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 checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox1


% --- Executes on button press in radiobutton9.
function radiobutton9_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton9
saveSwitch=get(hObject,'Value');
if saveSwitch>0.5
    handles.saveSwitch=1;
    set(handles.saveName,'Visible','on')
else
    handles.saveSwitch=0;
    set(handles.saveName,'Visible','off')
end
guidata(hObject,handles);



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


% --- Executes during object creation, after setting all properties.
function timeLimit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to timeLimit (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 during object creation, after setting all properties.
function uibuttongroup4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to uibuttongroup4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
handles.f=[];%对figure的handles进行初始化
guidata(hObject,handles);


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



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


% --- Executes during object creation, after setting all properties.
function unitH_CreateFcn(hObject, eventdata, handles)
% hObject    handle to unitH (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

3 仿真结果

卡尔曼滤波目标跟踪python例子 卡尔曼滤波 跟踪_卡尔曼滤波_02

卡尔曼滤波目标跟踪python例子 卡尔曼滤波 跟踪_卡尔曼滤波目标跟踪python例子_03

4 参考文献

[1]武建, 张雷, 刘艋,等. 基于卡尔曼滤波器的运动目标跟踪的实现[J]. 硅谷, 2013(10):2.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。