Esempio n. 1
0
 def testContrastStretchUpperThreshold(self):
     '''test contrast_stretch for contrast stretch from 0 to 90'''
     im = np.arange(10).reshape(2, 5)
     im_stretch = image_functions.contrast_stretch(im, s=[0, 90])
     self.assertTrue(
         np.allclose(im_stretch,
                     np.array([[0, 10, 20, 30, 40], [50, 60, 70, 80, 90]])))
Esempio n. 2
0
 def testContrastStretchLowerUpperThreshold(self):
     '''test contrast_stretch for contrast stretch from 90 to 180'''
     im = np.arange(10).reshape(2, 5)
     im_stretch = image_functions.contrast_stretch(im, s=[90, 180])
     self.assertTrue(
         np.allclose(
             im_stretch,
             np.array([[90, 100, 110, 120, 130], [140, 150, 160, 170,
                                                  180]])))
Esempio n. 3
0
 def testContrastStretchLowerThreshold(self):
     '''test contrast_stretch for contrast stretch from 100 to 255'''
     im = np.arange(10).reshape(2, 5)
     im_stretch = image_functions.contrast_stretch(im, s=[100, 255])
     self.assertTrue(
         np.allclose(
             im_stretch,
             np.array([[100, 1055 / 9, 1210 / 9, 455 / 3, 1520 / 9],
                       [1675 / 9, 610 / 3, 1985 / 9, 2140 / 9, 255]])))
Esempio n. 4
0
 def testContrastStretchFullRange(self):
     '''test contrast_stretch for full contrast stretch from 0 to 255'''
     im = np.arange(10).reshape(2, 5)
     im_stretch = image_functions.contrast_stretch(im)
     self.assertTrue(
         np.allclose(
             im_stretch,
             np.array([[0, 85 / 3, 170 / 3, 85, 340 / 3],
                       [425 / 3, 170, 595 / 3, 680 / 3, 255]])))
Esempio n. 5
0
 def testContrastStretchLimitHistogram(self):
     '''test contrast_stretch for contrast stretch of only part of image
     histogram to full contrast limits'''
     im = np.arange(10).reshape(2, 5)
     im_stretch = image_functions.contrast_stretch(im, s=[0, 255], r=[2, 8])
     self.assertTrue(
         np.allclose(
             im_stretch,
             np.array([[0, 0, 0, 255 / 6, 2 * 255 / 6],
                       [3 * 255 / 6, 4 * 255 / 6, 5 * 255 / 6, 255, 255]])))
Esempio n. 6
0
    def save_img(self, filename, clim=[None, None], cmap=None, **kwargs):
        '''
        Save image
        Filename: string to name the file, include directory path if the 
        current folder is not desired, filetype is given by the ending
        clim: colour limits, if the image should be thresholded to a minimum
        and maximum value. Should be of the form [minimum, maximum]
        cmap: colourmap to apply to the saved image (eg, for grayscale images)
        '''

        if not isstring(filename):
            raise ValueError('Your filename is not a string!')

        if clim[0] is not None:
            r_min = clim[0]
#            r_min = np.maximum(clim[0], np.min(self.data.flatten()))
        else:
            r_min = np.min(self.data.flatten())

        if clim[1] is not None:
            r_max = clim[1]
#            r_max = np.minimum(clim[1], np.max(self.data.flatten()))
        else:
            r_max = np.max(self.data.flatten())
        filename = file_namer.name_file(filename)

        save_im = imfun.contrast_stretch(self.data,
                                         r=[r_min, r_max],
                                         s=[0, 255],
                                         bits=8).astype('uint8')

        if cmap is not None:
            save_im = cmap(save_im)
            #            if np.ma.is_masked(self.data) and np.shape(save_im)[-1] == 4:
            #                print('mask')
            #                save_im[:, :, -1] = save_im[:, :, -1] * self.data.mask.astype(int)
            imfun.write_image(save_im, filename, **kwargs)
        else:
            imfun.write_image(save_im, filename, **kwargs)