美文网首页
OpenCV+Python 基于MeanShift、CAMShi

OpenCV+Python 基于MeanShift、CAMShi

作者: 音符纸飞机 | 来源:发表于2018-11-06 22:12 被阅读56次

OpenCV官方文档

物体跟踪

在一开始告诉程序一个跟踪目标,即我想跟踪什么,然后程序就在接下来的视频帧中去寻找这个目标。
首先对跟踪目标进行描述,这个描述是将跟踪目标区域转换为颜色HSV空间,然后得到H的这个通道的分布直方图,有了这个描述(特征)之后,我们就是要在下一个视频帧中找到和这个描述的一样的区域,但是我们知道要找到完全一样的区域很难,所以我们就用了一个相似函数来衡量我们找到的区域和我们的目标区域的相似度,通过这个相似函数,相似函数值越大说明我们找打的区域和目标区域越相似,所以我们的目标就是要找这个对应最大相似值的区域。

直方图反向投影

通过图像的反向投影矩阵,我们实际上把原图像简单化了,简单化的过程实际上就是提取出图像的某个特征。所以以后我们可以用这个特征来对比两幅图,如果两幅图的反向投影矩阵相似或相同,那么我们就可以判定这两幅图这个特征是相同的。

MeanShift

原理博文
算法博文
原理:在一堆数据中找到密度极值点。以二维的来说明,meanshift在一堆样本特征点中,任意找一个为圆心,以半径R画一个圆(在opencv中是一个矩形),然后落在这个圆中的所有点和圆心都会对应的一个向量,把所有这些向量相加(注意是向量相加),最终我们只得到一个向量。然后再以这个meanshift向量的终点为圆心,继续上述过程,又可以得到一个meanshift向量。 然后不断地继续这样的过程,我们可以得到很多连续的meanshift向量,这些向量首尾相连,最终得到会在一个地方停下来(即我们说的meanshift算法会收敛),最后的那个meanshift向量的终点就是最终得到的结果(一个点)。

MeanShift特征点,其实就是目标物体在下一帧的直方图反向投影
def meanShift(probImage, window, criteria): 
    """
    meanShift(probImage, window, criteria) -> retval, window
    .   @brief Finds an object on a back projection image.
    .   
    .   @param probImage Back projection of the object histogram. See calcBackProject for details.
    .   @param window Initial search window.
    .   @param criteria Stop criteria for the iterative search algorithm.
                       
        sample criteria: term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
        # Setup the termination criteria, either 10 iteration or move by atleast 1 pt

    .   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. 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.
    """

CamShift

由于Meanshift在跟踪中搜索框的大小一直不变,对目标的尺度变化不具有鲁棒性,Camshift的出现改进了这方面的不足。CamShift,即Continuously Adaptive Mean-Shift算法(连续自适应的Meanshift),利用不变矩对目标的尺寸进行估算,实现了连续自适应地调整跟踪窗口的大小和位置。
Computer Vision Face Tracking For Use in a Perceptual User Interface

def CamShift(probImage, window, criteria): 
    """
    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
    .   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()

    """

相关文章

网友评论

      本文标题:OpenCV+Python 基于MeanShift、CAMShi

      本文链接:https://www.haomeiwen.com/subject/ywrkxqtx.html