openCV study-Module 3_视频处理--目标追踪

In this section you will learn different techniques to work with videos like object tracking etc.

视频处理的主要的任务: 目标追踪。

复习之前学的视频颜色滤波,追踪某个颜色:CV_Lesson12图片颜色滤波,提取_视频中追踪蓝色物体.py

 和  CV_Lesson22-6_直方图反向投影.py


CV_Lesson36_目标追踪_Meanshift算法.py

steps:
    0.intial box
    1.先滤波
    2.归一化
    3.backproject
    4.meanshit 调整boundgin box
    5.画出来

bounding box 往像素概率分布大的方向移动。

问题:我们的窗口的大小是固 定的
而汽车由远及近 在视觉上 是一个 渐变大的过程
固定的窗口是不 合适的。所以我们需要根据目标的大小和角度来对窗口的大小和角度进行修订 !!!

§ Type  TBD

enum cv::TermCriteria::Type

Criteria type, can be one of: COUNT, EPS or COUNT + EPS

Enumerator
COUNT 

the maximum number of iterations or elements to compute

MAX_ITER 

ditto

EPS 

the desired accuracy or change in parameters at which the iterative algorithm stops

§ meanShift() 返回的结果是迭代次数,新的window 位置

int cv::meanShift(InputArray probImage,
  Rect & window,
  TermCriteria criteria 
 )  

Finds an object on a back projection image.

Parameters

probImageBack projection of the object histogram. See calcBackProject for details.
windowInitial search window.
criteria

Stop criteria for the iterative search algorithm. returns : Number of iterations CAMSHIFT took to converge. The function implements the iterative object search algorithm. It takes the input back projection of an object and the initial position. The mass center in window of the back projection image is computed and the search window center shifts to the mass center. The procedure is repeated until the specified number of iterations criteria.maxCount is done or until the window center shifts by less than criteria.epsilon. The algorithm is used inside CamShift and, unlike CamShift , the search window size or orientation do not change during the search.搜索窗口大小或方向在搜索过程中不会改变 BAD!!!

You can simply pass the output of calcBackProject to this function. But better results can be obtained if you pre-filter the back projection and remove the noise. For example, you can do this by retrieving connected components with findContours , throwing away contours with small area ( contourArea ), and rendering the remaining contours with drawContours.

§ calcBackProject() [1/3]

void cv::calcBackProject(const Mat * images,
  int nimages,
  const int * channels,
  InputArray hist,
  OutputArray backProject,
  const float ** ranges,
  double scale = 1,
  bool uniform = true 
 )  

Calculates the back projection of a histogram.

The function cv::calcBackProject calculates the back project of the histogram. That is, similarly to cv::calcHist , at each location (x, y) the function collects the values from the selected channels in the input images and finds the corresponding histogram bin. But instead of incrementing it, the function reads the bin value, scales it by scale , and stores in backProject(x,y) . In terms of statistics, the function computes probability of each element value in respect with the empirical probability distribution represented by the histogram. See how, for example, you can find and track a bright-colored object in a scene:

反向投影的意思就是,直方图是统计像素的分布,而反向投影呢, 就是在一个2D 的画布上,把 i像素对应的属于XX bin 颜色的的值 存在i的坐标下,得到一幅新的图片。这个反映了像素的概率分布

  • Before tracking, show the object to the camera so that it covers almost the whole frame. Calculate a hue histogram. The histogram may have strong maximums, corresponding to the dominant colors in the object.
  • When tracking, calculate a back projection of a hue plane of each input video frame using that pre-computed histogram. Threshold the back projection to suppress weak colors. It may also make sense to suppress pixels with non-sufficient color saturation and too dark or too bright pixels.
  • Find connected components in the resulting picture and choose, for example, the largest component.

This is an approximate algorithm of the CamShift color object tracker.

Parameters

