Пример #1
0
    def test_create_alpha(self):
        shape = (5, 5)
        expected_alpha = 255 * np.ones((5, 5), dtype=np.uint8)

        alpha_from_shape_cimage = io.CImage()
        alpha_from_shape_cimage.create_alpha(shape)
        np.testing.assert_array_equal(alpha_from_shape_cimage.alpha,
                                      expected_alpha)

        band = np.ones((5, 5), dtype=np.uint8)
        alpha_from_band_cimage = io.CImage()
        alpha_from_band_cimage.bands = [band]
        alpha_from_band_cimage.create_alpha()
        np.testing.assert_array_equal(alpha_from_band_cimage.alpha,
                                      expected_alpha)
Пример #2
0
    def test_load(self):
        input_file = 'test_file.tif'
        self._create_image_with_geotransform(input_file)
        test_cimage = io.CImage()
        test_cimage.load(input_file)

        test_geotransform = test_cimage.metadata['geotransform']
        self.assertEquals(test_geotransform, (-1.0, 1.0, 0.0, 1.0, 0.0, -1.0))

        os.unlink(input_file)
Пример #3
0
    def test_save(self):
        output_file = 'test_file.tif'
        test_band = 255 * np.ones((5, 5), dtype=np.uint8)
        test_cimage = io.CImage()
        test_cimage.bands = [test_band]
        test_cimage.save(output_file)

        test_ds = gdal.Open(output_file)

        saved_number_of_bands = test_ds.RasterCount
        self.assertEquals(saved_number_of_bands, 1)

        saved_band = test_ds.GetRasterBand(1).ReadAsArray()
        np.testing.assert_array_equal(saved_band, test_band)

        os.unlink(output_file)
Пример #4
0
    def test_convert_to_colorimage(self):
        # Create 4-band, 8-bit CImage. Each band has intensity values equal to
        # the band number
        test_bands = [i * numpy.ones((5, 5), dtype=numpy.uint8) \
            for i in range(4)]
        test_cimage = io.CImage()
        test_cimage.bands = test_bands

        # Trying to load a 4-band image should raise an exception
        self.assertRaises(colorimage.ImagePropertyException,
                          colorimage.convert_to_colorimage, test_cimage)

        # Specifying 3 bands should allow the 4-band image to be converted
        band_indices = [0, 2, 3]
        test_colorimage, test_colormask = colorimage.convert_to_colorimage(
            test_cimage, band_indices)

        # The band indices are read in as RGB but the colorimage is BGR
        # so the expected image should have band order that is the reverse
        # order of band_indices.
        expected_bands = [i * numpy.ones((5, 5), dtype=numpy.uint8) \
            for i in reversed(band_indices)]
        expected_image = cv2.merge(expected_bands)
        numpy.testing.assert_array_equal(test_colorimage, expected_image)

        # CImage has no alpha channel so mask should be blank
        expected_mask = 255 * numpy.ones((5, 5), dtype=numpy.uint8)
        numpy.testing.assert_array_equal(test_colormask, expected_mask)

        # Change CImage alpha mask and confirm colorimage mask matches
        test_cimage.create_alpha()
        test_cimage.alpha[2, 2] = 0
        _, test_colormask = colorimage.convert_to_colorimage(
            test_cimage, band_indices=band_indices)
        expected_mask = 255 * numpy.ones((5, 5), dtype=numpy.uint8)
        expected_mask[2, 2] = 0
        numpy.testing.assert_array_equal(test_colormask, expected_mask)

        # Check that curve_function is applied to image
        def test_curve(r, g, b):
            return r / 2, g / 2, b / 2

        test_colorimage, _ = colorimage.convert_to_colorimage(
            test_cimage, band_indices=band_indices, curve_function=test_curve)
        expected_bands = [i/2 * numpy.ones((5, 5), dtype=numpy.uint8) \
            for i in reversed(band_indices)]
        expected_image = cv2.merge(expected_bands)
        numpy.testing.assert_array_equal(test_colorimage, expected_image)

        # Create 3-band, 16-bit cimage
        def numpy_1d_to_3d(numpy_1d):
            band = numpy.reshape(numpy_1d, (2, 2))
            return [band, band, band]

        test_bands = numpy_1d_to_3d(
            numpy.array([0, 256, 4095, 65535], dtype=numpy.uint16))
        test_cimage = io.CImage()
        test_cimage.bands = test_bands

        # No bit depth provided, should assume all 16 bits are used and divide
        # by 256 and mask should be blank
        test_colorimage, test_colormask = colorimage.convert_to_colorimage(
            test_cimage)
        expected = numpy_1d_to_3d(
            numpy.array([0, 1, 15, 255], dtype=numpy.uint8))
        expected_image = cv2.merge(expected)
        numpy.testing.assert_array_equal(test_colorimage, expected_image)

        expected_mask = 255 * numpy.ones((2, 2), dtype=numpy.uint8)
        numpy.testing.assert_array_equal(test_colormask, expected_mask)

        # If bit depth of 12 is provided, the values should be scaled by 2**4
        # and pixels with values above 2**12 should be masked
        test_colorimage, test_colormask = colorimage.convert_to_colorimage(
            test_cimage, bit_depth=2**12)
        expected = numpy_1d_to_3d(
            numpy.array([0, 16, 255, 255], dtype=numpy.uint8))
        expected_image = cv2.merge(expected)
        numpy.testing.assert_array_equal(test_colorimage, expected_image)

        expected_mask = numpy.reshape(
            numpy.array([255, 255, 255, 0], dtype=numpy.uint8), (2, 2))
        numpy.testing.assert_array_equal(test_colormask, expected_mask)

        # Check that curve_function downsampling is applied to bands and avoids
        # further bit decimation
        def test_curve(r, g, b):
            r = (r / 2**8).astype(numpy.uint8)
            g = (g / 2**8).astype(numpy.uint8)
            b = (b / 2**8).astype(numpy.uint8)
            return r, g, b

        test_colorimage, _ = colorimage.convert_to_colorimage(
            test_cimage, curve_function=test_curve)
        expected_bands = numpy_1d_to_3d(
            numpy.array([0, 1, 15, 255], dtype=numpy.uint8))
        expected_image = cv2.merge(expected_bands)
        numpy.testing.assert_array_equal(test_colorimage, expected_image)