Esempio n. 1
0
 def get_feature_map(self, im_patch, hog_cell_sz):
     hog_feature = extract_hog_feature(im_patch,
                                       cell_size=hog_cell_sz)[:, :, :27]
     if hog_cell_sz > 1:
         im_patch = self.mex_resize(
             im_patch,
             (self._window.shape[1], self._window.shape[0])).astype(
                 np.uint8)
     gray = cv2.cvtColor(im_patch,
                         cv2.COLOR_BGR2GRAY)[:, :, np.newaxis] / 255 - 0.5
     return np.concatenate((gray, hog_feature), axis=2)
Esempio n. 2
0
 def get_scale_subwindow(self, im, center, base_target_sz, scale_factors,
                         scale_window, scale_model_sz, hog_scale_cell_sz):
     n_scales = len(self.scale_factors)
     out = None
     for s in range(n_scales):
         patch_sz = (int(base_target_sz[0] * scale_factors[s]),
                     int(base_target_sz[1] * scale_factors[s]))
         patch_sz = (max(2, patch_sz[0]), max(2, patch_sz[1]))
         im_patch = cv2.getRectSubPix(im, patch_sz, center)
         im_patch_resized = self.mex_resize(im_patch,
                                            scale_model_sz).astype(np.uint8)
         tmp = extract_hog_feature(im_patch_resized,
                                   cell_size=hog_scale_cell_sz)
         if out is None:
             out = tmp.flatten() * scale_window[s]
         else:
             out = np.c_[out, tmp.flatten() * scale_window[s]]
     return out