imagesSource arrays. They all should have the same depth, CV_8U, CV_16U or CV_32F , and the same size. Each of them can have an arbitrary number of channels.
nimagesNumber of source images.
channelsThe list of channels used to compute the back projection. The number of channels must match the histogram dimensionality. The first array channels are numerated from 0 to images[0].channels()-1 , the second array channels are counted from images[0].channels() to images[0].channels() + images[1].channels()-1, and so on.
histInput histogram that can be dense or sparse.
backProjectDestination back projection array that is a single-channel array of the same size and depth as images[0] .
rangesArray of arrays of the histogram bin boundaries in each dimension. See cv::calcHist .
scaleOptional scale factor for the output back projection.
uniformFlag indicating whether the histogram is uniform or not (see above).

原理: TBD 


CV_Lesson37_目标追踪_CamShift算法.py

CAMshift (Continuously Adaptive Meanshift)--改变上面的问题。可以动态调整bounding box size  和方向!

camshift_face.gif

steps:
1. meanshit
2.调整bounding box 大小,方向

CamShift(...)
    CamShift(probImage, window, criteria) -> retval, window
    .   @brief Finds an object center, size, and orientation.
    .   
    .   @param probImage Back projection of the object histogram. See calcBackProject.
    .   @param window Initial search window.
    .   @param criteria Stop criteria for the underlying meanShift.
    .   returns
    .   (in old interfaces) Number of iterations CAMSHIFT took to converge---
    注意接口新的CV中以及改掉了:返回的是一个旋转矩形。
    .   The function implements the CAMSHIFT object tracking algorithm @cite Bradski98 . First, it finds an
    .   object center using meanShift and then adjusts the window size and finds the optimal rotation. The
    .   function returns the rotated rectangle structure that includes the object position, size, and
    .   orientation. The next position of the search window can be obtained with RotatedRect::boundingRect()

原理: TBD 


CV_Lesson38_目标追踪_稀疏光流LK.py

应用:

把运动目标轮廓点,跳出来,画出运动路径

steps:

1.找到第一幅图的轮廓坐标

2,运用光流追踪,获得运动后的点坐标

连线画出运动方向。

 

光流是由物体或相机的运动引起的图像对象在连续两帧之间的视运动模式。它是一个二维向量场,其中每个向量都是一个位移向量,表示点从第一帧到第二帧的移动

可以用于:运动追踪,视频稳定,视频压缩

理论:https://blog.youkuaiyun.com/qq_41368247/article/details/82562165

基于以下假设:

1.像素强度(值)连续帧间保持不变

2.相邻像素的运动相似

物理意义:下图展示的便是三维空间内物体的运动在二维成像平面上的投影。得到的是一个描述位置变化的二维矢量,但在运动间隔极小的情况下,我们通常将其视为一个描述该点瞬时速度的二维矢量u=(u,v),称为光流矢量。

 


 

fx=就是x的梯度,fy就是y的梯度,ft= I/dt,时间的梯度。

V=[u,v] 无法直接得到。V*grident(x,y)= I(t)

解二元一次方程, 只有一个方程,显然无法得到唯一解,

利用最小二乘法,n个点(这些点的运动相似--微小运动)拟合。得到运动向量V。


CV_Lesson39_目标追踪_稠密光流.py

稠密光流法:稀疏的光流,没有效验输出的准确性

Lucas-Kanade方法计算稀疏特征集的光流(在我们的示例中,使用Shi-Tomasi算法检测拐角)。OpenCV提供了另一种计算稠密光流的算法。它计算帧内所有点的光流。它基于Gunner Farneback在2003年发表的“基于多项式展开的两帧运动估计”中解释的算法。

下面的例子展示了如何使用上面的算法找到密集光流。我们得到了一个具有光流向量(u,v)的2通道阵列。我们找到它们的大小和方向。为了更好地可视化,我们对结果进行了颜色编码。方向对应于图像的色调值。大小对应于值平面。请参阅以下代码:

原理: TBD

Exercises

  1. Check the code in samples/python/lk_track.py. Try to understand the code.
  2. Check the code in samples/python/opt_flow.py. Try to understand the code.

去除运动视频中的背景,保留前景。了解即可

CV_Lesson40_背景去除.py

CV_Lesson41_背景去除2.py


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值