def track(self, image):
        test_patch = utils.get_subwindow(image, self.pos, self.sz, scale_factor=self.currentScaleFactor)
        hog_feature_t = pyhog.features_pedro(test_patch / 255., 1)
        hog_feature_t = np.lib.pad(hog_feature_t, ((1, 1), (1, 1), (0, 0)), 'edge')
        xt = np.multiply(hog_feature_t, self.cos_window[:, :, None])
        xtf = np.fft.fft2(xt, axes=(0, 1))
        response = np.real(np.fft.ifft2(np.divide(np.sum(np.multiply(self.x_num, xtf),
                                                         axis=2), (self.x_den + self.lamda))))

        v_centre, h_centre = np.unravel_index(response.argmax(), response.shape)
        vert_delta, horiz_delta = \
            [(v_centre - response.shape[0] / 2) * self.currentScaleFactor,
             (h_centre - response.shape[1] / 2) * self.currentScaleFactor]

        self.pos = [self.pos[0] + vert_delta, self.pos[1] + horiz_delta]

        st = utils.get_scale_subwindow(image, self.pos, self.base_target_size,
                                 self.currentScaleFactor * self.scaleSizeFactors, self.scale_window,
                                 self.scale_model_sz)
        stf = np.fft.fftn(st, axes=[0])

        scale_reponse = np.real(np.fft.ifftn(np.sum(np.divide(np.multiply(self.s_num, stf),
                                                              (self.s_den[:, None] + self.lamda_scale)), axis=1)))
        recovered_scale = np.argmax(scale_reponse)
        self.currentScaleFactor = self.currentScaleFactor * self.scaleFactors[recovered_scale]

        if self.currentScaleFactor < self.min_scale_factor:
            self.currentScaleFactor = self.min_scale_factor
        elif self.currentScaleFactor > self.max_scale_factor:
            self.currentScaleFactor = self.max_scale_factor

        # update
        update_patch = utils.get_subwindow(image, self.pos, self.sz, scale_factor=self.currentScaleFactor)
        hog_feature_l = pyhog.features_pedro(update_patch / 255., 1)
        hog_feature_l = np.lib.pad(hog_feature_l, ((1, 1), (1, 1), (0, 0)), 'edge')
        xl = np.multiply(hog_feature_l, self.cos_window[:, :, None])
        xlf = np.fft.fft2(xl, axes=(0, 1))
        new_x_num = np.multiply(self.yf[:, :, None], np.conj(xlf))
        new_x_den = np.real(np.sum(np.multiply(xlf, np.conj(xlf)), axis=2))

        sl = utils.get_scale_subwindow(image, self.pos, self.base_target_size,
                                       self.currentScaleFactor * self.scaleSizeFactors, self.scale_window,
                                       self.scale_model_sz)
        slf = np.fft.fftn(sl, axes=[0])
        new_s_num = np.multiply(self.ysf[:, None], np.conj(slf))
        new_s_den = np.real(np.sum(np.multiply(slf, np.conj(slf)), axis=1))

        self.x_num = (1 - self.interp_factor) * self.x_num + self.interp_factor * new_x_num
        self.x_den = (1 - self.interp_factor) * self.x_den + self.interp_factor * new_x_den
        self.s_num = (1 - self.interp_factor) * self.s_num + self.interp_factor * new_s_num
        self.s_den = (1 - self.interp_factor) * self.s_den + self.interp_factor * new_s_den

        self.target_size = self.base_target_size * self.currentScaleFactor

        return vot.Rectangle(self.pos[1] - self.target_size[1] / 2,
                             self.pos[0] - self.target_size[0] / 2,
                             self.target_size[1],
                             self.target_size[0]
                             )
Example #2
0
def get_subwindow(im, pos, sz, feature='raw'):
    """
    Obtain sub-window from image, with replication-padding.
    Returns sub-window of image IM centered at POS ([y, x] coordinates),
    with size SZ ([height, width]). If any pixels are outside of the image,
    they will replicate the values at the borders.

    The subwindow is also normalized to range -0.5 .. 0.5, and the given
    cosine window COS_WINDOW is applied
    (though this part could be omitted to make the function more general).
    """

    if np.isscalar(sz):  # square sub-window
        sz = [sz, sz]

    ys = np.floor(pos[0]) + np.arange(sz[0], dtype=int) - np.floor(sz[0] / 2)
    xs = np.floor(pos[1]) + np.arange(sz[1], dtype=int) - np.floor(sz[1] / 2)

    ys = ys.astype(int)
    xs = xs.astype(int)

    # check for out-of-bounds coordinates and set them to the values at the borders
    ys[ys < 0] = 0
    ys[ys >= im.shape[0]] = im.shape[0] - 1

    xs[xs < 0] = 0
    xs[xs >= im.shape[1]] = im.shape[1] - 1

    out = im[np.ix_(ys, xs)]

    if feature == 'hog':
        hog_feature = pyhog.features_pedro(out / 255., 1)
        out = np.lib.pad(hog_feature, ((1, 1), (1, 1), (0, 0)), 'edge')

    return out
