Exemplo n.º 1
0
def test_classify_image():

    # create mineral classifier instance
    mc = mineral.MineralClassification(library_file_name=libraryFilenames[0])

    # for each of the test images
    for image_file_name in test_classifyImage_testFilenames:

        # classify the test image
        classified_file_name = image_file_name[:-4] + "_class_test.hdr"
        mc.classify_image(image_file_name, classified_file_name)
        actual = spectral.open_image(classified_file_name)

        # classified image for comparison
        expected = spectral.open_image(image_file_name[:-4] + "_class.hdr")

        # validate metadata
        assert actual.metadata.get(
            u'description'
        ) == 'COAL ' + pycoal.version + ' mineral classified image.'
        assert expected.metadata.get(u'file type') == actual.metadata.get(
            u'file type')
        assert expected.metadata.get(u'map info') == actual.metadata.get(
            u'map info')
        assert expected.metadata.get(u'class names') == actual.metadata.get(
            u'class names')
        assert expected.metadata.get(u'classes') == actual.metadata.get(
            u'classes')

        # verify that every pixel has the same classification
        assert numpy.array_equal(expected.asarray(), actual.asarray())
Exemplo n.º 2
0
def test_to_rgb_no_data():
    mineral.MineralClassification.to_rgb(test_toRGB_noData_imageFilename,
                                         test_toRGB_noData_testFilename)
    expected = spectral.open_image(test_toRGB_noData_rgbFilename)
    actual = spectral.open_image(test_toRGB_noData_testFilename)
    assert actual.metadata.get('data ignore value') == '0'
    assert numpy.array_equal(expected.asarray(), actual.asarray())
Exemplo n.º 3
0
def test_classify_image_threshold():

    # create mineral classification instance with threshold
    mc = mineral.MineralClassification(library_file_name=libraryFilenames[0],
                                       threshold=0.75)

    # classify image
    mc.classify_image(test_classifyImage_threshold_subset_imageFilename, \
                     test_classifyImage_threshold_subset_testFilename)
    actual = spectral.open_image(
        test_classifyImage_threshold_subset_testFilename)

    # compare expected to actual classifications
    expected = spectral.open_image(
        test_classifyImage_threshold_subset_classifiedFilename)
    for x in range(actual.shape[0]):
        for y in range(actual.shape[1]):
            actual_class_id = actual[x, y, 0]
            actual_class_name = actual.metadata.get(
                'class names')[actual_class_id]
            expected_class_id = expected[x, y, 0]
            expected_class_name = expected.metadata.get(
                'class names')[expected_class_id]
            assert actual_class_name == expected_class_name \
                or actual_class_name == 'No data'
Exemplo n.º 4
0
    def extract(self):

        if self._img_source == "":
            print('Please give path to extracted crops directory.\n')
        if self._save_path == "":
            print('Please give path to save tranin')

        crop_list = sorted(glob.glob(self._img_source + '*.hdr'))
        crop_names = list()

        print(
            '\n------------------Preparing training dataset---------------------\n\n'
        )

        print('Total {} crops are found in directory\n\n'.format(
            len(crop_list)))

        fw_w = open(self._save_path, 'w')

        for index, path in enumerate(crop_list):

            crop_name = path.split('/')[-1].split('.')[0]

            crop_names.append(crop_name)

            if self._noisy_bands is not None:

                img = open_image(path)
                all_bands = set(np.arange(img.nbands))
                img = img[:, :, list(all_bands - set(self._noisy_bands))]

            else:

                img = open_image(path)

            height = img.shape[0]
            width = img.shape[1]

            count = 0

            for i in range(height):
                for j in range(width):
                    temp = img[i, j]
                    if (np.mean(temp) > 0):
                        count += 1
                        for band in range(temp.size):
                            fw_w.write(str(temp[band]) + ",")
                        fw_w.write(str(index + 1) + '\n')
            print(
                "Crop No. : {} \t| Name : {}  \t\t| Tota samples : {}".format(
                    index + 1, crop_name, count))

        print('\n\nProcess completed. File saved at : ', self._save_path)

        print('\n\nCrop List : ', crop_names)
        print(
            '\n\n---------------------------------------------------------\n\n'
        )

        fw_w.close()
def loadHSIData():
    data_path = os.path.join(os.getcwd(), 'HSI_data')
    data = spectral.open_image(os.path.join(data_path, '92AV3C.lan')).load()
    data = np.array(data).astype(np.int32)
    labels = spectral.open_image(os.path.join(data_path, '92AV3GT.GIS')).load()
    labels = np.array(labels).astype(np.uint8)
    labels.shape = (145, 145)
    return data, labels
Exemplo n.º 6
0
 def test_save_load_classes(self):
     '''Verify that `envi.save_classification` saves data correctly.'''
     import spectral as spy
     fname = os.path.join(testdir, 'test_save_load_classes.hdr')
     gt = spy.open_image('92AV3GT.GIS').read_band(0)
     spy.envi.save_classification(fname, gt, dtype=np.uint8)
     gt2 = spy.open_image(fname).read_band(0)
     assert (np.all(gt == gt2))
Exemplo n.º 7
0
 def test_save_load_classes(self):
     '''Verify that `envi.save_classification` saves data correctly.'''
     import spectral as spy
     fname = os.path.join(testdir, 'test_save_load_classes.hdr')
     gt = spy.open_image('92AV3GT.GIS').read_band(0)
     spy.envi.save_classification(fname, gt, dtype=np.uint8)
     gt2 = spy.open_image(fname).read_band(0)
     assert(np.all(gt == gt2))
