在上篇文章中,介绍了从静态图片中分离目标的基础算法——二值化方法。本文将介绍从动态视频中分离出运动目标。将运动的目标或变化的区域从背景图像中分离出来,这一过程也被称为运动检测。运动检测常被用于视频监控,异常检测,追踪拍摄等领域。除了在工业领域有重要作用,运动检测也是许多领域应用落地的基础,例如无人机飞行,三维重建。目前运动检测主要的方法包括:背景相减,帧差分以及光流法。
背景相减,帧差分
背景相减与帧差分是运动检测的经典方法,也是目前的主流方法1。顾名思义,背景相减与帧差分主要利用图片相减得到的差分结果对目标进行检测,不同在于:背景相减将视频中的当前帧与确定好的或者实时更新的背景参考模型进行减法操作,根据阈值以找到不同的区域,例如将与背景图像差异超过一定阈值的区域作为运动区域,把小于阈值的部分作为背景区域。而帧差分则使用连续的两帧、三帧或者多帧图像进行差分运算根据阈值从差分结果中获取运动区域。背景相减与帧差分算法简单,主要用于
相机固定的场景,检测速度较快,然而容易受到外部光线变化的干扰。

光流算法
光流算法(Optical flow,OF)是指在连续的两帧图像中由于图像中的物体移动或者摄像头的移动导致的图像中目标像素的移动。与背景相减与帧差分不同,光流算法的核心是通过计算视频中像素在时间域上的变化以及相邻帧之间的相关性,找到上一帧跟当前帧之间存在的对应关系,从而计算出相邻帧之间物体的运动信息的方法2。光流法的工作原理基于两个基本假设:1) 场景的像素强度在相邻帧之间基本不变 2) 相邻像素具有相似的运动。
使用对于P(x,y)表示图像像素值,P(x,y,t)表示t时刻某一帧图像像素值,在经过\text{d}t时间,该像素在下一帧中移动了(dx,dy),由于这些像素是相同的且强度基本不变,因此P(x,y,t)可以表示成:
使用泰勒展开:
由于OF假设:场景的像素强度在相邻帧之间基本不变
左右同时除以 \Delta t,可得
上述式子称之为光流方程,\frac{\partial P}{\partial x} 和 \frac{\partial P}{\partial y} 为图像沿x, y梯度,\frac{\partial P}{\partial t} 是沿时间的梯度。光流法主要计算\frac{\Delta x}{\Delta t}, \frac{\Delta y}{\Delta t},以获取每个像素的速度估计,从而检测运动物体。目前光流方程存在多种解法,本文将主要介绍一种经典的光流解法——Lucas-Kanade算法3。相较于光流法两个基本的假设,L-K算法增加了一个空间假设的基本假设即:一个场景上邻近的点投影到图像上也是邻近点,且邻近点速度一致。opencv提供函数calcOpticalFlowPyrLK实现L-K算法,计算光流。
函数原型如下:
void cv::calcOpticalFlowPyrLK ( InputArray prevImg,
InputArray nextImg,
InputArray prevPts,
InputOutputArray nextPts,
OutputArray status,
OutputArray err,
Size winSize = Size(21, 21),
int maxLevel = 3,
TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01),
int flags = 0,
double minEigThreshold = 1e-4
)
代码时间
在使用
calcOpticalFlowPyrLK函数时,需要先调用GoodFeaturesToTrack函数寻找关键点,再使用calcOpticalFlowPyrLK计算关键点光流。同时本文依然使用上篇文章使用的Timer计算效率。
#include <iostream>
#include "timer.hpp"
#include <opencv2/video/tracking.hpp>
#include <opencv2/video.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/opencv.hpp>
#include "timer.hpp"
using namespace cv;
using namespace std;
int main(const int argc, const char *argv[])
{
const string file_name = std::string(argv[1]);
VideoCapture capture(file_name);
if (!capture.isOpened())
{
// error in opening the video input
cerr << "Unable to open file!" << endl;
cerr << file_name << endl;
return 0;
}
// Create some random colors
vector<Scalar> colors;
RNG rng;
for (int i = 0; i < 100; i++)
{
int r = rng.uniform(0, 256);
int g = rng.uniform(0, 256);
int b = rng.uniform(0, 256);
colors.push_back(Scalar(r, g, b));
}
Mat old_frame, old_gray;
vector<Point2f> p0, p1;
// Take first frame and find corners in it
capture >> old_frame;
cvtColor(old_frame, old_gray, COLOR_BGR2GRAY);
goodFeaturesToTrack(old_gray, p0, 100, 0.3, 7, Mat(), 7, false, 0.04);
// Create a mask image for drawing purposes
Mat mask = Mat::zeros(old_frame.size(), old_frame.type());
while (true)
{
Mat frame, frame_gray;
capture >> frame;
if (frame.empty())
break;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
// calculate optical flow
vector<uchar> status;
vector<float> err;
TermCriteria criteria =
TermCriteria((TermCriteria::COUNT) + (TermCriteria::EPS), 10, 0.03);
solution::TimerTracker tracker("LK");
calcOpticalFlowPyrLK(old_gray, frame_gray, p0, p1, status, err,
Size(15, 15), 2, criteria);
tracker("optical flow");
vector<Point2f> good_new;
for (uint i = 0; i < p0.size(); i++)
{
// Select good points
if (status[i] == 1)
{
good_new.push_back(p1[i]);
// draw the tracks
line(mask, p1[i], p0[i], colors[i], 2);
circle(frame, p1[i], 5, colors[i], -1);
}
}
Mat img;
add(frame, mask, img);
imshow("Frame", img);
int keyboard = waitKey(30);
if (keyboard == 'q' || keyboard == 27){
cv::imwrite("of.png", frame);
cv::imwrite("of_line.png", img);
break;
}
// Now update the previous frame and previous points
old_gray = frame_gray.clone();
p0 = good_new;
}
return 0;
return 0;
}
编译运行
$ cmake -Bbuild -S . -DCMAKE_BUILD_TYPE=Release
$ cmake --build build
user@phytiumpi:~/Documents/cv/track$ ./build/track_cross carseq.avi
LK, tracked
LK: optical flow elapsed: 5.328 ms.
LK, tracked
LK: optical flow elapsed: 2.135 ms.
LK, tracked
LK: optical flow elapsed: 2.663 ms.
LK, tracked
LK: optical flow elapsed: 1.501 ms.
LK, tracked
LK: optical flow elapsed: 2.044 ms.
LK, tracked
LK: optical flow elapsed: 1.684 ms.
追踪效果

光流效果


全部评论