Example #3
0
    def track(self, image):
        # ---------------------------------------track--------------------------------- #
        test_patch = utils.get_subwindow(image, self.pos, self.sz)

        hog_feature_t = pyhog.features_pedro(test_patch / 255., 1)
        hog_feature_t = np.lib.pad(hog_feature_t, ((1, 1), (1, 1), (0, 0)),
                                   'edge')
        xt = np.multiply(hog_feature_t, self.cos_window[:, :, None])
        xtf = np.fft.fft2(xt, axes=(0, 1))
        #计算响应,直接多通道叠加
        response = np.real(
            np.fft.ifft2(
                np.divide(np.sum(np.multiply(self.x_num, xtf), axis=2),
                          (self.x_den + self.lamda))))
        #找响应最大值
        v_centre, h_centre = np.unravel_index(response.argmax(),
                                              response.shape)
        vert_delta, horiz_delta = \
            [(v_centre - response.shape[0] / 2),
             (h_centre - response.shape[1] / 2)]
        #新的位置
        self.pos = [self.pos[0] + vert_delta, self.pos[1] + horiz_delta]

        # ---------------------------------------update--------------------------------- #
        update_patch = utils.get_subwindow(image, self.pos, self.sz)
        hog_feature_l = pyhog.features_pedro(update_patch / 255., 1)
        hog_feature_l = np.lib.pad(hog_feature_l, ((1, 1), (1, 1), (0, 0)),
                                   'edge')
        xl = np.multiply(hog_feature_l, self.cos_window[:, :, None])
        xlf = np.fft.fft2(xl, axes=(0, 1))
        #更新位置滤波器
        new_x_num = np.multiply(self.yf[:, :, None], np.conj(xlf))
        new_x_den = np.real(np.sum(np.multiply(xlf, np.conj(xlf)), axis=2))

        #滤波器学习
        self.x_num = (1 - self.interp_factor
                      ) * self.x_num + self.interp_factor * new_x_num
        self.x_den = (1 - self.interp_factor
                      ) * self.x_den + self.interp_factor * new_x_den
        self.target_size = self.base_target_size

        return vot.Rectangle(self.pos[1] - self.target_size[1] / 2,
                             self.pos[0] - self.target_size[0] / 2,
                             self.target_size[1], self.target_size[0])
Example #4
0
def get_scale_subwindow(im, pos, base_target_size, scaleFactors, scale_window,
                        scale_model_sz):
    from pyhog import pyhog
    nScales = len(scaleFactors)
    out = []
    for i in range(nScales):
        patch_sz = np.floor(base_target_size * scaleFactors[i])
        scale_patch = get_subwindow(im, pos, patch_sz)
        im_patch_resized = transform.resize(scale_patch,
                                            scale_model_sz,
                                            mode='reflect')
        temp_hog = pyhog.features_pedro(im_patch_resized, 4)
        out.append(np.multiply(temp_hog.flatten(), scale_window[i]))

    return np.asarray(out)
Example #5
0
 def get_scale_sample(self, im, scaleFactors):
     from pyhog import pyhog
     resized_im_array = []
     for i, s in enumerate(scaleFactors):
         patch_sz = np.floor(self.first_target_sz * s)
         im_patch = self.get_subwindow(im, self.pos, patch_sz)  # extract image
         # because the hog output is (dim/4)-2>1:
         if self.first_target_sz.min() < 12:
             scale_up_factor = 12. / np.min(self.first_target_sz)
             im_patch_resized = imresize(im_patch, np.asarray(self.first_target_sz * scale_up_factor).astype('int'))
         else:
             im_patch_resized = imresize(im_patch, self.first_target_sz)  #resize image to model size
         features_hog = pyhog.features_pedro(im_patch_resized.astype(np.float64)/255.0, 4)
         resized_im_array.append(np.multiply(features_hog.flatten(), self.scale_window[i]))
     return np.asarray(resized_im_array)
