示例#1
0
 def test_save_keypoints(self):
     ds.keypoints_to_file(
         self.test_keypoints,
         os.path.join(BASE_PATH, "testdata", "features", "writetest.kps"))
     loaded = ds.keypoints_from_file(
         os.path.join(BASE_PATH, "testdata", "features", "writetest.kps"))
     self.assertEqual(len(loaded), len(self.test_keypoints))
     for k1, k2 in zip(loaded, self.test_keypoints):
         self.assertEqual(k1, k2)
示例#2
0
    def heatmap_data(self, img_name, importances, img_size):
        """Creates a 2-dimensional float array, representing
        the heatmap of importances for the given image.

        The code to calculate a keypoint's environment is based on
        OpenCV's SIFT-implementation.
        Pixels that are contained in the environments of two or
        more distinct keypoints get assigned the maximum of
        the possible importances.

        Args:
            img_name: Filename of the image in question.
            importances: Array of feature importances.
            img_size: Tuple of (width, height) for the image.
        """
        keypoints_file = util.keypoints_name(img_name)
        indices_file = util.cluster_name(img_name)
        keypoints = ds.keypoints_from_file(os.path.join(self.datamanager.PATHS["KEYPOINTS"], keypoints_file))
        indices = [index[0] for index in ds.load_matrix(os.path.join(self.datamanager.PATHS["BOW"], indices_file))]

        assert len(keypoints) == len(indices), "Should be %d, but is %d" % (len(keypoints), len(indices))

        heatmap = np.zeros(img_size[0:2])
        rows = heatmap.shape[0]
        cols = heatmap.shape[1]

        for kp, index in zip(keypoints, indices):
            cosine = math.cos(math.radians(kp.angle)) / HIST_WIDTH
            sine = math.sin(math.radians(kp.angle)) / HIST_WIDTH

            for i in range(-RADIUS, RADIUS + 1):
                for j in range(-RADIUS, RADIUS + 1):
                    r = kp.y + i
                    c = kp.x + j

                    c_rot = j * cosine - i * sine
                    r_rot = j * sine + i * cosine;
                    rbin = r_rot + HIST_ARRAY_WIDTH/2 - 0.5
                    cbin = c_rot + HIST_ARRAY_WIDTH/2 - 0.5

                    if (-1 < rbin < HIST_ARRAY_WIDTH) and (-1 < cbin < HIST_ARRAY_WIDTH) and (0 <= r < rows) and (0 <= c < cols):
                        heatmap[r, c] = self.absmax(heatmap[r, c], importances[index])
        return heatmap
示例#3
0
 def test_save_keypoints(self):
     ds.keypoints_to_file(self.test_keypoints, os.path.join(BASE_PATH, "testdata", "features", "writetest.kps"))
     loaded = ds.keypoints_from_file(os.path.join(BASE_PATH, "testdata", "features", "writetest.kps"))
     self.assertEqual(len(loaded), len(self.test_keypoints))
     for k1, k2 in zip(loaded, self.test_keypoints):
         self.assertEqual(k1, k2)