Esempio n. 3
0
    def tracking(self, img, pos, polish):
        """
        obtain a subwindow for detecting at the positiono from last frame, and convert to Fourier domain
        find  a proper window size
        :param img:
        :param pos:
        :param iter:
        :return:
        """

        large_num = 0
        if polish > large_num:
            w_sz0 = self.window_sz0
            c_w = self.cos_window
        else:
            w_sz0 = self.window_sz_search0
            c_w = self.cos_window_search
        if self.is_rotation:
            patch = self.get_affine_subwindow(img, pos, self.sc, self.rot,
                                              w_sz0)
        else:
            sz_s = (int(np.floor(self.sc[0] * w_sz0[0])),
                    int(np.floor(self.sc[1] * w_sz0[1])))
            patchO = cv2.getRectSubPix(img, sz_s, pos)
            patch = cv2.resize(patchO, w_sz0, cv2.INTER_CUBIC)

        z = self.get_features(patch, self.cell_size)
        z = z * c_w[:, :, None]
        zf = fft2(z)
        ssz = (zf.shape[1], zf.shape[0], zf.shape[2])
        # calculate response of the classifier at all shifts
        wf = np.conj(self.model_xf) * self.model_alphaf[:, :, None] / np.size(
            self.model_xf)
        if polish <= large_num:
            w = pad(np.real(ifft2(wf)), (ssz[1], ssz[0]))
            wf = fft2(w)

        tmp_sz = ssz
        # compute convolution for each feature block in the Fourier domain
        # use general compute here for easy extension in future

        rff = np.sum(wf * zf, axis=2)
        rff_real = cv2.resize(rff.real, (tmp_sz[0], tmp_sz[1]),
                              cv2.INTER_NEAREST)
        rff_imag = cv2.resize(rff.imag, (tmp_sz[0], tmp_sz[1]),
                              cv2.INTER_NEAREST)
        rff = rff_real + 1.j * rff_imag
        response_cf = np.real(ifft2(rff))
        #response_cf=np.fft.fftshift(response_cf,axes=(0,1))
        response_cf = crop_filter_response(
            response_cf, (response_cf.shape[1], response_cf.shape[0]))

        response_color = np.zeros_like(response_cf)

        if self.use_color_hist:
            object_likelihood = self.get_colour_map(patch, self.pl, self.pi,
                                                    self.bin_mapping)
            response_color = get_center_likelihood(object_likelihood,
                                                   self.target_sz0)
            response_color = cv2.resize(
                response_color, (response_cf.shape[1], response_cf.shape[0]),
                cv2.INTER_CUBIC)

        # adaptive merge factor
        if self.adaptive_merge_factor is True:
            cf_conf = confidence_cf_apce(response_cf)
            adaptive_merge_factor = self.merge_factor * self.theta + (
                1 - self.theta) * (1 - cf_conf)
            response = (
                1 - adaptive_merge_factor
            ) * response_cf + adaptive_merge_factor * response_color
        else:
            response = (1 - self.merge_factor
                        ) * response_cf + self.merge_factor * response_color
        if self.vis is True:
            self.score = response
            self.crop_size = self.window_sz
        # sub-pixel search
        pty, ptx = np.unravel_index(np.argmax(response, axis=None),
                                    response.shape)

        if self.is_subpixel:
            slobe = 2
            idy = np.arange(pty - slobe, pty + slobe + 1)
            idx = np.arange(ptx - slobe, ptx + slobe + 1)
            idy = np.clip(idy, a_min=0, a_max=response.shape[0] - 1)
            idx = np.clip(idx, a_min=0, a_max=response.shape[1] - 1)
            weight_patch = response[idy, :][:, idx]
            s = np.sum(weight_patch) + 2e-16
            pty = np.sum(np.sum(weight_patch, axis=1) * idy) / s
            ptx = np.sum(np.sum(weight_patch, axis=0) * idx) / s
        cscore = PSR(response, 0.1)

        # update the translation status
        dy = pty - (response.shape[0]) // 2
        dx = ptx - (response.shape[1]) // 2

        if self.is_rotation:
            sn, cs = np.sin(self.rot), np.cos(self.rot)
            pp = np.array([[self.sc[1] * cs, -self.sc[0] * sn],
                           [self.sc[1] * sn, self.sc[0] * cs]])
            x, y = pos
            delta = self.cell_size * np.array([[dy, dx]]).dot(pp)
            x += delta[0, 1]
            y += delta[0, 0]
            pos = (x, y)
            patchL = self.get_affine_subwindow(
                img, pos, [1., 1.], self.rot,
                (int(np.floor(self.sc[0] * self.scale_sz[0])),
                 int(np.floor(self.sc[1] * self.scale_sz[1]))))
        else:
            x, y = pos
            pos = (x + self.sc[0] * self.cell_size * dx,
                   y + self.sc[1] * self.cell_size * dy)
            patchL = cv2.getRectSubPix(
                img, (int(np.floor(self.sc[0] * self.scale_sz[0])),
                      int(np.floor(self.sc[1] * self.scale_sz[1]))), pos)
        patchL = cv2.resize(patchL, self.scale_sz_window, cv2.INTER_CUBIC)
        patchLp = cv2.logPolar(patchL.astype(np.float32),
                               (patchL.shape[1] // 2, patchL.shape[0] // 2),
                               self.mag,
                               flags=cv2.INTER_LINEAR + cv2.WARP_FILL_OUTLIERS)
        patchLp = extract_hog_feature(patchLp, self.cell_size)
        #patchLp = patchLp * self.cos_window_scale[:, :, None]
        tmp_sc, tmp_rot, sscore = self.estimate_scale(self.model_patchLp,
                                                      patchLp, self.mag)
        tmp_sc = np.clip(tmp_sc, a_min=0.6, a_max=1.4)
        if tmp_rot > 1 or tmp_rot < -1:
            tmp_rot = 0
        return pos, tmp_sc, tmp_rot, cscore, sscore
Esempio n. 4
0
 def get_features(self, img, cell_size):
     hog_feature = extract_hog_feature(img.astype(np.uint8), cell_size)
     #resized_img=cv2.resize(img,(hog_feature.shape[1],hog_feature.shape[0]),cv2.INTER_CUBIC).astype(np.uint8)
     #cn_feature=extract_cn_feature(resized_img,1)
     cn_feature = extract_cn_feature(img, cell_size)
     return np.concatenate((hog_feature, cn_feature), axis=2)
Esempio n. 5
0
    def logupdate(self, init, img, pos, tmp_sc, tmp_rot):
        tmp = np.floor(self.sc[0] * tmp_sc * self.window_sz0[0]) + np.floor(
            self.sc[1] * tmp_sc * self.window_sz0[1])
        if tmp < 10:
            tmp_sc = 1.
        self.sc = (self.sc[0] * tmp_sc, self.sc[1] * tmp_sc)
        self.rot = self.rot + tmp_rot
        self.window_sz = (int(np.floor(self.sc[0] * self.window_sz0[0])),
                          int(np.floor(self.sc[1] * self.window_sz0[1])))
        self.window_sz_search = (int(
            np.floor(self.sc[0] * self.window_sz_search0[0])),
                                 int(
                                     np.floor(self.sc[1] *
                                              self.window_sz_search0[1])))
        # compute the current CF_STRCF model
        # sampling the image
        if self.is_rotation:
            patch = self.get_affine_subwindow(img, pos, self.sc, self.rot,
                                              self.window_sz0)
        else:
            patchO = cv2.getRectSubPix(img, self.window_sz, pos)
            patch = cv2.resize(patchO,
                               self.window_sz0,
                               interpolation=cv2.INTER_CUBIC)
        x = self.get_features(patch, self.cell_size)
        x = x * self.cos_window[:, :, None]
        xf = fft2(x)
        #kf=np.sum(xf*np.conj(xf),axis=2)/xf.size
        kf = self._kernel_correlation(xf, xf, self.kernel_type)
        alphaf = self.yf / (kf + self.lambda_)

        if self.is_rotation:
            # here is not similarity transformation
            patchL = self.get_affine_subwindow(
                img, pos, [1., 1.], self.rot,
                (int(np.floor(self.sc[0] * self.scale_sz[0])),
                 int(np.floor(self.sc[1] * self.scale_sz[1]))))
        else:
            patchL = cv2.getRectSubPix(
                img, (int(np.floor(self.sc[0] * self.scale_sz[0])),
                      int(np.floor(self.sc[1] * self.scale_sz[1]))), pos)
        patchL = cv2.resize(patchL, self.scale_sz_window, cv2.INTER_CUBIC)
        # get logpolar space and apply feature extraction
        patchLp = cv2.logPolar(patchL.astype(np.float32),
                               (patchL.shape[1] // 2, patchL.shape[0] // 2),
                               self.mag,
                               flags=cv2.INTER_LINEAR + cv2.WARP_FILL_OUTLIERS)

        patchLp = extract_hog_feature(patchLp, self.cell_size)
        #patchLp = patchLp * self.cos_window_scale[:, :, None]

        # updating color histogram probabilities
        sz = (patch.shape[1], patch.shape[0])

        #is_color=True
        if self.use_color_hist:
            pos_in = ((sz[0]) / 2 - 1, (sz[1]) / 2 - 1)
            lab_patch = patch
            inter_patch = cv2.getRectSubPix(lab_patch.astype(
                np.uint8), (int(round(sz[0] * self.inter_patch_rate)),
                            int(round(sz[1] * self.inter_patch_rate))), pos_in)
            self.interp_patch = inter_patch
            pl = self.get_color_space_hist(lab_patch, self.nbin)
            pi = self.get_color_space_hist(inter_patch, self.nbin)
        interp_factor_scale = self.learning_rate_scale
        if init == 1:  # first_frame
            self.model_alphaf = alphaf
            self.model_xf = xf
            self.model_patchLp = patchLp
            if self.use_color_hist:
                self.pl = pl
                self.pi = pi

        else:
            # CF_STRCF model
            self.model_alphaf = (
                1 - self.interp_factor
            ) * self.model_alphaf + self.interp_factor * alphaf
            self.model_xf = (1 - self.interp_factor
                             ) * self.model_xf + self.interp_factor * xf
            self.model_patchLp = (
                1 - interp_factor_scale
            ) * self.model_patchLp + interp_factor_scale * patchLp
            if self.use_color_hist:
                self.pi = (1 - self.color_update_rate
                           ) * self.pi + self.color_update_rate * pi
                self.pl = (1 - self.color_update_rate
                           ) * self.pl + self.color_update_rate * pl