Exemple #1
0
    def test_extracts_mean_value(self):

        img1 = np.zeros((64, 128))
        img1[24:48, 65:100] = 1
        img1[36:48, 65:100] = 2

        extractor = poly.GridValueExtractor(np.arange(128), np.arange(64))
        extractor.add_image(img1)

        coords = np.array([
            [0.0, 0.0],
            [5.0, 0.0],
            [5.0, 5.0],
            [0.0, 5.0],
        ])
        values = extractor.extract_values(coords)
        np.testing.assert_almost_equal(values, [0.0])

        coords = np.array([
            [65.1, 24.1],
            [65.1, 46.9],
            [99.9, 46.9],
            [99.9, 24.1],
        ])
        values = extractor.extract_values(coords)
        np.testing.assert_almost_equal(values, [1.5])
Exemple #2
0
    def test_equivalent_but_faster(self):

        img1 = np.random.rand(64, 128)
        img2 = np.random.rand(64, 128)

        x = np.arange(64)
        y = np.arange(128)

        extractor = poly.GridValueExtractor(y, x)
        extractor.add_image(img1)
        extractor.add_image(img2)

        coords = np.array([
            [0.0, 0.0],
            [10, 0.0],
            [10, 10],
            [20, 10],
            [20, 20],
            [0.0, 20.0],
        ])
        mask = poly.mask_in_polygon(coords, y, x)

        exp1 = np.mean(img1[mask])
        exp2 = np.mean(img2[mask])

        res1, res2 = extractor.extract_values(coords)

        self.assertAlmostEqual(res1, exp1, places=4)
        self.assertAlmostEqual(res2, exp2, places=4)

        coords = np.array([
            [70.0, 0.0],
            [80.0, 0.0],
            [80.0, 10.0],
            [90.0, 10.0],
            [90.0, 20.0],
            [70.0, 20.0],
        ])
        mask = poly.mask_in_polygon(coords, y, x)

        exp1 = np.mean(img1[mask])
        exp2 = np.mean(img2[mask])

        res1, res2 = extractor.extract_values(coords)

        self.assertAlmostEqual(res1, exp1, places=4)
        self.assertAlmostEqual(res2, exp2, places=4)
Exemple #3
0
    def test_extract_outside_image_default_value(self):

        img1 = np.zeros((64, 128))
        img1[24:48, 65:100] = 1
        img1[36:48, 65:100] = 2

        extractor = poly.GridValueExtractor(np.arange(128), np.arange(64))
        extractor.add_image(img1)

        coords = np.array([
            [-2, 0],
            [-1, 0],
            [-1, -1],
            [-2, -1],
        ])
        values = extractor.extract_values(coords)
        np.testing.assert_almost_equal(values, [np.nan])

        values = extractor.extract_values(coords, fill_value=-1)
        np.testing.assert_almost_equal(values, [-1])
Exemple #4
0
    def test_can_extract_different_values(self):

        img1 = np.zeros((64, 128))
        img1[24:48, 65:100] = 1
        img1[36:48, 65:100] = 2

        extractor = poly.GridValueExtractor(np.arange(128), np.arange(64))
        extractor.add_image(img1)

        coords = np.array([
            [65.1, 24.1],
            [65.1, 46.9],
            [99.9, 46.9],
            [99.9, 24.1],
        ])
        values = extractor.extract_values(coords, func=np.min)
        np.testing.assert_almost_equal(values, [1.0])

        values = extractor.extract_values(coords, func=np.max)
        np.testing.assert_almost_equal(values, [2.0])
Exemple #5
0
    def test_extracts_equivalent_mask_right_side(self):

        x = np.arange(64)
        y = np.arange(128)

        extractor = poly.GridValueExtractor(y, x)

        coords = np.array([
            [70.0, 0.0],
            [80.0, 0.0],
            [80.0, 10.0],
            [90.0, 10.0],
            [90.0, 20.0],
            [70.0, 20.0],
        ])
        mask1 = poly.mask_in_polygon(coords, y, x)
        mask2 = extractor.extract_mask(coords)

        self.assertTrue(np.any(mask1))
        self.assertTrue(np.any(mask2))
        self.assertTrue(np.all(mask1 == mask2))
Exemple #6
0
    def test_segment_and_extract_values(self):

        img1 = np.zeros((64, 128))
        img1[32, 72] = 50
        img1[48, 12] = 100
        img1[24, 32] = 25
        img1[32, 24] = 75

        extractor = poly.GridValueExtractor(np.arange(128), np.arange(64))
        extractor.add_image(img1)

        coords = extractor.segment_peaks(img1)

        exp_coords = np.array([
            [12, 48],
            [24, 32],
            [72, 32],
            [32, 24],
        ])
        np.testing.assert_almost_equal(coords, exp_coords)

        values = extractor.extract_points(coords)[0]
        np.testing.assert_almost_equal(values, [100, 75, 50, 25])
Exemple #7
0
    def test_segment_and_extract_masks(self):

        img1 = np.zeros((64, 128))
        img1[32, 72] = 50
        img1[48, 12] = 100
        img1[24, 32] = 25
        img1[32, 24] = 75

        extractor = poly.GridValueExtractor(np.arange(128), np.arange(64))
        extractor.add_image(img1)

        coords = extractor.segment_circles(img1, radius=5)

        # Make sure the circles are centered where they should be
        exp_centers = [
            np.array([12, 48]),
            np.array([24, 32]),
            np.array([72, 32]),
            np.array([32, 24]),
        ]
        exp_values = [100, 75, 50, 25]

        self.assertEqual(len(coords), len(exp_centers))

        for coord, exp_center in zip(coords, exp_centers):
            self.assertEqual(coord.shape, (50, 2))

            res_center = np.mean(coord, axis=0)
            np.testing.assert_almost_equal(res_center, exp_center)

        # Make sure extracting the circles gives the peak value
        self.assertEqual(len(coords), len(exp_values))
        for coord, exp_value in zip(coords, exp_values):
            max_value = extractor.extract_values(coord, func=np.max)[0]

            self.assertEqual(max_value, exp_value)