Example #1
0
    def initialize(self, image, region):

        if len(region) == 8:
            x_ = np.array(region[::2])
            y_ = np.array(region[1::2])
            region = [
                np.min(x_),
                np.min(y_),
                np.max(x_) - np.min(x_) + 1,
                np.max(y_) - np.min(y_) + 1
            ]

        self.window = max(region[2],
                          region[3]) * self.parameters.enlarge_factor

        left = max(region[0], 0)
        top = max(region[1], 0)

        right = min(region[0] + region[2], image.shape[1] - 1)
        bottom = min(region[1] + region[3], image.shape[0] - 1)

        self.position = (region[0] + region[2] / 2, region[1] + region[3] / 2)
        self.size = (round(region[2]), round(region[3]))
        self.template = image[int(top):int(bottom), int(left):int(right)]
        self.kernel = create_epanechnik_kernel(self.size[0], self.size[1],
                                               self.parameters.kernel_sigma)
        self.q = normalize_histogram(
            extract_histogram(self.template,
                              self.parameters.histogram_bins,
                              weights=self.kernel))
    def initialize(self, img, region):
        """
        Initialize the mean-shift tracker.

        Args:
            img (numpy.ndarray): First image.
            region (list): bounding box specification for the
            object on the first image. First and second values
            represent the position of the left-upper corner. The
            third and fourth values represent the width and the 
            height of the bounding box respectively.
        """

        # Set tracker name.
        self.name = "mean-shift-tracker"

        # Number of iterations performed to reposition bounding box,
        # maximum number of iterations performed and umber of trackings performed.
        self.num_it = []
        self.max_it = 0
        self.num_tracking_runs = 0

        # Get initialized position.
        self.pos = [region[0] + region[2] / 2, region[1] + region[3] / 2]

        # Set tracking patch size. Increment size by one if even.
        self.size = (int(region[2]) + abs(int(region[2]) % 2 - 1),
                     int(region[3]) + abs(int(region[3]) % 2 - 1))

        # Initialize tracking window indices grid.
        self.mesh_x, self.mesh_y = np.meshgrid(
            np.arange(-self.size[0] // 2 + 1, self.size[0] // 2 + 1),
            np.arange(-self.size[1] // 2 + 1, self.size[1] // 2 + 1))

        # Initialize kernels.
        self.kern1 = create_epanechnik_kernel(self.size[0], self.size[1], 2)
        self.kern2 = np.ones((self.size[1], self.size[0]))
        self.kern_bandwidth = 4

        # Get initial patch.
        patch = get_patch(img, self.pos, self.size)

        # Extract hitrogram from template using the specified kernel.
        hist_ = extract_histogram(patch[0],
                                  self.parameters.n_bins,
                                  weights=self.kern1)
        self.hist = hist_ / np.sum(hist_)
    def initialize(self, image, region):
        if len(region) == 8:
            x_ = np.array(region[::2])
            y_ = np.array(region[1::2])
            region = [
                np.min(x_),
                np.min(y_),
                np.max(x_) - np.min(x_) + 1,
                np.max(y_) - np.min(y_) + 1
            ]

        self.window = max(region[2],
                          region[3]) * self.parameters.enlarge_factor

        left = max(region[0], 0)
        top = max(region[1], 0)

        right = min(region[0] + region[2], image.shape[1] - 1)
        bottom = min(region[1] + region[3], image.shape[0] - 1)

        self.position = (region[0] + region[2] / 2, region[1] + region[3] / 2)
        self.size = (region[2], region[3])
        self.template = ex2_utils.get_patch(image, self.position, self.size)[0]
        # print(self.size)
        # print(self.position)
        # print(self.window)
        # cv2.imshow("Init", self.template)
        # cv2.waitKey(0)

        ep_kernel = ex2_utils.create_epanechnik_kernel(
            self.size[0], self.size[1], self.parameters.kernel_sigma)
        self.kernel = ep_kernel[:self.template.shape[0], :self.template.
                                shape[1]]

        # print(f"template shape: {self.template.shape}")
        # print(f"Kernel shape: {self.kernel.shape}")
        # print(f"Size 0: {self.size[0]}")
        # print(f"Size 1: {self.size[1]}")
        h1 = ex2_utils.extract_histogram(self.template,
                                         16,
                                         weights=self.kernel)
        self.current_model = h1 / np.sum(h1)
Example #4
0
    def initialize(self, image, region):

        region = [int(el) for el in region]

        if (region[2] % 2 == 0):
            region[2] += 1
        if (region[3] % 2 == 0):
            region[3] += 1

        self.kernel_sigma = 0.5
        self.histogram_bins = 8
        self.n_of_particles = 50
        self.enlarge_factor = 2
        self.distance_sigma = 0.11
        self.update_alpha = 0.01
        self.color_change = (True, "YCRCB")
        self.draw_particles = False
        self.dynamic_model = "NCV"

        if self.color_change[0]:
            image = change_colorspace(image, self.color_change[1])

        if len(region) == 8:
            x_ = np.array(region[::2])
            y_ = np.array(region[1::2])
            region = [
                np.min(x_),
                np.min(y_),
                np.max(x_) - np.min(x_) + 1,
                np.max(y_) - np.min(y_) + 1
            ]

        self.window = max(region[2], region[3]) * self.enlarge_factor
        self.position = (region[0] + region[2] / 2, region[1] + region[3] / 2)
        self.size = (region[2], region[3])
        self.template, _ = get_patch(image, self.position, self.size)

        image_pl = image.shape[0] * image.shape[1]
        patch_pl = self.size[0] * self.size[1]

        q = int(patch_pl / image_pl * 200)
        self.q = q if q > 0 else 1
        #self.q = 100

        # CREATING VISUAL MODEL
        self.kernel = create_epanechnik_kernel(self.size[0], self.size[1],
                                               self.kernel_sigma)
        self.patch_size = self.kernel.shape
        self.template_histogram = normalize_histogram(
            extract_histogram(self.template,
                              self.histogram_bins,
                              weights=self.kernel))

        # GENERATING DYNAMIC MODEL MATRICES
        self.system_matrix, self.system_covariance = get_dynamic_model_matrices(
            self.q, self.dynamic_model)
        self.particle_state = [self.position[0], self.position[1]]

        if self.dynamic_model == "NCV":
            self.particle_state.extend([0, 0])
        if self.dynamic_model == "NCA":
            self.particle_state.extend([0, 0, 0, 0])

        # GENERATING N PARTICLES AROUND POSITION
        self.particles = sample_gauss(self.particle_state,
                                      self.system_covariance,
                                      self.n_of_particles)

        self.weights = np.array(
            [1 / self.n_of_particles for _ in range(self.n_of_particles)])
 def epanechnikov(w, h, sigma):
     return ex2_utils.create_epanechnik_kernel(w, h, sigma)