def test_create_mask(self): test_band = np.array([ [1, 2], [3, 4] ], dtype=np.uint8) # Mask should be the same shape as band with all locations unmasked test_mask = mask.create_mask(test_band) expected_mask = np.array([ [255, 255], [255, 255] ], dtype=np.uint8) np.testing.assert_array_equal(test_mask, expected_mask) # Mask should be the same shape as band with location corresponding # to 3 masked test_mask = mask.create_mask(test_band, value=3) expected_mask = np.array([ [255, 255], [0, 255] ], dtype=np.uint8) np.testing.assert_array_equal(test_mask, expected_mask) # Mask should be the same shape as band with all locations unmasked # because the band contains no pixels with intensity equal to 'value' test_mask = mask.create_mask(test_band, value=6) expected_mask = np.array([ [255, 255], [255, 255] ], dtype=np.uint8) np.testing.assert_array_equal(test_mask, expected_mask)
def convert_to_colorimage(cimage, band_indices=None, bit_depth=None, curve_function=None): """ Creates a 3-band, 8-bit BGR colorimage from the bands of the input RGB CImage. If band indices are provided, they are used to identify the red, green, and blue bands of the CImage, respectively. If the CImage is 16-bit, the intensity values of the bands are scaled to 8-bit. In this case, the bit depth is assumed to be 16 unless bit depth is provided. If the CImage has an alpha channel, it is converted to the colorimage mask. :param CImage cimage: CImage to be converted :param list band_indices: ordered list of indices for red, green, and blue channels :param int bit_depth: effective bit depth, if less than actual CImage bit depth :param function curve_function: function for applying a color curve to the RGB bands :returns: colorimage (3-band, 8-bit BRG openCV array), mask (1-band, 8-bit array where 0 corresponds to masked pixels and 255 corresponds to unmasked pixels), image (same as input CImage) """ if band_indices is None: if len(cimage.bands) != 3: raise ImagePropertyException("CImage is not 3-band") band_indices = [0, 1, 2] else: assert len(band_indices) == 3 assert max(band_indices) < len(cimage.bands) assert min(band_indices) >= 0 # Make a mask from the CImage alpha channel if cimage.alpha is not None: cmask = cimage.alpha.astype(np.uint8) else: cmask = mask.create_mask(cimage.bands[0]) bands = cimage.bands # Apply curve function to RGB image if curve_function is not None: bands = curve_function(*bands) # Scale the band intensities and mask out-of-range pixels out_bands = [] for band in bands: out_bands.append(band) # Mask out values above or below the 8-bit extents # This is only necessary if bit depth is given clip_max = band > 255 cmask[clip_max] = 0 clip_min = band < 0 cmask[clip_min] = 0 cimg = np.dstack(out_bands[::-1]) return cimg, cmask
def test_create_mask(self): test_band = numpy.array([[1, 2], [3, 4]], dtype=numpy.uint8) # Mask should be the same shape as band with all locations unmasked test_mask = mask.create_mask(test_band) expected_mask = numpy.array([[255, 255], [255, 255]], dtype=numpy.uint8) numpy.testing.assert_array_equal(test_mask, expected_mask) # Mask should be the same shape as band with location corresponding # to 3 masked test_mask = mask.create_mask(test_band, value=3) expected_mask = numpy.array([[255, 255], [0, 255]], dtype=numpy.uint8) numpy.testing.assert_array_equal(test_mask, expected_mask) # Mask should be the same shape as band with all locations unmasked # because the band contains no pixels with intensity equal to 'value' test_mask = mask.create_mask(test_band, value=6) expected_mask = numpy.array([[255, 255], [255, 255]], dtype=numpy.uint8) numpy.testing.assert_array_equal(test_mask, expected_mask)
def convert_to_colorimage(cimage, band_indices=None, bit_depth=None, curve_function=None): '''Creates a 3-band, 8-bit BGR colorimage from the bands of the input RGB CImage. If band indices are provided, they are used to identify the red, green, and blue bands of the CImage, respectively. If the CImage is 16-bit, the intensity values of the bands are scaled to 8-bit. In this case, the bit depth is assumed to be 16 unless bit depth is provided. If the CImage has an alpha channel, it is converted to the colorimage mask. :param CImage cimage: CImage to be converted :param list band_indices: ordered list of indices for red, green, and blue channels :param int bit_depth: effective bit depth, if less than actual CImage bit depth :param function curve_function: function for applying a color curve to the RGB bands :returns: colorimage (3-band, 8-bit BRG openCV array), mask (1-band, 8-bit array where 0 corresponds to masked pixels and 255 corresponds to unmasked pixels), image (same as input CImage) ''' if band_indices is None: if len(cimage.bands) != 3: raise ImagePropertyException("CImage is not 3-band") band_indices = [0, 1, 2] else: assert len(band_indices) == 3 assert max(band_indices) < len(cimage.bands) assert min(band_indices) >= 0 # Make a mask from the CImage alpha channel if cimage.alpha is not None: cmask = cimage.alpha.astype(numpy.uint8) else: cmask = mask.create_mask(cimage.bands[0]) bands = [] for i in band_indices: bits16 = gdal_array.NumericTypeCodeToGDALTypeCode( cimage.bands[i].dtype.type) != gdal.GDT_Byte # Make RGB band image ntype = numpy.uint16 if bits16 else numpy.uint8 cimage.bands[i].astype(ntype).dtype bands.append(cimage.bands[i].astype(ntype)) # Apply curve function to RGB image if curve_function is not None: bands = curve_function(*bands) # Scale the band intensities and mask out-of-range pixels out_bands = [] for band in bands: # If 16-bit, convert to 8-bit, assuming 16-bit image is using the # entire bit depth unless bit depth is given if band.dtype == numpy.uint16: if bit_depth is not None: band = (band / (bit_depth / 2**8)) else: band = (band / 2**8) elif band.dtype != numpy.uint8: raise ImagePropertyException( "Band type is {}, should be {} or {}".format( band.dtype, numpy.uint8, numpy.uint16)) # Mask out values above or below the 8-bit extents # This is only necessary if bit depth is given clip_max = band > 255 cmask[clip_max] = 0 clip_min = band < 0 cmask[clip_min] = 0 out_bands.append(band.astype(numpy.uint8)) cimg = rgb_bands_to_colorimage(out_bands) return cimg, cmask