Example #6
0
def get_scale_window(image, position, target_size, sfs, scale_window,
                     scale_model_sz):
    # pos = [position[1],position[0]]
    # ts = np.array([target_size[1],target_size[0]])

    out = []
    for i in range(len(sfs)):
        patch_sz = np.floor(target_size * sfs[i])
        scale_patch = get_ori(image, position, patch_sz)
        im_patch_resized = transform.resize(scale_patch,
                                            scale_model_sz,
                                            mode='reflect')
        temp_hog = pyhog.features_pedro(im_patch_resized, 4)
        out.append(np.multiply(temp_hog.flatten(), scale_window[i]))

    return np.asarray(out)
Example #7
0
def get_feature_map(im_patch, feature_list, num_feature_ch, out_size, w2c,
                    cell_size, projection_matrix):
    #im_patch_size = im_patch.shape
    #if 'fhog' in feature_list:
    ##    out_size = np.round(np.array([im_patch_size[0],im_patch_size[1]])/cell_size)
    #if 'fhog' not in feature_list:
    #    out_size = np.round(np.array([im_patch_size[0],im_patch_size[1]]))
    out = np.zeros((np.int(out_size[0]), np.int(out_size[1]), num_feature_ch))
    print('out_size')

    print(im_patch.shape)
    channel_id = 0
    if 'gray' in feature_list:
        print('using gray feature')
        im_patch_255 = np.floor(im_patch * 255)
        im_patch_255 = im_patch_255.astype(np.uint8)
        dstimg = cv2.cvtColor(im_patch_255, cv2.COLOR_BGR2GRAY)

        out[:, :, channel_id] = transform.resize(dstimg,
                                                 out_size,
                                                 mode='reflect')
        channel_id = channel_id + 1
    if 'fhog' in feature_list:
        print('using fhog feature')

        hog_feature_t = pyhog.features_pedro(im_patch / 255., cell_size)
        img_crop = np.lib.pad(hog_feature_t, ((1, 1), (1, 1), (0, 0)), 'edge')
        img_crop = img_crop[:, :, 18:27]
        out[:, :, channel_id:channel_id + 9] = img_crop
        channel_id = channel_id + 9
    if 'cn' in feature_list:
        print('using cn feature')
        im_patch_255 = np.floor(im_patch * 255)
        cn_out = im2c(im_patch_255, w2c,
                      (np.int(out_size[0]), np.int(out_size[1])))
        cn_out = np.dot(cn_out, projection_matrix)
        out[:, :, channel_id:channel_id + 2] = cn_out
    if 'raw' in feature_list:
        print('using raw feature')
        im_patch_255 = np.floor(im_patch * 255)
        img_colour = im_patch_255 - im_patch_255.mean()
        out[:, :, channel_id:channel_id + 3] = transform.resize(img_colour,
                                                                out_size,
                                                                mode='reflect')
    print(out.shape)
    return out
Example #8
0
def get_subwindow(im, pos, sz, scale_factor=None, feature='raw'):
    """
    Obtain sub-window from image, with replication-padding.
    Returns sub-window of image IM centered at POS ([y, x] coordinates),
    with size SZ ([height, width]). If any pixels are outside of the image,
    they will replicate the values at the borders.

    The subwindow is also normalized to range -0.5 .. 0.5, and the given
    cosine window COS_WINDOW is applied
    (though this part could be omitted to make the function more general).
    """
    #测试sz是不是标量,如果是,就是方形窗口
    if np.isscalar(sz):  # square sub-window
        sz = [sz, sz]

    sz_ori = sz
    if scale_factor != None:
        sz = np.floor(sz * scale_factor)
    #获取有效区域位置的坐标序号,注意数列/矩阵的运用
    ys = np.floor(pos[0]) + np.arange(sz[0], dtype=int) - np.floor(sz[0] / 2)
    xs = np.floor(pos[1]) + np.arange(sz[1], dtype=int) - np.floor(sz[1] / 2)

    ys = ys.astype(int)
    xs = xs.astype(int)

    # check for out-of-bounds coordinates and set them to the values at the borders
    # 有可能这个取特征的区域已经超出了图像的范围,将超出的范围置为边界数值
    ys[ys < 0] = 0
    ys[ys >= im.shape[0]] = im.shape[0] - 1

    xs[xs < 0] = 0
    xs[xs >= im.shape[1]] = im.shape[1] - 1

    out = im[np.ix_(ys, xs)]

    #misc.imresize函数实现图像的大小调整,可以给数字对表示调整到固定大小,小于1的数字表示按照比例调整,大于1就按照百分比调整
    if scale_factor != None:
        out = misc.imresize(out, sz_ori.astype(int))

    if feature == 'hog':
        from pyhog import pyhog
        hog_feature = pyhog.features_pedro(out / 255., 1)
        out = np.lib.pad(hog_feature, ((1, 1), (1, 1), (0, 0)), 'edge')
        print('getting hog features in get_subwindow')
    return out