Exemplo n.º 8
0
 def setup(self):
     if not os.path.isdir(testdir):
         os.mkdir(testdir)
     self.image = spy.open_image('92AV3C.lan')
     self.data = self.image.load()
     self.gt = spy.open_image('92AV3GT.GIS').read_band(0)
     self.ts = spy.create_training_classes(self.data, self.gt,
                                           calc_stats=True)
     self.class_filename = os.path.join(testdir, '92AV3C.classes')
Exemplo n.º 9
0
def test_filter_classes():
    mineral.MineralClassification.filter_classes(test_filterClasses_testFilename)
    original = spectral.open_image(test_filterClasses_Filename)
    filtered = spectral.open_image(test_filterClasses_testFilename)
    assert int(filtered.metadata.get('classes')) == len(set(original.asarray().flatten().tolist()))
    for x in range(original.shape[0]):
        for y in range(original.shape[1]):
            original_class_name = original.metadata.get('class names')[original[x,y,0]]
            filtered_class_name = filtered.metadata.get('class names')[filtered[x,y,0]]
            assert original_class_name == filtered_class_name
Exemplo n.º 10
0
 def setup(self):
     if not os.path.isdir(testdir):
         os.mkdir(testdir)
     self.image = spy.open_image('92AV3C.lan')
     self.data = self.image.load()
     self.gt = spy.open_image('92AV3GT.GIS').read_band(0)
     self.ts = spy.create_training_classes(self.data,
                                           self.gt,
                                           calc_stats=True)
     self.class_filename = os.path.join(testdir, '92AV3C.classes')
Exemplo n.º 11
0
 def test_save_image_spyfile(self):
     '''Test saving an ENVI formatted image from a SpyFile object.'''
     import os
     import spectral
     (r, b, c) = (3, 8, 23)
     fname = os.path.join(testdir, 'test_save_image_spyfile.hdr')
     src = spectral.open_image('92AV3C.lan')
     spectral.envi.save_image(fname, src)
     img = spectral.open_image(fname)
     assert_almost_equal(src[r, b, c], img[r, b, c])
Exemplo n.º 12
0
 def test_save_image_spyfile(self):
     '''Test saving an ENVI formatted image from a SpyFile object.'''
     import os
     import spectral
     (r, b, c) = (3, 8, 23)
     fname = os.path.join(testdir, 'test_save_image_spyfile.hdr')
     src = spectral.open_image('92AV3C.lan')
     spectral.envi.save_image(fname, src)
     img = spectral.open_image(fname)
     assert_almost_equal(src[r, b, c], img[r, b, c])
Exemplo n.º 13
0
def test_to_rgb_avc():
    mineral.MineralClassification.to_rgb(test_toRGB_AVC_imageFilename, test_toRGB_AVC_testFilename)
    expected = spectral.open_image(test_toRGB_AVC_rgbFilename)
    actual = spectral.open_image(test_toRGB_AVC_testFilename)
    assert numpy.array_equal(expected.asarray(), actual.asarray())
    assert expected.metadata.get('wavelength') == actual.metadata.get('wavelength')
    assert expected.metadata.get('correction factors') == actual.metadata.get('correction factors')
    assert expected.metadata.get('fwhm') == actual.metadata.get('fwhm')
    assert expected.metadata.get('bbl') == actual.metadata.get('bbl')
    assert expected.metadata.get('smoothing factors') == actual.metadata.get('smoothing factors')
Exemplo n.º 14
0
 def create_test_image_file(self):
     import os
     import spectral
     img = spy.open_image(self.file)
     fname = os.path.join(testdir, 'memmap_test_%s.hdr' % self.src_inter)
     spy.envi.save_image(fname,
                         img,
                         dtype=img.dtype,
                         interleave=self.src_inter,
                         force=True)
     self.image = spy.open_image(fname)
Exemplo n.º 15
0
def test_classify_image():
    # classify mining and and save to temporary file
    mc = pycoal.mining.MiningClassification()
    mc.classify_image(mineral_file_name, test_file_name, spectral_version)

    # open the mining and temporary files
    expected = spectral.open_image(mining_file_name)
    actual = spectral.open_image(test_file_name)

    # verify that every pixel has the same classification
    assert numpy.array_equal(expected.asarray(), actual.asarray())
Exemplo n.º 16
0
 def create_test_image_file(self):
     import os
     import spectral
     img = spectral.open_image(self.file)
     fname = os.path.join(testdir, 'memmap_test_%s.hdr' % self.src_inter)
     spectral.envi.save_image(fname,
                              img,
                              dtype = img.dtype,
                              interleave = self.src_inter,
                              force=True)
     self.image = spectral.open_image(fname)
Exemplo n.º 17
0
def test_classify_image_joblib():
    # use our test config file with algo set to joblib
    config = 'test_config_files/config_test_joblib.ini'

    # create mineral classifier instance
    mc = mineral.MineralClassification(
        config_file=config, library_file_name=test.libraryFilenames[0])

    # for each of the test images
    for image_file_name in test_classifyImage_testFilenames:
        # classify the test image
        classified_file_name = image_file_name[:-4] + "_class_test.hdr"
        mc.classify_image(image_file_name, classified_file_name)
        actual = spectral.open_image(classified_file_name)

        # classified image for comparison
        expected = spectral.open_image(image_file_name[:-4] + "_class.hdr")

        # validate metadata
        assert actual.metadata.get(
            u'description') == 'COAL ' + pycoal.version + ' mineral ' \
                                                          'classified image.'
        assert expected.metadata.get(u'file type') == actual.metadata.get(
            u'file type')
        assert expected.metadata.get(u'map info') == actual.metadata.get(
            u'map info')
        assert expected.metadata.get(u'class names') == actual.metadata.get(
            u'class names')
        assert expected.metadata.get(u'classes') == actual.metadata.get(
            u'classes')

        # verify that every pixel has the same classification
        assert numpy.array_equal(expected.asarray(), actual.asarray())

    # create mineral classifier instance with image loading enabled
    mc = mineral.MineralClassification(
        config_filename=config,
        library_file_name=test.libraryFilenames[0],
        in_memory=True)

    # for each of the test images
    for image_file_name in test_classifyImage_testFilenames:
        # classify the test image
        classified_file_name = image_file_name[:-4] + "_class_test.hdr"
        mc.classify_image(image_file_name, classified_file_name)
        actual = spectral.open_image(classified_file_name)

        # classified image for comparison
        expected = spectral.open_image(image_file_name[:-4] + "_class.hdr")

        # verify that every pixel has the same classification
        assert numpy.array_equal(expected.asarray(), actual.asarray())
