def load_image(image_path, band_indices=None, bit_depth=None, curve_function=None): '''Loads an image into a colorimage. If no bit depth is supplied, 8-bit is assumed. If no band indices are supplied, RGB is assumed.''' im_raster = CImage() im_raster.load(image_path) img, mask = colorimage.convert_to_colorimage(im_raster, band_indices=band_indices, bit_depth=bit_depth, curve_function=curve_function) return img, mask, im_raster
def load_image(image_path, band_indices=None, bit_depth=None, curve_function=None): '''Loads an image into a colorimage. If no bit depth is supplied, 8-bit is assumed. If no band indices are supplied, RGB is assumed.''' im_raster = CImage() im_raster.load(image_path) img, mask = colorimage.convert_to_colorimage(im_raster, band_indices=band_indices, bit_depth=bit_depth, curve_function=curve_function) return img, mask, im_raster
def test_convert_to_colorimage(self): # Create 4-band, 8-bit CImage. Each band has intensity values equal to # the band number test_bands = [ bidx * np.ones((5, 5), dtype=np.uint8) for bidx in range(4) ] test_cimage = io.CImage() test_cimage.bands = test_bands # Trying to load a 4-band image should raise an exception with 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 = [ bidx * np.ones((5, 5), dtype=np.uint8) for bidx in band_indices ] expected_image = np.dstack(expected_bands) np.testing.assert_array_equal(test_colorimage, expected_image) # CImage has no alpha channel so mask should be blank expected_mask = 255 * np.ones((5, 5), dtype=np.uint8) np.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 * np.ones((5, 5), dtype=np.uint8) expected_mask[2, 2] = 0 np.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 * np.ones((5, 5), dtype=np.uint8) \ for i in reversed(band_indices)] expected_image = np.dstack(expected_bands) np.testing.assert_array_equal(test_colorimage, expected_image) # Create 3-band, 16-bit cimage def np_1d_to_3d(np_1d): band = np.reshape(np_1d, (2, 2)) return [band, band, band] test_bands = np_1d_to_3d(np.array([0, 256, 4095, 65535], dtype=np.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 = np_1d_to_3d(np.array([0, 1, 15, 255], dtype=np.uint8)) expected_image = np.dstack(expected) # np.testing.assert_array_equal(test_colorimage, expected_image) expected_mask = 255 * np.ones((2, 2), dtype=np.uint8) # np.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 = np_1d_to_3d(np.array([0, 16, 255, 255], dtype=np.uint8)) expected_image = np.dstack(expected) # np.testing.assert_array_equal(test_colorimage, expected_image) expected_mask = np.array([ [255, 255], [255, 0] ], dtype=np.uint8) # np.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(np.uint8) g = (g/2 ** 8).astype(np.uint8) b = (b/2 ** 8).astype(np.uint8) return r, g, b test_colorimage, _ = colorimage.convert_to_colorimage(test_cimage, curve_function=test_curve) expected_bands = np_1d_to_3d(np.array([0, 1, 15, 255], dtype=np.uint8)) expected_image = np.dstack(expected_bands) np.testing.assert_array_equal(test_colorimage, expected_image)
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)