Exemplo n.º 18
0
def test_intersect_proximity():
    ec = environment.EnvironmentalCorrelation()
    ec.intersect_proximity(mining_filename, vector_filename, proximity,
                           test_filename)
    expected = spectral.open_image(correlated_filename)
    actual = spectral.open_image(test_filename)
    assert numpy.array_equal(expected.asarray(), actual.asarray())
    assert actual.metadata.get(
        'description') == 'COAL ' + pycoal.version + ' environmental ' \
                                                     'correlation image.'
    assert expected.metadata.get('class names') == actual.metadata.get(
        'class names')
    assert expected.metadata.get('map info') == actual.metadata.get('map info')
 def get_data(self, swath):
     data_file = os.path.expanduser("{}/{}".format(
         self.dir,
         self.data_file.format(swath) if swath else self.data_file))
     print(data_file)
     assert os.path.exists(data_file)
     if self.name == 'indian_pines':
         self.data = sio.loadmat(data_file)[self.data_key]
     elif self.name == 'acadia':
         self.data = spectral.open_image(data_file)[:, :, :]
     else:
         # Keep only a subset of the rows that overlaps the ground truth
         self.data = spectral.open_image(data_file)[2000:3500, :, :]
Exemplo n.º 20
0
    def create_empty_copy(self, source_filename, destination_filename):
        """
        Create an empty copy of a COAL classified image with the same size.

        Args:
            source_filename (str):      filename of the source image
            destination_filename (str): filename of the destination image
        """
        logging.info(
            "Creating an empty copy of classified image '%s' with the same size. Saving to '%s'"
            % (source_filename, destination_filename))
        # open the source image
        source = spectral.open_image(source_filename)

        # create an empty array of the same dimensions
        destination = numpy.zeros(shape=source.shape, dtype=numpy.uint16)

        # save it with source metadata
        spectral.io.envi.save_classification(
            destination_filename,
            destination,
            class_names=['No data', 'Data'],
            metadata={
                'data ignore value': 0,
                'map info': source.metadata.get('map info')
            })
Exemplo n.º 21
0
 def train_with_threshold_hyperspectral(self, threshold, path):
     filenames = [join(path, name) for name in os.listdir(path)]
     centroids = np.zeros((1, self.input_dim))
     for filename in filenames:
         if filename.find(".lan") == -1:  #hdr
             continue
         #img = envi.open(filename)
         img = open_image(filename)
         if img.shape[2] != self.input_dim:
             continue
         for i in self.generator_image(img.shape[0], img.shape[1]):
             input_vec = img[i]
             if (np.min(
                     np.linalg.norm(input_vec -
                                    centroids[range(centroids.shape[0])])) >
                     threshold):
                 np.append(centroids, input_vec)
     for iter in range(self._learn_iterations):
         if (iter % 1000 == 0):
             print("Iteration: " + str(iter // 1000) + " / " +
                   str(self._learn_iterations // 1000))
         for input_vec in centroids:
             self._sess.run(self._training_op,
                            feed_dict={
                                self._input_vec: input_vec,
                                self._learning_iteration: iter
                            })
     print("training done")
Exemplo n.º 22
0
def get_hyper_labels(hyper_image, forest_labels, hyper_gt, forest_gt, should_remove_bad_data):
    """
    Create hyperspectral labels from forest data
    :param hyper_image: W1xH1xC
    :param forest_labels: W2xH2xB
    :param hyper_gt
    :param forest_gt
    :param should_remove_bad_data
    :return:
    """
    rows, cols, _ = hyper_image.shape

    c, r = hyp2for((0, 0), hyper_gt, forest_gt)
    hyper_labels = np.array(forest_labels[r:(r+rows), c:(c+cols)])

    if should_remove_bad_data:
        stand_id_data = spectral.open_image(datadir+'/standids_in_pixels.hdr')
        stand_ids_full = stand_id_data.open_memmap()
        stand_ids_mapped = np.array(stand_ids_full[r:(r + rows), c:(c + cols)], dtype='int')  # shape RxCx1
        stand_ids_mapped = np.squeeze(stand_ids_mapped)  # remove the single-dimensional entries, size RxC
        bad_stand_df = pd.read_csv(datadir+'/bad_stands.csv')
        bad_stand_list = bad_stand_df['standid'].tolist()
        for stand_id in bad_stand_list:
            hyper_labels[stand_ids_mapped == stand_id] = 0

    return hyper_labels
Exemplo n.º 23
0
 def train(self, threshold, path):
         filenames = [join(path, name) for name in os.listdir(path)]
         centroids = np.zeros((0, self.input_dim))
         for filename in filenames:
             if filename.find(".lan") > 0:
                 img = open_image(filename)
             elif filename.find(".hdr") > 0:
                 img = envi.open(filename)
             elif filename.find(".mat") > 0:
                 img_mat = loadmat(filename)
                 for i in img_mat:
                     img = img_mat[i]
             # cv2 dodaj dla rgb
             else:
                 continue
             if img.shape[2] != self.input_dim:
                 continue
             for i in self.generator_image(img.shape[0], img.shape[1]):
                 input_vec = self.create_input_vec(img, i)
                 if (np.min(np.linalg.norm(input_vec - centroids[range(centroids.shape[0])])) > threshold):
                     np.append(centroids, input_vec)
         for iter in range(self._learn_iterations):
             if (iter%10000 == 0):
                 print("Training: " + str(100.0 * iter//self._learn_iterations) + "%")
             for input_vec in centroids:
                 self._sess.run(self._training_op, feed_dict={self._input_vec: input_vec, self._learning_iteration: iter})
             np.random.shuffle(centroids)
         print("training done")
Exemplo n.º 24
0
def vegetation_mask(root_dir, filename):
    dirname = os.path.join(root_dir, filename, "reflectance/")
    fpath = os.path.join(dirname, filename + "reflectance-crop.hdr")

    img_obj = spectral.open_image(fpath)
    img = img_obj.open_memmap(writable=True)

    img_band_obj = img_obj.bands
    img_bandcenters_array = np.array(img_band_obj.centers)

    red_band_idx = np.argmin(np.absolute(img_bandcenters_array - 670.0))
    nir_band_idx = np.argmin(np.absolute(img_bandcenters_array - 800.0))

    red_image = img_obj.read_band(red_band_idx)
    nir_image = img_obj.read_band(nir_band_idx)

    NDVI_image = (nir_image - red_image) / (nir_image + red_image)
    #subset image based on NDVI threshold of 0.5
    img[NDVI_image < 0.5] = np.nan

    #save image in new folder called veg-extract
    if not os.path.isdir(dirname + "veg-extract"):
        os.makedirs(dirname + "veg-extract")
    envi.save_image(os.path.join(dirname, "veg-extract/",
                                 filename + "NDVI05.hdr"),
                    img,
                    force=True,
                    dtype=np.float32)
    get_header_file_radiance_conv(
        fpath, os.path.join(dirname, "veg-extract", filename + "NDVI05.hdr"))
Exemplo n.º 25
0
def load_image(source, scale=1, gray=False, memory=Memory(cachedir=None)):
    data_dir = get_data_dirs()[0]
    if source == 'face':
        image = face(gray=gray)
        image = image.astype(np.float32) / 255
        if image.ndim == 2:
            image = image[..., np.newaxis]
        if scale != 1:
            image = memory.cache(rescale)(image, scale=scale)
        return image
    elif source == 'lisboa':
        image = imread(join(data_dir, 'images', 'lisboa.jpg'), as_grey=gray)
        image = image.astype(np.float32) / 255
        if image.ndim == 2:
            image = image[..., np.newaxis]
        if scale != 1:
            image = memory.cache(rescale)(image, scale=scale)
        return image
    elif source == 'aviris':
        image = open_image(
            join(
                data_dir, 'aviris', 'f100826t01p00r05rdn_b/'
                'f100826t01p00r05rdn_b_sc01_ort_img.hdr'))
        image = np.array(image.open_memmap(), dtype=np.float32)
        good_bands = list(range(image.shape[2]))
        good_bands.remove(110)
        image = image[:, :, good_bands]
        indices = image == -50
        image[indices] = -1
        image[~indices] -= np.min(image[~indices])
        image[~indices] /= np.max(image[~indices])
        return image
    else:
        raise ValueError('Data source is not known')
Exemplo n.º 26
0
def crop_save_images(image_path, output_dir, crop_size, stride):
    # read the image
    if image_path[-3:] == 'tif':
        image_array = tifffile.imread(image_path)
        if len(image_array.shape) == 3:
            image_array = image_array.transpose(2,0,1)
        else:
            image_array = image_array[np.newaxis, :, :]
    elif image_path[-3:] in ['hdr', 'HDR']:
        img = spectral.open_image(image_path)
        image_array = img.load()
        image_array = image_array.transpose(2,0,1)
    else:
        img = Image.open(image_path)
        image_array = np.array(img)
        if len(image_array.shape) == 2:
            image_array = np.expand_dims(image_array, axis=-1)
            image_array = image_array.transpose(2,0,1)

    # get shape of image
    (c, h, w) = image_array.shape

    # compute the window number
    X = (w - crop_size)//stride + 1
    Y = (h - crop_size)//stride + 1
    for j in tqdm(range(Y)):
        for i in range(X):
            y = j * stride
            x = i * stride
            patch_array = image_array[:, y: y + crop_size, x: x + crop_size]
            output_image_name = image_path[:-4] + '_{}_{}'.format(str(j), str(i))
            output_path = os.path.join(output_dir, output_image_name.split('/')[-1])
            np.save(output_path, patch_array)
 def setup(self):
     import spectral
     from spectral.io.spyfile import SpyFile
     if isinstance(self.file, SpyFile):
         self.image = self.file
     else:
         self.image = spectral.open_image(self.file)
Exemplo n.º 28
0
    def buttondatafile( self ):
        """
        get data file name and load metadata
        """
        # reset openfilelist and other environment as if no data file were loaded
        self.hypdata_loaded = False
        self.openfilelist = []
#        self.button_datafile.configure( background='SystemButtonFace' )
        self.button_datafile.configure()
        self.button_run.configure( state=DISABLED )
        self.button_p.configure( state=DISABLED )
        self.filename2 = ""
        
        self.filename2 =  filedialog.askopenfilename(initialdir = self.hypdatadir, title = "Hyperspectal data file", filetypes = (("ENVI header files","*.hdr"),("all files","*.*")))
        
        if self.filename2 != "" :
            # open the data file -- reads only metadata
            self.hypdata = spectral.open_image(self.filename2)
            # hypdata.metadata is of type dict, use e.g. hypdata.metadata.keys()
            # print(self.hypdata.metadata.keys())
            if 'wavelength' in self.hypdata.metadata.keys():
                # wavelengths should be in metadata
                # these will be stored in the class for other functions to use (interpolation and plotting of reference data)
                self.wl_hyp = np.array(self.hypdata.metadata['wavelength'],dtype='float')
                if self.wl_hyp.max() < 100:
                    # in microns, convert to nm
                    self.wl_hyp *= 1000
                
                if self.hypdata.interleave == 1:
                    print("Band interleaved (BIL)")
                else:
                    print( self.filename2 + " not BIL -- opening still as BIL -- will be slower" )
                self.hypdata_map = self.hypdata.open_memmap()
                print("dimensions ", self.hypdata_map.shape) #shape[0]==lines, shape[1]==pixels, shape[2]==bands
                
                # save the handles to the 0th element of openfilelist
                self.openfilelist = [ [ self.filename2 , self.hypdata, self.hypdata_map ] ]
            
                self.plothypdata()
                
                # clear listbox and load the spectral bands in the hyperspectral data file
                self.listbox_wl.delete( 0, END )
                for item in self.wl_hyp:
                    self.listbox_wl.insert( END , str(item) )
                
                # set the initial selection    
                listboxselection_wl = np.where( np.logical_and(self.wl_hyp > 709,self.wl_hyp<791) )[0]
                self.listbox_wl.see( listboxselection_wl[0] ) # asks index to be visible
                # self.listbox_wl.index( listboxselection_wl[0] ) # asks index to be at top
                self.listbox_wl.selection_set( listboxselection_wl[0], listboxselection_wl[-1] )           
        
                self.hypdata_loaded = True
                self.button_datafile.configure(bg="green")

                self.button_run.configure( state=ACTIVE )
                self.button_p.configure( state=ACTIVE )
                self.plotrefspectrum2()
            else:
                print("Cannot load "+self.filename2)
                print("No wavelength information in file.")
Exemplo n.º 29
0
 def setup(self):
     import spectral
     from spectral.io.spyfile import SpyFile
     if isinstance(self.file, SpyFile):
         self.image = self.file
     else:
         self.image = spectral.open_image(self.file)
Exemplo n.º 30
0
    def to_hypercube(image_file_name):
        """
        Generate a three-band hypercube from an AVIRIS image.
        Args:
            image_file_name (str):     filename of the source image

        Returns:
            None
        """

        start = time.time()
        logging.info("Starting generation of hypercube from input file: '%s'" %
                     (image_file_name))

        # # open the image
        image = spectral.open_image(image_file_name)

        #view the hypercube
        view_cube(image,
                  bands=[29, 19, 9],
                  title="Hypercube representation of %s" %
                  os.path.basename(image_file_name))

        end = time.time()
        seconds_elapsed = end - start
        m, s = divmod(seconds_elapsed, 60)
        h, m = divmod(m, 60)
        logging.info(
            "Completed hypercube generation. Time elapsed: '%d:%02d:%02d'" %
            (h, m, s))
Exemplo n.º 31
0
    def filter_classes(classified_file_name):
        """
        Modify a classified image to remove unused classes.
        Args:
            classified_file_name (str): file of the classified image
        Returns:
            None
        """

        # open the image
        classified = spectral.open_image(classified_file_name)
        data = classified.asarray()
        M = classified.shape[0]
        N = classified.shape[1]

        # allocate a copy for reindexed pixels
        copy = numpy.zeros(shape=(M,N), dtype=numpy.uint16)

        # find classes actually present in the image
        classes = sorted(set(classified.asarray().flatten().tolist()))
        lookup = [classes.index(i) if i in classes else 0 for i in range(int(classified.metadata.get('classes')))]

        # reindex each pixel
        for x in range(M):
            for y in range(N):
                copy[x,y] = lookup[data[x,y,0]]

        # overwrite the file
        spectral.io.envi.save_classification(
            classified_file_name,
            copy,
            force=True,
            class_names=[classified.metadata.get('class names')[i] for i in classes],
            metadata=classified.metadata)
Exemplo n.º 32
0
    def __init__(self, library_file_name, class_names=None, threshold=0.0, in_memory=False):
        """
        Construct a new ``MineralClassification`` object with a spectral library
        in ENVI format such as the `USGS Digital Spectral Library 06
        <https://speclab.cr.usgs.gov/spectral.lib06/>`_ or the `ASTER Spectral
        Library Version 2.0 <https://asterweb.jpl.nasa.gov/`_ converted with
        ``pycoal.mineral.AsterConversion.convert()``.
        If provided, the optional class name parameter will initialize the
        classifier with a subset of the spectral library, otherwise the full
        spectral library will be used.
        The optional threshold parameter defines a confidence value between zero
        and one below which classifications will be discarded, otherwise all
        classifications will be included.
        In order to improve performance on systems with sufficient memory,
        enable the optional parameter to load entire images.
        Args:
            library_file_name (str):        filename of the spectral library
            class_names (str[], optional): list of names of classes to include
            threshold (float, optional):  classification threshold
            in_memory (boolean, optional): enable loading entire image
        """
        # load and optionally subset the spectral library
        self.library = spectral.open_image(library_file_name)
        if class_names is not None:
            self.library = self.subset_spectral_library(self.library, class_names)

        # store the threshold
        self.threshold = threshold

        # store the memory setting
        self.in_memory = in_memory
        logging.info("Instantiated Mineral Classifier with following specification: " \
         "-spectral library '%s', -class names '%s', -threshold '%s', -in_memory '%s'" 
            %(library_file_name, class_names, threshold, in_memory))
Exemplo n.º 33
0
 def test_create_image_metadata(self):
     '''Test calling `envi.create_image` using a metadata dict.'''
     import os
     import spectral
     (R, B, C) = (10, 20, 30)
     (r, b, c) = (3, 8, 23)
     offset = 1024
     datum = 33
     md = {'lines': R,
           'samples': B,
           'bands': C,
           'interleave': 'bsq',
           'header offset': offset,
           'data type': 12,
           'USER DEFINED': 'test case insensitivity'}
     fname = os.path.join(testdir, 'test_create_image_metadata.hdr')
     img = spectral.envi.create_image(fname, md)
     mm = img.open_memmap(writable=True)
     mm.fill(0)
     mm[r, b, c] = datum
     mm.flush()
     img = spectral.open_image(fname)
     img._disable_memmap()
     assert_almost_equal(img[r, b, c], datum)
     assert(img.offset == offset)
     for key in md:
         assert key.lower() in img.metadata
         assert str(md[key]) == img.metadata[key.lower()]
Exemplo n.º 34
0
 def test_create_image_metadata(self):
     '''Test calling `envi.create_image` using a metadata dict.'''
     import os
     import spectral
     (R, B, C) = (10, 20, 30)
     (r, b, c) = (3, 8, 23)
     offset = 1024
     datum = 33
     md = {
         'lines': R,
         'samples': B,
         'bands': C,
         'interleave': 'bsq',
         'header offset': offset,
         'data type': 12,
         'USER DEFINED': 'test case insensitivity'
     }
     fname = os.path.join(testdir, 'test_create_image_metadata.hdr')
     img = spectral.envi.create_image(fname, md)
     mm = img.open_memmap(writable=True)
     mm.fill(0)
     mm[r, b, c] = datum
     mm.flush()
     img = spectral.open_image(fname)
     img._disable_memmap()
     assert_almost_equal(img[r, b, c], datum)
     assert (img.offset == offset)
     for key in md:
         assert key.lower() in img.metadata
         assert str(md[key]) == img.metadata[key.lower()]
Exemplo n.º 35
0
 def test_save_zero_frame_offset_passes(self):
     '''Opening files with nonzero frame offsets should fail.'''
     import os
     import spectral as spy
     img = spy.open_image('92AV3C.lan')
     fname = os.path.join(testdir, 'test_save_zero_frame_offset_passes.hdr')
     meta = {'major frame offsets': 0}
     spy.envi.save_image(fname, img, metadata=meta)
Exemplo n.º 36
0
    def setup(self):
        from spectral.algorithms.detectors import MatchedFilter
        self.data = spy.open_image('92AV3C.lan').load()
        self.background = spy.calc_stats(self.data)
        self.target_ij = [33, 87]
#        self.target = self.data[33, 87]
        (i, j) = self.target_ij
        self.mf = MatchedFilter(self.background, self.data[i, j])
Exemplo n.º 37
0
 def test_save_zero_frame_offset_passes(self):
     '''Opening files with nonzero frame offsets should fail.'''
     import os
     import spectral as spy
     img = spy.open_image('92AV3C.lan')
     fname = os.path.join(testdir, 'test_save_zero_frame_offset_passes.hdr')
     meta = {'major frame offsets' : 0}
     spy.envi.save_image(fname, img, metadata=meta)
Exemplo n.º 38
0
 def prepare_data(self, img_path, gt_path):
     if img_path[-3:] == 'mat':
         import scipy.io as sio
         img_mat = sio.loadmat(img_path)
         gt_mat = sio.loadmat(gt_path)
         img_keys = img_mat.keys()
         gt_keys = gt_mat.keys()
         img_key = [k for k in img_keys if k != '__version__' and k != '__header__' and k != '__globals__']
         gt_key = [k for k in gt_keys if k != '__version__' and k != '__header__' and k != '__globals__']
         return img_mat.get(img_key[0]).astype('float64'), gt_mat.get(gt_key[0]).astype('int8')
     else:
         import spectral as spy
         img = spy.open_image(img_path).load()
         gt = spy.open_image(gt_path)
         a = spy.principal_components()
         a.transform()
         return img, gt.read_band(0)
Exemplo n.º 39
0
    def run(self):
        import os
        import itertools
        import spectral
        from spectral.tests import testdir

        print('\n' + '-' * 72)
        print('Running SpyFile read tests.')
        print('-' * 72)

        if not os.path.isdir(testdir):
            os.mkdir(testdir)
        image = spectral.open_image(self.filename)
        basename = os.path.join(testdir,
                                os.path.splitext(self.filename)[0])
        interleaves = ('bil', 'bip', 'bsq')
        ends = ('big', 'little')
        cases = itertools.product(interleaves, self.dtypes, ends)
        for (inter, dtype, endian) in cases:
            fname = '%s_%s_%s_%s.hdr' % (basename, inter, dtype,
                                         endian)
            spectral.envi.save_image(fname, image, interleave=inter,
                                     dtype=dtype, byteorder=endian)
            msg = 'Running SpyFile read tests on %s %s %s-endian file ' \
                % (inter.upper(), np.dtype(dtype).name, endian)
            testimg = spectral.open_image(fname)
            if testimg.using_memmap is True:
                print('\n' + '-' * 72)
                print(msg + 'using memmap...')
                print('-' * 72)
                test = SpyFileTest(testimg, self.datum, self.value)
                test.run()
                print('\n' + '-' * 72)
                print(msg + 'without memmap...')
                print('-' * 72)
                testimg._disable_memmap()
                test = SpyFileTest(testimg, self.datum, self.value)
                test.run()
            else:
                print('\n' + '-' * 72)
                print(msg + 'without memmap...')
                print('-' * 72)
                test = SpyFileTest(testimg, self.datum, self.value)
                test.run()
Exemplo n.º 40
0
 def test_save_image_ndarray_no_ext(self):
     '''Test saving an ENVI formated image with no image file extension.'''
     import os
     import spectral
     data = np.arange(1000, dtype=np.int16).reshape(10, 10, 10)
     base = os.path.join(testdir, 'test_save_image_ndarray_noext')
     hdr_file = base + '.hdr'
     spectral.envi.save_image(hdr_file, data, ext='')
     rdata = spectral.open_image(hdr_file).load()
     assert(np.all(data==rdata))
Exemplo n.º 41
0
 def test_iterator_spyfile_nomemmap(self):
     '''Iteration over SpyFile object without memmap'''
     from spectral.algorithms.algorithms import iterator
     i = 5
     data = self.image.load()
     classes = self.gt.ravel()
     pixels = data.reshape((-1, data.shape[-1]))
     sum = np.sum(pixels[classes == 5], 0)
     image = spy.open_image('92AV3C.lan')
     itsum = np.sum(np.array([x for x in iterator(image, classes, 5)]), 0)
     assert_allclose(sum, itsum)
Exemplo n.º 42
0
 def test_open_zero_frame_offset_passes(self):
     '''Files with frame offsets set to zero should open.'''
     import os
     import spectral as spy
     img = spy.open_image('92AV3C.lan')
     fname = os.path.join(testdir, 'test_open_zero_frame_offset_passes.hdr')
     spy.envi.save_image(fname, img)
     fout = open(fname, 'a')
     fout.write('major frame offsets = 0\n')
     fout.write('minor frame offsets = {0, 0}\n')
     fout.close()
     img2 = spy.envi.open(fname)
Exemplo n.º 43
0
 def test_save_nonzero_frame_offset_fails(self):
     '''Opening files with nonzero frame offsets should fail.'''
     import os
     import spectral as spy
     img = spy.open_image('92AV3C.lan')
     fname = os.path.join(testdir, 'test_save_nonzero_frame_offset_fails.hdr')
     meta = {'major frame offsets' : [128, 0]}
     try:
         spy.envi.save_image(fname, img, metadata=meta)
     except spy.envi.EnviFeatureNotSupported:
         pass
     else:
         raise Exception('File erroneously saved.')
Exemplo n.º 44
0
 def test_save_image_ndarray(self):
     '''Test saving an ENVI formated image from a numpy.ndarray.'''
     import os
     import spectral
     (R, B, C) = (10, 20, 30)
     (r, b, c) = (3, 8, 23)
     datum = 33
     data = np.zeros((R, B, C), dtype=np.uint16)
     data[r, b, c] = datum
     fname = os.path.join(testdir, 'test_save_image_ndarray.hdr')
     spectral.envi.save_image(fname, data, interleave='bil')
     img = spectral.open_image(fname)
     assert_almost_equal(img[r, b, c], datum)
Exemplo n.º 45
0
 def test_open_nonzero_frame_offset_fails(self):
     '''Opening files with nonzero frame offsets should fail.'''
     import os
     import spectral as spy
     img = spy.open_image('92AV3C.lan')
     fname = os.path.join(testdir, 'test_open_nonzero_frame_offset_fails.hdr')
     spy.envi.save_image(fname, img)
     fout = open(fname, 'a')
     fout.write('major frame offsets = 128\n')
     fout.close()
     try:
         img2 = spy.envi.open(fname)
     except spy.envi.EnviFeatureNotSupported:
         pass
     else:
         raise Exception('File erroneously opened.')
Exemplo n.º 46
0
    def setup(self):
        import numpy as np
        import spectral
        from spectral.io.spyfile import SpyFile
        if isinstance(self.file, SpyFile):
            self.image = self.file
        elif isinstance(self.file, np.ndarray):
            self.image = self.file
        else:
            self.image = spectral.open_image(self.file)

        self.scalar = 10.
        self.matrix = self.scalar * np.identity(self.image.shape[2],
                                                dtype='f8')
        self.pre = 37.
        self.post = 51.
Exemplo n.º 47
0
 def test_catch_parse_error(self):
     '''Failure to parse parameters should raise EnviHeaderParsingError.'''
     import os
     import spectral as spy
     img = spy.open_image('92AV3C.lan')
     fname = os.path.join(testdir, 'test_catch_parse_error.hdr')
     spy.envi.save_image(fname, img)
     fout = open(fname, 'a')
     fout.write('foo = {{\n')
     fout.close()
     try:
         img2 = spy.envi.open(fname)
     except spy.envi.EnviHeaderParsingError:
         pass
     else:
         raise Exception('Failed to raise EnviHeaderParsingError')
Exemplo n.º 48
0
 def test_create_image_keywords(self):
     '''Test calling `envi.create_image` using keyword args.'''
     import os
     import spectral
     (R, B, C) = (10, 20, 30)
     (r, b, c) = (3, 8, 23)
     datum = 33
     fname = os.path.join(testdir, 'test_create_image_keywords.hdr')
     img = spectral.envi.create_image(fname, shape=(R,B,C),
                                      dtype=np.uint16,
                                      offset=120)
     mm = img.open_memmap(writable=True)
     mm.fill(0)
     mm[r, b, c] = datum
     mm.flush()
     img = spectral.open_image(fname)
     assert_almost_equal(img[r, b, c], datum)
Exemplo n.º 49
0
 def test_missing_ENVI_in_header_fails(self):
     '''FileNotAnEnviHeader should be raised if "ENVI" not on first line.'''
     import os
     import spectral as spy
     img = spy.open_image('92AV3C.lan')
     fname = os.path.join(testdir, 'test_header_missing_ENVI_fails.hdr')
     spy.envi.save_image(fname, img)
     lines = open(fname).readlines()
     fout = open(fname, 'w')
     for line in lines[1:]:
         fout.write(line)
     fout.close()
     try:
         img2 = spy.envi.open(fname)
     except spy.envi.FileNotAnEnviHeader:
         pass
     else:
         raise Exception('Failed to raise EnviMissingHeaderParameter')
Exemplo n.º 50
0
 def test_header_missing_mandatory_parameter_fails(self):
     '''Missing mandatory parameter should raise EnviMissingHeaderParameter.'''
     import os
     import spectral as spy
     img = spy.open_image('92AV3C.lan')
     fname = os.path.join(testdir, 'test_missing_param_fails.hdr')
     spy.envi.save_image(fname, img)
     lines = [line for line in open(fname).readlines() \
              if 'bands' not in line]
     fout = open(fname, 'w')
     for line in lines:
         fout.write(line)
     fout.close()
     try:
         img2 = spy.envi.open(fname)
     except spy.envi.MissingEnviHeaderParameter:
         pass
     else:
         raise Exception('Failed to raise EnviMissingHeaderParameter')
Exemplo n.º 51
0
 def test_create_image_metadata(self):
     '''Test calling `envi.create_image` using a metadata dict.'''
     import os
     import spectral
     (R, B, C) = (10, 20, 30)
     (r, b, c) = (3, 8, 23)
     datum = 33
     md = {'lines': R,
           'samples': B,
           'bands': C,
           'data type': 12}
     fname = os.path.join(testdir, 'test_create_image_metadata.hdr')
     img = spectral.envi.create_image(fname, md)
     mm = img.open_memmap(writable=True)
     mm.fill(0)
     mm[r, b, c] = datum
     mm.flush()
     img = spectral.open_image(fname)
     assert_almost_equal(img[r, b, c], datum)
Exemplo n.º 52
0
def run():
    import spectral as spy
    (fname, datum, value) = ('92AV3C.lan', (99, 99, 99), 2057.0)
    image = spy.open_image(fname)
    print '\n' + '-' * 72
    print 'Running LinearTransform tests on SpyFile object.'
    print '-' * 72
    test = LinearTransformTest(image, datum, value)
    test.run()
    data = image.load()
    print '\n' + '-' * 72
    print 'Running LinearTransform tests on ImageArray object.'
    print '-' * 72
    test = LinearTransformTest(data, datum, value)
    test.run()
    image.scale_factor = 10000.0
    print '\n' + '-' * 72
    print 'Running LinearTransform tests on SpyFile object with scale factor.'
    print '-' * 72
    test = LinearTransformTest(image, datum, value / 10000.0)
    test.run()
Exemplo n.º 53
0
def getSubset(filePath, extend = None):
    if extend is None:
        iface = qgis_utils.iface
        e = iface.mapCanvas().extent()
        xMax = e.xMaximum()
        yMax = e.yMaximum()
        xMin = e.xMinimum()
        yMin = e.yMinimum()
        srcImage = gdal.Open(filePath)
        geoTrans = srcImage.GetGeoTransform()
        print geoTrans
        if geoTrans[0] == 0 and geoTrans[3] == 0:
            # apparently no projection
            minImage = [int(xMin), -int(yMin)]
            maxImage = [int(xMax), -int(yMax)]
            print minImage
            print maxImage
        else:
            # image has projection
            minImage = list(world2Pixel(geoTrans, xMin, yMin))
            maxImage = list(world2Pixel(geoTrans, xMax, yMax))
            if minImage[0] < 0: minImage[0] = 0
            if minImage[1] < 0: minImage[1] = 0
            if maxImage[0] > srcImage.RasterXSize: maxImage[0] = srcImage.RasterXSize
            if maxImage[1] > srcImage.RasterYSize: maxImage[1] = srcImage.RasterYSize
    else:
        minImage = extend[0:2]
        maxImage = extend[2:4]

    if ((minImage[1] - maxImage[1]) * (maxImage[0] - minImage[0])) > 10 ** 7:
        QMessageBox.critical(None, u"Size error", u"The current extent is too large")
        return None
    if '.bsq' in filePath:
        f = filePath.replace('.bsq', '.hdr')
    else:
        f = filePath + '.hdr'
    return spectral.open_image(f).read_subregion([maxImage[1], minImage[1]], [minImage[0], maxImage[0]]), minImage[0], maxImage[1]
Exemplo n.º 54
0
 def setup(self):
     self.image = spy.open_image('92AV3C.lan')
     self.gt = spy.open_image('92AV3GT.GIS').read_band(0)
 def perform_single_pass_clustering(self, filePath):
     img = open_image(filePath).load()
     (self.single_pass_map, self.single_pass_clusters) = cluster(img, self.numClusters)
 def displayLan(self, filePath):
     img = open_image(filePath)
     view(img)
 def perform_kmeans_clustering(self, filePath):
     img = open_image(filePath).load()
     (self.kmeans_map, self.kmeans_clusters) = kmeans(img, self.numClusters, self.numIterations)
Exemplo n.º 58
0
 def setup(self):
     import spectral as spy
     self.data = spy.open_image('92AV3C.lan').load()
Exemplo n.º 59
0
 def setup(self):
     self.data = spy.open_image('92AV3C.lan').load()
Exemplo n.º 60
0
 def setup(self):
     self.data = spy.open_image('92AV3C.lan').load()
     self.background = spy.calc_stats(self.data)