def __load_qb_data(self):
        # sensor filter
        sensor_filter = envi.open(
            resource_filename(
                sbc.__name__,
                "tests/data/sensor_filters/qbtest_filter_350_900nm.hdr"),
            resource_filename(
                sbc.__name__,
                "tests/data/sensor_filters/qbtest_filter_350_900nm.lib"),
        ).spectra

        # input spectra
        input_spectra = envi.open(
            resource_filename(sbc.__name__,
                              "tests/data/qbtest_input_spectra.hdr"),
            resource_filename(sbc.__name__,
                              "tests/data/qbtest_input_spectra.lib"),
        ).spectra[0]

        # output spectra
        output_spectra = envi.open(
            resource_filename(sbc.__name__,
                              "tests/data/qbtest_output_spectra.hdr"),
            resource_filename(sbc.__name__,
                              "tests/data/qbtest_output_spectra.lib"),
        ).spectra[0]

        return sensor_filter, input_spectra, output_spectra
示例#2
0
def read_hyper_data(path, folder_name):
    """
    Read hyperspectral data in ENVI fromat.

    :param path: the path containing the folders of the data.
    :param folder_name: the name of the data folder.
    :return: {'white:', hypercube_of_white, 'dark:', hypercube_of_dark, 'plant:', hypercube_of_object}, meta_of_plant\

    Author: Huajian liu
    version: v0 (10 May, 2018)
    """
    import spectral.io.envi as envi
    spectral.settings.envi_support_nonlowercase_params = True

    # Reading data
    meta_white = envi.open(
        path + '/' + folder_name + '/' + 'capture' + '/' + 'WHITEREF_' +
        folder_name + '.hdr', path + '/' + folder_name + '/' + 'capture' +
        '/' + 'WHITEREF_' + folder_name + '.raw')

    meta_dark = envi.open(
        path + '/' + folder_name + '/' + 'capture' + '/' + 'DARKREF_' +
        folder_name + '.hdr', path + '/' + folder_name + '/' + 'capture' +
        '/' + 'DARKREF_' + folder_name + '.raw')

    meta_plant = envi.open(
        path + '/' + folder_name + '/' + 'capture' + '/' + folder_name +
        '.hdr', path + '/' + folder_name + '/' + 'capture' + '/' +
        folder_name + '.raw')

    return {
        'white': meta_white.load(),
        'dark': meta_dark.load(),
        'plant': meta_plant.load()
    }, meta_plant
    def crism_CreateModeledImage(self, abImgName, saveImg=0, targetFolder=''):
        """
        @function name      :crism_CreateModeledImage
        @description        :This function will be used to created modeled image from the constituents generated
                            using the UMass Pipeline
        ----------------------------------------------------------------------------------------------------------------
        INPUTS
        ----------------------------------------------------------------------------------------------------------------
        :param self:
        :param abImgName: Name/address of the absoprtion image generated by the UMass Pipeline
        :param saveImg: Flag to decide if the modeled image is to be saved. The image is saved if the flag is not 0.
                        (default = 0)
        ----------------------------------------------------------------------------------------------------------------
        OUTPUTS
        ----------------------------------------------------------------------------------------------------------------
        :return: msImg: The image modeled by the UMass pipeline
        """

        'The absorption image header is'
        abHdrName = abImgName.replace('.img', '.hdr')
        'Read in the background image'
        abImg = envi.open(abHdrName, abImgName)
        abCube = abImg.load()
        abHeader = envi.read_envi_header(abHdrName)

        'The background image name is'
        bgImgName = abImgName.replace('_AB.img', '_Bg.img')
        bgHdrName = bgImgName.replace('.img', '.hdr')
        bgImg = envi.open(bgHdrName, bgImgName)
        bgCube = bgImg.load()

        'The modeled image is'
        msCube = abCube * bgCube

        if (saveImg != 0):
            finalLoc = abImgName.rfind('/')
            imgName = abImgName[(finalLoc + 1):]
            imgName = imgName.replace('_AB.img', '_MS.hdr')
            if targetFolder:
                if not os.path.isdir(targetFolder):
                    os.makedirs(targetFolder)
                imgName = targetFolder + imgName
            else:
                localFolder = os.getcwd() + '/out'
                if not os.path.isdir(localFolder):
                    os.makedirs(localFolder)
                imgName = localFolder + imgName

            'Save the image'
            envi.save_image(imgName,
                            msCube,
                            dtype=np.float32,
                            force=True,
                            interleave='bil',
                            metadata=abHeader)
            return imgName.replace('.hdr', '.img')
        else:
            str = 'Completed'
            return str
示例#4
0
def remap(inputfile, labels, outputfile, flag, chunksize):
    """."""

    ref_file = inputfile
    lbl_file = labels
    out_file = outputfile
    nchunk = chunksize

    ref_img = envi.open(ref_file + '.hdr', ref_file)
    ref_meta = ref_img.metadata
    ref_mm = ref_img.open_memmap(interleave='source', writable=False)
    ref = np.array(ref_mm[:, :])

    lbl_img = envi.open(lbl_file + '.hdr', lbl_file)
    lbl_meta = lbl_img.metadata
    labels = lbl_img.read_band(0)

    nl = int(lbl_meta['lines'])
    ns = int(lbl_meta['samples'])
    nb = int(ref_meta['bands'])

    out_meta = dict([(k, v) for k, v in ref_meta.items()])

    out_meta["samples"] = ns
    out_meta["bands"] = nb
    out_meta["lines"] = nl
    out_meta['data type'] = ref_meta['data type']
    out_meta["interleave"] = "bil"

    out_img = envi.create_image(out_file + '.hdr',
                                metadata=out_meta,
                                ext='',
                                force=True)
    out_mm = out_img.open_memmap(interleave='source', writable=True)

    # Iterate through image "chunks," restoring as we go
    for lstart in np.arange(0, nl, nchunk):
        print(lstart)
        del out_mm
        out_mm = out_img.open_memmap(interleave='source', writable=True)

        # Which labels will we extract? ignore zero index
        lend = min(lstart + nchunk, nl)

        lbl = labels[lstart:lend, :]
        out = flag * np.ones((lbl.shape[0], nb, lbl.shape[1]))
        for row in range(lbl.shape[0]):
            for col in range(lbl.shape[1]):
                out[row, :, col] = np.squeeze(ref[int(lbl[row, col]), :])

        out_mm[lstart:lend, :, :] = out
示例#5
0
def open_file():
    open_file.has_been_called = True
    filedialog.askopenfilename.has_been_called = True
    file_name = filedialog.askopenfilename(initialdir=os.path.expanduser("/"),
                                           filetypes=(("ENVI", "*.envi"),
                                                      ("All files", "*")))
    if str(file_name).endswith(".envi"):
        img = envi.open(file_name + ".hdr", file_name + ".envi")
    else:
        img = envi.open(file_name + ".hdr", file_name)
    file_save = file_name.split('/')
    file_save = file_save[:-1]
    save_dir = ""
    for string in file_save:
        save_dir = save_dir + string + "/"
    print(save_dir)
    filedialog.askopenfilename.has_been_called = False
    imshow.has_been_called = True
    imshow(img, bands=(55, 32, 20), aspect=0.45, stretch=0.25)
    print(img)
    print(file_name)
    noisy_bands_info = hy.noise_removal(file_name,
                                        min_threshold=0,
                                        max_threshold=0.55)
    noisy_bands_info.reflectance_plot()
    print("--------- List of noisy bands ---------")
    x = noisy_bands_info.show_noisy_bands_with_min_max()
    print(len(x))
    for values in x:
        print(values)
    nb = noisy_bands_info.show_noisy_bands()
    pre = hy.preprocessing(img_path=file_name.split(".")[0] + ".hdr",
                           save_directory=save_dir,
                           available_memory_gb=8)
    pre.perform(ndvi_threshold=125,
                data_ignore_value=-9999.0,
                NIR=90,
                RED=55,
                min_threshold=0,
                max_threshold=0.55,
                noisy_bands=nb)
    file2_header = file_name + "_part_1"
    pre_image = envi.open(file2_header + ".hdr", file2_header)
    imshow(pre_image, bands=(55, 32, 20), aspect=0.45, stretch=0.25)
    imshow.has_been_called = True
    print(pre_image)
    if imshow.has_been_called == False and filedialog.askopenfilename.has_been_called == False:
        open_file.has_been_called = False
    def crism_fillNanRows(self, imgName):
        """
        This function fill the empty of NaN rows using the neighbors.

        :param imgName: Name/address of the image to be smoothed
        :return:
        """

        hdrName = imgName.replace('.img', '.hdr')
        header = envi.read_envi_header(hdrName)
        'Read in the background image'
        crImg = envi.open(hdrName, imgName)
        crCube = crImg.load()
        [rows, cols, bands] = crCube.shape

        arrCrImg = crCube.reshape((rows * cols, bands))
        arrCrImgCrop = arrCrImg[:, self.strtBand:self.stopBand]
        'Fill the NaNs in the columns'
        arrCrImgCrop = generalUtilities().fill_nan(arrCrImgCrop)
        'Fill the NaNs in the rows'
        arrCrImgCrop = generalUtilities().fill_nan(arrCrImgCrop.T)
        arrCrImg[:, self.strtBand:self.stopBand] = arrCrImgCrop.T
        'Reshape to image size'
        crCube_nr = arrCrImg.reshape((rows, cols, bands))

        'Save the background image'
        outFileName1 = imgName.replace('_MS_CR.img', '_MS_CRnR.hdr')
        envi.save_image(outFileName1,
                        crCube_nr,
                        dtype='single',
                        force=True,
                        interleave='bil',
                        metadata=header)

        return outFileName1.replace('.hdr', '.img')
示例#7
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")
示例#8
0
def getdatesimage(dt):
	print 'Getting %s'%dt
	for (dirpath, dirnames, filenames) in walk('../londiani/%s'%dt):
		filenames = sorted(filenames)
			
		
		im = []
		for f in filenames:
			#print f
			if f[-3:] =='hdr':
				
				img = envi.open('../londiani/%s/%s'%(dt,f))
				l = img.load()
				m = img.open_memmap()
				im.append(m)

	i = im
	#print len(im)
	if len(im) == 7:
		big = np.dstack((im[0],im[1],im[2],im[3],im[4],im[5],im[6]))
	if len(im) == 6:
		big = np.dstack((im[0],im[1],im[2],im[3],im[4],im[5]))
	if len(im) == 5:
		big = np.dstack((im[0],im[1],im[2],im[3],im[4]))
	if len(im) == 4:
		big = np.dstack((im[0],im[1],im[2],im[3]))
		
	return big
示例#9
0
def load_raw_cube(hdr_file):
    data = envi.open(hdr_file).asarray()
    data = data.astype(float)
    metadata = _read_metadata(hdr_file)
    cube = RawCube(data, metadata)

    return cube
示例#10
0
def load_urban_dataset():
    envi_data = envi.open('../data/real/urban/urban.hdr')
    img = envi_data[:, :, :]
    hsi_path = '../data/real/urban/urban.mat'
    ground_truth_path = '../data/real/urban/urban.gt'
    hsi = sio.loadmat(hsi_path)
    Y = hsi['Y'][:, :]
    ground_truth = sio.loadmat(ground_truth_path)['M']
    endmembers_names = [
        'Asphalt Road', 'Grass', 'Tree', 'Roof', 'Metal', 'Dirt'
    ]
    number_rows = hsi['nRow'][0][0]
    number_columns = hsi['nCol'][0][0]
    number_pixels = int(number_rows) * int(number_columns)
    number_endmembers = ground_truth.shape[1]
    number_bands = Y.shape[0]
    dimensions = [number_rows, number_columns, number_bands, number_pixels]
    selected_bands = hsi['SlectBands'].T[0]
    null_bands = list(
        set(list(range(0, len(ground_truth)))) - set(selected_bands))
    ground_truth_plot = ground_truth.copy()
    ground_truth_plot[null_bands, :] = None
    bandsSelection = [selected_bands, null_bands]
    dimensions_plot = [2, 3]
    ground_truth = normalize_data(ground_truth)
    return Y, img, dimensions, number_endmembers, ground_truth, ground_truth_plot, endmembers_names, dimensions_plot, bandsSelection, 'Urban'
示例#11
0
def load_S3_image(image_path):
    """
    load S3 image downloaded with SNAP, applies conversion to TOA and compute ratios

    :param image_path: path to .data/ dir
    :return: 3D np.array with the image bands sorted in BAND_NAMES_S3_RATIOS order
    """
    inputs = [os.path.join(image_path,"Oa%s_reflectance.hdr"%b) for b in BAND_NAMES_S3]

    images = []
    for ip in inputs:
        metadata = envi.read_envi_header(ip)
        gain = np.array([float(m) for m in metadata['data gain values']])
        offset = np.array([float(m) for m in metadata['data offset values']])
        img = np.float64(envi.open(ip)[:,:,:])
        img *= gain
        img += offset
        img/= np.pi
        images.append(img)

    S3ratio1 = (images[7]) / (images[3])
    S3ratio2 = (images[10]) / (images[3])
    images.append(S3ratio1)
    images.append(S3ratio2)
    images = np.concatenate(images,axis=2)

    return images
示例#12
0
def get_data(name, remove_bands=True, clean=True, path=PATH_DATA):
    """
    Returns HSI data from a datacube
    
    Parameters:
    ---------------------
    name: name
    remove_bands: if True, noisy bands are removed (leaving 113 bands)
    clean: if True, remove damaged line
    
    Returns:
    -----------------------
    data, wavelenghts as numpy arrays (float32)
    """
    name = convert_name(name)
    filename = "{}data/{}".format(path, name)
    hsimage = envi.open('{}.hdr'.format(filename), '{}.float'.format(filename))
    wavs = np.asarray(hsimage.bands.centers)
    data = np.asarray(hsimage[:, :, :], dtype=np.float32)

    #removal of damaged sensor line
    if clean and name != 'F_2k':
        data = np.delete(data, 445, 0)

    if not remove_bands:
        return data, wavs
    return data[:, :, get_good_indices(name)], wavs[get_good_indices(name)]
def envi_to_array(img, hdr):
    
    img = envi.open(hdr, img)
    
    return img.read_bands(np.arange(img.shape[2]))
    
    
示例#14
0
    def _load_fixemask(self, slice_=None):
        import spectral.io.envi as envi

        if slice_ is None:
            slice_ = (slice(None), slice(None))

        return envi.open(self.hdr_file)[slice_]
示例#15
0
def read_envi(hdr_file):
    """Load an ENVI map from the .hdr header file

    Parameters:
    -----------
    hdr_file: string
        file path for a .hdr file

    Returns:
    --------
    out : object
        a spectral.io.bipfile.BipFile object

    Examples:
    ---------
    >>> hdr_file = os.path.join(test_data_home, 'test_envi.hdr')
    >>> img = envi.open(hdr_file)
    (17, 32, 1738)
    """

    # read in parameter list in the header file
    params = []

    with open(hdr_file, 'r', encoding='utf-8', errors='ignore') as header:
        for line in header:
            params.append(line.split('=')[0].strip())

    # if parameter 'byte order' is not found, append 'byte order = 0' to the header file
    if 'byte order' not in params:
        with open(hdr_file, 'a', encoding='utf-8', errors='ignore') as header:
            header.write('byte order = 0\n')

    img = envi.open(hdr_file)

    return img
示例#16
0
def preview_cluster(pred8c_hdr, pred8c_img, band, slice_region, columns, rgb):
    img = envi.open(pred8c_hdr, pred8c_img)
    full_shape = img.shape[:2]
    band_names = img.metadata['band names']
    if (slice_region[0] != -1):
        ymin, xmin, ymax, xmax = slice_region
        sl = np.s_[slice_region[0]:slice_region[2], slice_region[1]:slice_region[3]]
    else:
        ymin, xmin, ymax, xmax = (0, 0, full_shape[0], full_shape[1])
        sl = np.s_[:, :]
    if rgb:
        fig1, ax1 = plt.subplots()
        r = img.read_band(band[0], use_memmap=True)[sl]
        g = img.read_band(band[1], use_memmap=True)[sl]
        b = img.read_band(band[2], use_memmap=True)[sl]
        ax1.imshow(np.stack((r, g, b), axis=-1))
    elif len(band):
        cols = min(columns, len(band))
        rows = np.math.ceil(len(band) / cols)
        fig = plt.figure(figsize=(rows, cols))
        for i, _b in enumerate(band):
            b = img.read_band(_b, use_memmap=True)[sl]
            ax = fig.add_subplot(rows, cols, i + 1)
            plt.imshow(b)
            plt.tight_layout()
            ax.tick_params(axis='both', which='major', labelsize=8)
            ax.tick_params(axis='both', which='minor', labelsize=6)
            ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, _: f'{int(x + xmin)}'))
            ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, _: f'{int(y + ymin)}'))
            ax.set_title(band_names[_b], fontdict={'fontsize': 8})
    plt.subplots_adjust(bottom=0.01, left=0.04, wspace=0.1, hspace=0.1, right=0.99, top=0.99)
    plt.show()
示例#17
0
def get_pure_spectra_library():
    img = envi.open('Data/usgs_min.hdr', 'Data/usgs_min.sli')
    spectra = pd.DataFrame(img.spectra, index=img.names, columns=[x*1000 for x in img.bands.centers]).T
    spectra = spectra[(spectra.index > 1000) & (spectra.index < 2540)]
    for column in spectra.columns:
        spectra[column] = spectra[[column]].apply(normalize).apply(savgol).values
    return spectra
    def hsiNan_fill(self, imgHdrName):
        """
        This function can be used to fill in the nans based on the other data in the image.

        :param imgHdrName: location of the HDR file associated with the envi image of choice
        :return:
        """
        imgName = imgHdrName.replace('.hdr', '.img')
        header = envi.read_envi_header(imgHdrName)
        'Read in the background image'
        crImg = envi.open(imgHdrName, imgName)
        crCube = crImg.load()
        [rows, cols, bands] = crCube.shape

        arrCrImg = crCube.reshape((rows * cols, bands))
        'Fill the NaNs in the columns'
        arrCrImg =  generalUtilities().fill_nan(arrCrImg)
        'Fill the NaNs in the rows'
        arrCrImgCrop = arrCrImg[:, 4:244]
        arrCrImgCrop = generalUtilities().fill_nan(arrCrImgCrop.T)
        arrCrImg[:, 4:244] = arrCrImgCrop.T
        'Reshape to image size'
        crCube_nr = arrCrImg.reshape((rows, cols, bands))

        'Save the background image'
        outFileName1 = imgName.replace('.img', '_CRnR.hdr')
        envi.save_image(outFileName1, crCube_nr, dtype='single',
                        force=True, interleave='bil', metadata=header)

        return outFileName1
示例#19
0
    def create_Mask4CRISMImages(self, imgName, sigAbsoprtionLevel=0.99):
        """
        @function name      :create_Mask4CRISMImages
        @description        :This function accepts the smoothed continuum removed image and flag spectra which have a
        comparitively large absorptions and are worthy of further analysis.
        ----------------------------------------------------------------------------------------------------------------
        INPUTS
        ----------------------------------------------------------------------------------------------------------------
        1) imgName:              Address of the Continuum Removed smoothed image.
        2) sigAbsoprtionLevel:   The threshold below which the spectra can be considered as having absorptions of which
        can be considered significant.
        ----------------------------------------------------------------------------------------------------------------
        OUTPUTS
        ----------------------------------------------------------------------------------------------------------------
        1) mapName:              Address of the Hyperspectral image which contains the mask image.
        """

        hdrName = imgName.replace('.img', '.hdr')
        'Read in the background image'
        img = envi.open(hdrName, imgName)
        sCRCube = img.load()
        sCRCube = sCRCube[:, :, self.strtBand:self.stopBand]

        sCRCubeMap = np.min(sCRCube, axis=2)
        temp = np.zeros(sCRCubeMap.shape)
        temp[sCRCubeMap < sigAbsoprtionLevel] = 1

        outFileName = imgName.replace('.img', '_mask.hdr')
        envi.save_image(outFileName, temp, dtype=np.float32, force=True,
                        interleave='bil')

        return outFileName.replace('.hdr', '.img')
示例#20
0
def load_mask(image_path):
    """
    laod the water mask from S3 image

    :param image_path: path to .data/ dir
    :return:
    """
    mask_file = os.path.join(image_path,"WQSF_lsb.hdr")
    mask = envi.open(mask_file)[:,:,0]
    mask = mask[:,:,0]
    dim_file,_ = os.path.splitext(image_path)
    dim_file+=".dim"

    e = emt.parse(dim_file).getroot()

    gp = e.find("./Flag_Coding[@name='WQSF_lsb']")
    flag_to_index = dict([(f.find("Flag_Name").text,int(f.find("Flag_Index").text)) for f in gp.getchildren()])
    mascara = np.zeros(mask.shape,dtype=mask.dtype)

    masks_to_use = ["CLOUD","CLOUD_AMBIGUOUS","CLOUD_MARGIN","SNOW_ICE","HIGHGLINT"]
    for m in masks_to_use:
        mascara |= ((mask & flag_to_index[m])!=0)

    mascara = (mascara==1)

    masks_to_use = ["WATER","INLAND_WATER"]
    mascara_water = np.zeros(mask.shape,dtype=mask.dtype)
    for m in masks_to_use:
        mascara_water |= ((mask & flag_to_index[m])!=0)
    mascara_water = (mascara_water == 1)

    mascara |= (~mascara_water)

    return mascara
def main(infile):

    inhdr = infile + ".hdr"
    outhdr = os.path.basename(infile)[0:-11] + "_glint900metric.hdr"

    img = envi.open(inhdr, infile)

    wl = s.array([float(w) for w in img.metadata['wavelength']])
    if (wl[0] < 100):
        wl = wl * 1000
    fwhm = s.array([float(w) for w in img.metadata['fwhm']])

    b900 = s.argmin(abs(wl - 900.0))
    ## b1050 = s.argmin(abs(wl-1050.0))

    b900imgs = img.read_bands([b900 - 1, b900, b900 + 1])
    ## b1050imgs = img.read_bands([b1050-1, b1050, b1050+1])

    glintm900 = np.median(b900imgs, axis=2).reshape(
        (b900imgs.shape[0], 1, b900imgs.shape[1]))
    ## glintm1050 = np.median(b1050imgs, axis=2).reshape((b1050imgs.shape[0], 1, b1050imgs.shape[1]))

    # make output glint metric file and open memmap
    metadata = img.metadata.copy()
    metadata['bands'] = '%i' % 1
    metadata['interleave'] = 'bil'
    metadata['data type'] = '4'
    out = envi.create_image(outhdr, metadata, ext='', force=True)
    outmm = out.open_memmap(interleave='source', writable=True)

    outmm[:, :, :] = glintm900
    del outmm, out, glintm900, img
示例#22
0
 def open_file(self):
     self.name = QtGui.QFileDialog.getOpenFileName(None, 'Open File')
     global lib
     lib = envi.open(str(self.name))
     collection = lib.names
     for x in collection:
         self.listview.addItem(x)
示例#23
0
def getdatesimage(dt):
    print 'Getting %s' % dt
    for (dirpath, dirnames, filenames) in walk('../londiani/%s' % dt):
        filenames = sorted(filenames)

        im = []
        for f in filenames:
            #print f
            if f[-3:] == 'hdr':

                img = envi.open('../londiani/%s/%s' % (dt, f))
                l = img.load()
                m = img.open_memmap()
                im.append(m)

    i = im
    #print len(im)
    if len(im) == 7:
        big = np.dstack((im[0], im[1], im[2], im[3], im[4], im[5], im[6]))
    if len(im) == 6:
        big = np.dstack((im[0], im[1], im[2], im[3], im[4], im[5]))
    if len(im) == 5:
        big = np.dstack((im[0], im[1], im[2], im[3], im[4]))
    if len(im) == 4:
        big = np.dstack((im[0], im[1], im[2], im[3]))

    return big
示例#24
0
def load_cuprite_dataset():
    envi_data = envi.open('../data/real/cuprite/cuprite.hdr')
    img = envi_data[:, :, :]
    hsi_path = '../data/real/cuprite/cuprite.mat'
    ground_truth_path = '../data/real/cuprite/cuprite.gt'
    hsi = sio.loadmat(hsi_path)
    Y = hsi['Y'][1:, :]
    ground_truth = sio.loadmat(ground_truth_path)['M']
    ground_truth = ground_truth[1:, :]
    endmembers_names = [
        'Alunite', 'Andradite', 'Buddingtonite', 'Dumortierite', 'Kaolinite_1',
        'Kaolinite_2', 'Muscovite', 'Montmorillonite', 'Nontronite', 'Pyrope',
        'Sphene', 'Chalcedony'
    ]
    number_rows = hsi['nRow'][0][0]
    number_columns = hsi['nCol'][0][0]
    number_pixels = int(number_rows) * int(number_columns)
    number_endmembers = ground_truth.shape[1]
    number_bands = Y.shape[0]
    dimensions = [number_rows, number_columns, number_bands, number_pixels]
    selected_bands = hsi['SlectBands'].T[0]
    selected_bands = selected_bands[1:]
    null_bands = list(
        set(list(range(0, len(ground_truth)))) - set(selected_bands))
    ground_truth_plot = ground_truth.copy()
    ground_truth_plot[null_bands, :] = None
    bandsSelection = [selected_bands, null_bands]
    ground_truth = ground_truth[selected_bands]
    dimensions_plot = [4, 3]
    ground_truth = normalize_data(ground_truth)
    return Y, img, dimensions, number_endmembers, ground_truth, ground_truth_plot, endmembers_names, dimensions_plot, bandsSelection, 'Cuprite'
示例#25
0
文件: specPy.py 项目: nv73/specPy
    def importSpectralData(self):

        headerFilePath = QtGui.QFileDialog.getOpenFileName(
            self, 'Select header file to open...', '', ".hdr(*.hdr)")

        imageFilePath = QtGui.QFileDialog.getOpenFileName(
            self, 'Select image file to open...', '',
            "all(*);;.img(*.img);;.bip(*.bip)")

        self.hsImage = envi.open(str(headerFilePath), str(imageFilePath))

        #Generate a numpy nd array of the hyperspectral dataset.
        hsArray = self.hsImage.load()

        #Assign a class variable to the array
        self.hsArray = hsArray

        #Also provide the child graphicsview widget with access to the array
        self.drawArea.img = hsArray

        #get the dimensions of the nd array.
        x, y, z = hsArray.shape

        #Slice the array to retrieve the band 0 layer
        hsArray = hsArray[:, :, 0]

        hsArray = np.reshape(hsArray, (x, y))

        self.drawArea.display_Image_From_Array(hsArray, True)

        self.isEmpty = False
示例#26
0
文件: rx.py 项目: wyj0613/CRC4Docker
def main():
    usage = '''
Usage:
------------------------------------------------

RX anomaly detection for multi- and hyperspectral images

python %s [OPTIONS]  filename

Options:
  
  -h         this help
  
-------------------------------------------------''' % sys.argv[0]
    options, args = getopt.getopt(sys.argv[1:], 'h')
    for option, value in options:
        if option == '-h':
            print usage
            return
    gdal.AllRegister()
    infile = args[0]
    path = os.path.dirname(infile)
    basename = os.path.basename(infile)
    root, ext = os.path.splitext(basename)
    outfile = path + '/' + root + '_rx' + ext
    print '------------ RX ---------------'
    print time.asctime()
    print 'Input %s' % infile
    start = time.time()
    #  input image, convert to ENVI format
    inDataset = gdal.Open(infile, GA_ReadOnly)
    cols = inDataset.RasterXSize
    rows = inDataset.RasterYSize
    projection = inDataset.GetProjection()
    geotransform = inDataset.GetGeoTransform()
    driver = gdal.GetDriverByName('ENVI')
    enviDataset = driver.CreateCopy('imagery/entmp', inDataset)
    inDataset = None
    enviDataset = None
    #  RX-algorithm
    img = envi.open('imagery/entmp.hdr')
    arr = img.load()
    rx = RX(background=calc_stats(arr))
    res = rx(arr)
    #  output
    driver = gdal.GetDriverByName('GTiff')
    outDataset = driver.Create(outfile,cols,rows,1,\
                                    GDT_Float32)
    if geotransform is not None:
        outDataset.SetGeoTransform(geotransform)
    if projection is not None:
        outDataset.SetProjection(projection)
    outBand = outDataset.GetRasterBand(1)
    outBand.WriteArray(np.asarray(res, np.float32), 0, 0)
    outBand.FlushCache()
    outDataset = None
    os.remove('imagery/entmp')
    os.remove('imagery/entmp.hdr')
    print 'Result written to %s' % outfile
    print 'elapsed time: %s' % str(time.time() - start)
示例#27
0
 def __init__(self, in_file, cloud_file=None):
     sensor = 'MODIS'
     super(ReadModisEnvi, self).__init__(in_file, sensor)
     self.cloud_file = cloud_file
     img = envi.open(self.in_file)
     bands = range(0, 44)
     img_data = img.read_bands(bands)
     self.data = img_data
def get_images(img_dir, d_img_name, s_img_name, l_img_name):
    """
    """
    # Replace all NULLs with zeros
    ddr_img = envi.open(file=img_dir + d_img_name + '.hdr')
    # ddr_img = remove_nulls(ddr_img)
    ddr_img = ddr_img[:, :, :]

    s_img = envi.open(file=img_dir + s_img_name + '.hdr')
    # s_img = remove_nulls(s_img)
    s_img = s_img[:, :, :]

    l_img = envi.open(file=img_dir + l_img_name + '.hdr')
    # l_img = remove_nulls(l_img)
    l_img = l_img[:, :, :]

    return ddr_img, s_img, l_img
示例#29
0
    def add_files(self, hdr_file, dat_file):
        self.idx_dta = QSettings.ENVIITEMS
        self.envi_img = envi.open(hdr_file, dat_file)

        wavenum = envi.read_envi_header(hdr_file)['wavelength']
        self.bands = np.array([float(i) for i in wavenum])

        self.maxidx = len(self.idx_dta)
    def data_dims(self):
        '''
            # Returns the predicted number of samples and the number of bands.
        '''

        # these would be used to take care of boundary conditions
        h_rep = 0
        w_rep = 0

        rows = self.__masks.shape[0] + h_rep
        cols = self.__masks.shape[1] + w_rep

        annotated_pixels = np.zeros((self.__masks.shape[0], self.__masks.shape[1]))
        for i in range(self.__num_masks):
            annotated_pixels += self.__masks[:, :, i]

        annotated_pixels[annotated_pixels > 1] = 0
        num_samples = 0

        if self.__balance:
            if self.__num_samples is None:

                max_samples = np.amax(self.__samples_per_class)

                for i in range(0, self.__num_masks):
                    num_samples += self.__samples_per_class[i] * int(
                        floor(max_samples / self.__samples_per_class[i]))
                    num_samples += max_samples % self.__samples_per_class[i]  # add remaining samples
            else:
                num_samples = 0

                max_samples = np.amax(self.__samples_per_class)

                for i in range(0, self.__num_masks):
                    num_samples += self.__samples_per_class[i] * int(
                        floor(max_samples / self.__samples_per_class[i]))
                    num_samples += max_samples % self.__samples_per_class[i]  # add remaining samples

        elif self.__num_samples is None:

            num_samples = np.count_nonzero(annotated_pixels)
            # print('\n========>num_samples: ', num_samples)
        else:
            for i in range(0, self.__num_masks):
                if self.__num_samples > np.count_nonzero(self.__masks[:, :, i]):
                    num_samples += np.count_nonzero(self.__masks[:, :, i])
                else:
                    num_samples += self.__num_samples
                    # print('balancing -- num-samples: ', num_samples)

        # assuming a reader object has been initialized and filenames has been loaded
        file_name = self.__data_file_names[0]
        file_name = path.join(self.__data_folder_name, file_name)

        # only load metadata
        cur_data = envi.open(file_name + HDR_EXT, file_name)

        return num_samples, cur_data.nbands, rows, cols
示例#31
0
    def __init__(self):

        # Load dataset
        trainingset = envi.open('Flevoland/Data/Flevoland.hdr',
                                'Flevoland/Data/Flevoland.raw')

        # trainingset_gt = np.array(Image.open('Flevoland/Data/TrainingSet/cm3253_ground_truth_training.tiff'))
        trainingset_gt = envi.open(
            'Flevoland/Data/Flevoland_gt_corrected.hdr',
            'Flevoland/Data/Flevoland_gt_corrected.raw')

        # Dataset variables
        # Input data shape: (145,145,200)
        self.height = trainingset.nrows
        self.width = trainingset.ncols
        self.bands = trainingset.nbands
        self.num_pixels = self.height * self.width

        self.num_classes = int(trainingset.metadata['classes']) - 1
        self.class_names = trainingset.metadata['class names'][1:]

        # Complete ground truth
        # self.complete_gt = self.convert_gt(scipy.io.loadmat("IndianPines/Data/Indian_pines_gt.mat")['indian_pines_gt'])

        # Obtain train data
        self.input_data = trainingset.load()
        self.train_data = trainingset_gt.load().squeeze()
        self.padded_data = self.input_data

        self.complete_gt = self.train_data

        # Obtain test data by comparing training set to complete ground truth
        # self.test_data = self.get_test_data()

        # Store number of pixels training/test
        self.train_pixels = np.count_nonzero(self.train_data)
        # self.test_pixels = np.count_nonzero(self.test_data)

        # Color scale to display image
        class_colors = np.asarray(trainingset.metadata['class lookup'],
                                  dtype=int)
        class_colors = class_colors.reshape((int(class_colors.size / 3), 3))

        self.color_scale = ColorScale(
            [x for x in range(class_colors.shape[0])], class_colors)
示例#32
0
 def load_envi(self, path):
     basename = os.path.basename(path)
     dest = os.path.join(tempdir, basename)
     if not os.path.exists(dest):
         self.download(path, dest)
     raw = f"{dest.split('.')[0]}.raw"
     if not os.path.exists(raw):
         self.download(f"{path.split('.')[0]}.raw", raw)
     return np.array(envi.open(dest, raw)[:, :, :])
示例#33
0
def load_image(path):
  hdrpath = path+'.hdr'
  for p in [path, hdrpath]:
    if not os.path.exists(p):
      raise IOError('Cannot find %s\n' % p)
  img = envi.open(hdrpath)
  if img.sample_size != 4: 
    raise ValueError('I need 4 byte floating point values\n')
  if img.bands.band_unit == 'Nanometers':
    img.bands.band_unit = 'Microns'
    img.bands.centers = [v/1000.0 for v in img.bands.centers]
    img.bands.bandwidths = [v/1000.0 for v in img.bands.bandwidths] 
  return img, path
示例#34
0
def load_ENVI_spec_lib(file_name):
    """
    Load a ENVI .sli file.

    Parameters:
        file_name: `path string`
            The complete path to the library file to load.

    Returns: `numpy array`
        A (n x p) HSI cube.

        head: `dictionary`
            Starting at version 0.13.1, the ENVI file header

    """
    sli = envi.open(file_name)
    head = envi.read_envi_header(file_name)
    return sli.spectra, head
def envi_to_array(img, hdr):
    """Function which used the spectralpy package to convert envi style images
    to numpy style arrays.

    Parameters:
    -----------
    img - .img file
        .img file with the image

    hdr - header file

    Returns
    -------
    Numpy array - (n by m by d) array

    """
    img = envi.open(hdr, img)

    return img.read_bands(np.arrange(img.shape[2]))
示例#36
0
def load_ENVI_file(file_name):
    """
    Load the data and the header from an ENVI file.
    It use the SPy (spectral) library. At 'file_name' give the envi header file name.

    Parameters:
        file_name: `path string`
            The complete path to the file to load. Use the header file name.

    Returns: `tuple`
        data: `numpy array`
            A (m x n x p) HSI cube.

        head: `dictionary`
            Starting at version 0.13.1, the ENVI file header
     """
    img = envi.open(file_name)
    head = envi.read_envi_header(file_name)
    return np.array(img.load()), head
示例#37
0
def main():
    gdal.AllRegister()
    path = auxil.select_directory("Input directory")
    if path:
        os.chdir(path)
    #  input image, convert to ENVI format
    infile = auxil.select_infile(title="Image file")
    if infile:
        inDataset = gdal.Open(infile, GA_ReadOnly)
        cols = inDataset.RasterXSize
        rows = inDataset.RasterYSize
        projection = inDataset.GetProjection()
        geotransform = inDataset.GetGeoTransform()
        driver = gdal.GetDriverByName("ENVI")
        enviDataset = driver.CreateCopy("entmp", inDataset)
        inDataset = None
        enviDataset = None
    else:
        return
    outfile, outfmt = auxil.select_outfilefmt(title="Output file")
    #  RX-algorithm
    img = envi.open("entmp.hdr")
    arr = img.load()
    rx = RX(background=calc_stats(arr))
    res = rx(arr)
    #  output
    driver = gdal.GetDriverByName(outfmt)
    outDataset = driver.Create(outfile, cols, rows, 1, GDT_Float32)
    if geotransform is not None:
        outDataset.SetGeoTransform(geotransform)
    if projection is not None:
        outDataset.SetProjection(projection)
    outBand = outDataset.GetRasterBand(1)
    outBand.WriteArray(np.asarray(res, np.float32), 0, 0)
    outBand.FlushCache()
    outDataset = None
    print "Result written to %s" % outfile
示例#38
0
 def __init__(self, fname):
     self.f_handle = envi.open(fname)
     self.spectra = self.f_handle.spectra
     self.head = envi.read_envi_header(fname)
     self.wvl = [float(x) for x in self.head['wavelength']]
     self.spectra_names = self.head['spectra names']
示例#39
0
plotspeed=False


### load the .nc file #####
data = loadnc('runs/' +grid+'/' + name + '/output/',singlename=grid + '_0001.nc')
print('done load')
data = ncdatasort(data,trifinder=True)
print('done sort')







img=envi.open('data/kelp_data/NDVI_1999-2013_autumn-mean.hdr','data/kelp_data/NDVI_1999-2013_autumn-mean.img')


A=np.squeeze(img[:,:]).T




startx=432105
starty=5928375





Asize=A.shape
示例#40
0
文件: phsRead.py 项目: AI42/RITSAR
def DIRSIG(directory):
    from spectral.io import envi
    
    #get phase history
    phs_fname = getWildcard(directory, '*.hdr')
    phs = envi.open(phs_fname).load(dtype = np.complex128)
    phs = np.squeeze(phs)
    
    #get platform geometry
    ppd_fname = getWildcard(directory, '*.ppd')
    tree = ET.parse(ppd_fname)
    root = tree.getroot()
    
    pos_dirs = []
    for children in root.iter('point'):
        pos_dirs.append(float(children[0].text))
        pos_dirs.append(float(children[1].text))
        pos_dirs.append(float(children[2].text))
    pos_dirs = np.asarray(pos_dirs).reshape([len(pos_dirs)/3,3])
    
    t_dirs=[]
    for children in root.iter('datetime'):
        t_dirs.append(float(children.text))
    t_dirs = np.asarray(t_dirs)
    
    #get platform system paramters
    platform_fname = getWildcard(directory, '*.platform')
    tree = ET.parse(platform_fname)
    root = tree.getroot()
    
    #put metadata into a dictionary
    metadata = root[0]
    keys = []; vals = []
    for children in metadata:
        keys.append(children[0].text)
        vals.append(children[1].text)
    metadata = dict(zip(keys,vals))
    
    #obtain key parameters
    c           = 299792458.0
    nsamples    = int(phs.shape[1])
    npulses     = int(phs.shape[0])
    vp          = float(get(root, 'speed'))
    delta_t     = float(get(root, 'delta'))
    t           = np.linspace(-nsamples/2, nsamples/2, nsamples)*delta_t
    prf         = float(get(root, 'clockrate'))
    chirprate   = float(get(root, 'chirprate'))/pi
    T_p         = float(get(root, 'pulseduration'))
    B           = T_p*chirprate
    B_IF        = (t.max() - t.min())*chirprate
    delta_r     = c/(2*B_IF)
    f_0         = float(get(root, 'center'))*1e9
    freq        = f_0+chirprate*t
    omega       = 2*pi*freq
    k_r         = 2*omega/c
    T0          = float(get(root, 'min'))
    T1          = float(get(root, 'max'))
    
    #compute slowtime position
    ti = np.linspace(0,1.0/prf*npulses, npulses)
    x = np.array([np.interp(ti, t_dirs, pos_dirs[:,0])]).T
    y = np.array([np.interp(ti, t_dirs, pos_dirs[:,1])]).T
    z = np.array([np.interp(ti, t_dirs, pos_dirs[:,2])]).T
    pos = np.hstack((x,y,z))
    L = norm(pos[-1]-pos[0])
    k_y = np.linspace(-npulses/2,npulses/2,npulses)*2*pi/L
    
    #Vector to scene center at synthetic aperture center
    if np.mod(npulses,2)>0:
        R_c = pos[npulses/2]
    else:
        R_c = np.mean(
                pos[npulses/2-1:npulses/2+1],
                axis = 0)
                
    #Derived Parameters
    if np.mod(nsamples,2)==0:
        T = np.arange(T0, T1+0*delta_t, delta_t)
    else:
        T = np.arange(T0, T1, delta_t)
    
    #Mix signal
    signal = np.zeros(phs.shape)+0j
    for i in range(0,npulses,1):
        r_0 = norm(pos[i])
        tau_c = 2*r_0/c
        ref = np.exp(-1j*(2*pi*f_0*(T-tau_c)+pi*chirprate*(T-tau_c)**2))
        signal[i,:] = ref*phs[i,:]
    
    platform = \
    {
        'f_0'       :   f_0,
        'freq'      :   freq,
        'chirprate' :   chirprate,
        'B'         :   B,
        'B_IF'      :   B_IF,
        'nsamples'  :   nsamples,
        'npulses'   :   npulses,
        'delta_r'   :   delta_r,
        'delta_t'   :   delta_t,
        'vp'        :   vp,
        'pos'       :   pos,
        'R_c'       :   R_c,
        't'         :   t,
        'k_r'       :   k_r,
        'k_y'       :   k_y,
        'metadata'  :   metadata
    }
    
    return(signal, platform)
示例#41
0
def load_spec_lib(lib_path, lib_hdr_name):
    dfin = osp.join(lib_path, lib_hdr_name)
    sli = envi.open(dfin)
    return sli.spectra
示例#42
0
def fmle(image_path="C:\\Users\\jose\\Desktop\\imagenes\\remanso", roi_path="C:\\Users\\jose\\Desktop\\imagenes\\remanso_map_verifi", gt_path="C:\\Users\\jose\\Desktop\\imagenes\\remanso_map_entre",img_dest= "C:\\Users\\jose\\Desktop\\",m=1.5):       
    ''''
    Ejecucion del algoritmo fmle .
    Parameteros:
        image_path: ruta de la imagen a clasificar.
        roi_path:  ruta del mapa de entrenamiento.
        gt_path: ruta del mapa de verificacion.
        img_dest=:  ruta donde se desea guardar el mapa de clasificacion.
    '''
    from numpy import zeros, transpose, dot, newaxis,compress, indices, reshape, not_equal
    ## crea un ImageArray object que contiene toda la interface de un numpy array, apartir de la ruta de la roi .
    roi = envi.open( roi_path + '.hdr', roi_path).read_band(0)
    ## crea un ImageArray object que contiene toda la interface de un numpy array, apartir de un archivo de cabecera envi, que contien la imagen.
    img = envi.open( image_path + '.hdr', image_path).load()
    gt = envi.open( gt_path + '.hdr', gt_path).read_band(0)
    
    tiempo_inicial = clock()
    #crea las clases de entrenamiento
    classes = create_training_classes(img, gt,m=m)
    d = len(classes.classes)
    #crea el clasificador gaussiano
    gmlc = FuzzyGaussianClassifier(classes)
    #clasificacion de la imagen
    clMap = gmlc.classify_image(img)
    tiempo_final = clock()
    (nrows, ncols) = clMap.shape
  
    class_indices = set(roi.ravel())
    k = len(class_indices)-1
    
    class_indices = class_indices.difference([0])
    class_indices =list(class_indices)
    
    ind_clas ={}
    for i in range(k):
        ind_clas[class_indices[i]]=i
    
    (nrows, ncols) = roi.shape
    
    mask_index = numpy.not_equal(roi, 0.0)
       
    inds = transpose(indices((nrows, ncols)), (1, 2, 0))
    inds = reshape(inds, (nrows * ncols, 2))
    inds = compress(numpy.not_equal(mask_index.ravel(), 0), inds, 0).astype('h')
 
    ne= len(inds)
    roi_test = zeros(ne)
    img_test = zeros(ne)
    
    for i in range(inds.shape[0]):      
        x = roi[inds[i,0],inds[i,1]]
        roi_test[i]= ind_clas[x]
        y = clMap[inds[i][0], inds[i][1]]
        img_test[i]= int(y)
    

    
    w = np.zeros((nrows, ncols, 3))
    table_color = lookup_table()
    for i in range(nrows):
       for j in range(ncols):
            maxi = int(clMap[i,j])
            w[i,j,:] = table_color[maxi+1]
            if (maxi == k) :
                w[i,j,:] = table_color[0]
        
                
    imagen = save_image(img_dest, "classified_image_by_fmle", w)
    print "runtime of FMLE is: %f " % (tiempo_final - tiempo_inicial)
    
    return (roi_test,img_test) 
示例#43
0
def create_image_from_envi_header(image_path):
    img = envi.open(image_path + '.hdr' , image_path)
    img = img.load()
    return img
示例#44
0
def main(argv=None):
    class usage(Exception):
    	def __init__(self, msg):            
            self.msg = usage_msg

    if argv is None:
        argv = sys.argv
    try:
        try:
            longopts   = ['verbose','flip','norm','help']	  
            longoptsp  = ['dat','ref','bands','matkey','load-rois','scale','wvl']   
            shortopts  = ''.join([o[0] for o in longopts])
            shortopts += ''.join([o[0]+':' for o in longoptsp])
            opts, args = getopt.getopt(argv[1:], shortopts, longopts)
            datf = args[0]
            
        except (getopt.error, IndexError), msg:
            raise usage(msg)
 
        verbose = False
        flipdat = False
        norm = False
        refbands = [0,1,2]
        scalec = 1.0
        bbox,wvlf = None,None        
        reff,refkey = None,None
        colorf,roif = None,None
        for opt, val in opts:
            if opt in ('--verbose','-v'):
                verbose=True
            elif opt in ('--help','-h'):
                print __doc__
                print usage_msg
                sys.exit(0)
            elif opt in ('--bands','-b'):
                refbands = np.asarray(val.split(','),int)
            elif opt in ('--ref','-r'):
                reff = val.split(':')
                if len(reff)==2:
                    refkey = reff[1]
                reff = reff[0]
            elif opt in ('--colorfile', '-c'):
                colorf = val
            elif opt in ('--flip', '-f'):
                flipdat = True
            elif opt in ('--norm', '-n'):
                norm = True
            elif opt in ('--load-rois', '-l'):
                roif = val
            elif opt in ('--scale','-s'):
                scalec = float(val)
            elif opt in ('--wvl', '-w'):
                wvlf = val

        if len(args) != 1:
            raise usage(usage_msg)

        print datf
    
        datf = datf.split(':')
        if len(datf)==2:
            datkey = datf[1]
        datf = datf[0]
    
        if len(refbands) > 3:
            print "Too many refbands"
            return

        colortab = None
        if colorf is not None:
            #    colorf = 'cm_jet16.txt'
            colortab = np.loadtxt(colorf,dtype=float)
            colortab = np.c_[colortab,np.ones([colortab.shape[0],1])*0.75]            
            
        dfile,dext = os.path.splitext(datf)
        normf = '%s_normed.mat'%dfile
        
        if norm and pathexists(normf):
            print "Loading precomputed normalized data file %s"%normf
            dat = load_matlab(normf,'normed')
            print "Finished loading normalized data"
                   
        if dext == '.mat':
            dat = load_matlab(datf,datkey)
            if len(dat)==0:
                return -1
        elif dext in ('.img','.hdr'):
            import spectral.io.envi as envi
            dinfo = envi.open(datf)
            dat = dinfo.read_bands(range(dinfo.shape[2])).squeeze()
        else:
            dat = pl.imread(datf)

        x,y,z = np.atleast_3d(dat).shape

        if verbose:
            print "Loaded %d x %d x %d image data from file %s"%(x,y,z,datf)

        if norm and not pathexists(normf):
            from scipy.io import savemat 
            print "Saving normalized data to file %s"%normf  
            dat = dat.reshape([x*y,z])   
            normv = np.apply_along_axis(np.linalg.norm,1,dat)
            normv[normv==0] = 1.0
            dat = (dat.T / normv.T).T            
            dat = dat.reshape([x,y,z])               
            savemat(normf,{'normed':dat})
            print "Finished saving normalized data"            

        if flipdat:
            dat = dat[::-1,:,:]
                
        if reff is None:
            ref = dat[:,:,refbands]
        else:
            rfile,rext = os.path.splitext(reff)
            if rext == '.mat':
                ref = load_matlab(reff,refkey)
                if len(ref)==0:
                    return -1
            else:
                ref = pl.imread(reff)   

        if scalec != 1.0:
            reftype = ref.dtype
            ref = (ref.astype(float)*scalec).astype(reftype)

        if verbose:
            print "Reference image bands=%s, scaling factor=%f"%(refbands,scalec)
    
        if wvlf is None:
            wvl = np.arange(dat.shape[2])
        else:
            wvl = load_matlab(wvlf,'wvl')
        
        if tuple(dat.shape[:2]) != tuple(ref.shape[:2]):
            print 'Data and Reference images not the same size:',
            print dat.shape, ref.shape
            return -1
        
        if roif is None:
            roif = dfile+'_rois.mat'

        lab = LABELER(dat,ref,wvl=wvl,colortab=colortab,roifile=roif,
                      verbose=verbose)

        pl.ioff()
        pl.show()
示例#45
0
import time
from spectral import *
import spectral.io.envi as envi
import numpy as np


i = []
dir = '1Jan1979'
names = [2,3,4,5,6,7]
for n in names:
	img = envi.open('../londiani/%s/band%s.hdr'%(dir,n))
	l = img.load()
	m = img.open_memmap()
	i.append(m)


big = np.dstack((i[0],i[1],i[2],i[3],i[4],i[5]))
#rgb= np.dstack((s5,s4,s3))

(mm,c) = kmeans(big,20,20)
clusters = {}
for i in range(20):
    clusters[i] = np.sum(mm==i)

print clusters
a =sorted(clusters.items(), key=lambda clusters: clusters[1])
a.reverse()
clusters = a


pc = principal_components(big)
示例#46
0
if not os.path.exists(savepath): os.makedirs(savepath)

region=regions(regionname)
nidx=get_nodes(data,region)
eidx=get_elements(data,region)





ll=np.loadtxt('data/kelp_data/newkelp_ll_nad.dat')
np.save('data/kelp_data/newkelp_ll_nad.npy',ll)

ll=np.load('data/kelp_data/newkelp_ll_nad.npy')

img=envi.open('data/kelp_data/NDVI_1999-2013_winter-mean.hdr','data/kelp_data/NDVI_1999-2013_winter-mean.img')

#img2=envi.open('data/kelp_data/NDVI_1999-2013_summer-mean.hdr','data/kelp_data/NDVI_1999-2013_summer-mean.img')
#A2=(np.squeeze(img2[:,:]).T)
#indata = np.genfromtxt('data/kelp_data/NDVI_1999-2013_summer-mean_LL.txt',skip_header=5)
#A=indata[:,2]

A=(np.squeeze(img[:,:]).T)
lonin=ll[:,0]
latin=ll[:,1]

lonin2=lonin.reshape(A.T.shape).T
latin2=latin.reshape(A.T.shape).T

#lonin2=indata[:,0]
#latin2=indata[:,1]
示例#47
0

for d in dirnames:
    print d
    filenames= []
    for (dirpath, dirnames, filenames) in walk('../londiani/%s'%d):
        filenames = sorted(filenames)
            
        
        i = []
        for f in filenames:
            print f
            if f[-3:] =='hdr':
                print f
                
                img = envi.open('../londiani/%s/%s'%(d,f))
                l = img.load()
                m = img.open_memmap()
                i.append(m)
        #got i now process
        print len(i)
        if len(i) == 6:
            big = np.dstack((i[0],i[1],i[2],i[3],i[4],i[5]))
        if len(i) == 5:
            big = np.dstack((i[0],i[1],i[2],i[3],i[4]))
        if len(i) == 4:
            big = np.dstack((i[0],i[1],i[2],i[3]))
        #Do kmeans clustering on original image
        dokmeans = False
        if dokmeans:
            (mm,c) = kmeans(big,maxclasses,20)
示例#48
0
def super(type='e',emax=1.e-10, imax=150,m=2.,image_path="C:\\Users\\jose\\Desktop\\imagenes\\remanso", roi_path="C:\\Users\\jose\\Desktop\\imagenes\\remanso_map_verifi", gt_path="C:\\Users\\jose\\Desktop\\imagenes\\remanso_map_entre",img_dest="C:\\Users\\jose\\Desktop\\",win_size = 3):
    ''''
    Ejecucion del algoritmo FCM,GKFCM,WFCM, de naturaleza supervisada .
    Parameteros:
        image_path: ruta de la imagen a clasificar.
        img_dest:  ruta donde se desea guardar el mapa de clasificacion.
        roi_path:  ruta del mapa de entrenamiento.
        gt_path: ruta del mapa de verificacion.
        type: metodo a ejecutar.
        emax: umbral de error.
        imax: numero de iteraciones maximas permitidas.
        m: grado de borrosidad.
    '''
    
    
    
    from numpy import zeros, transpose, dot, newaxis,compress, indices, reshape, not_equal,amax

    img = create_image_from_envi_header(image_path)
    roi = envi.open( roi_path + '.hdr', roi_path).read_band(0)
    gt = envi.open( gt_path + '.hdr', gt_path).read_band(0)
        
    (nrows, ncols, nbands) = img.shape
    N = nrows * ncols    
    if type == 'e':
        fcm = SupFuzzyCMeans(img,gt,m,False)
    elif type == 'gus':
        fcm = SupGKFuzzyCMeans(img,gt,m,False)
    elif type == 'w':
        fcm = SupWFuzzyCMeans(img,gt,m,False,win_size=win_size)
    
    tiempo_inicial = clock()
    fcm(emax=emax,imax=imax)
    tiempo_final = clock()

    
    membership = fcm.mu
    des=fcm.desvs
    me=fcm.means
    ind_clas =fcm.index_class
    (_,nclas) = membership.shape 
    
    cen=fcm.c 
    count=0
    print membership
    w = zeros(N)
    classified = zeros((N,3))
    table_color = lookup_table()
    for k in range(N):
            maxi = argmax(membership[k,:])
            w[k] = maxi
            classified[k,:]=table_color[maxi+1]
            if amax(membership[k,:]) < 0.51:
                count = count + 1
                w[k] = int(nclas)
                classified[k,:]=table_color[0]
            
    w = w.reshape((nrows,ncols))
    classified = classified.reshape((nrows,ncols,3))
    
    mask_index = numpy.not_equal(roi, 0.0)
     
    inds = transpose(indices((nrows, ncols)), (1, 2, 0))
    inds = reshape(inds, (nrows * ncols, 2))
    inds = compress(numpy.not_equal(mask_index.ravel(), 0), inds, 0).astype('h')
    
    ne= len(inds)
    roi_test = zeros(ne)
    img_test = zeros(ne)
    for i in range(inds.shape[0]):      
        x = roi[inds[i,0],inds[i,1]]
        roi_test[i]= ind_clas[x]
        y = w[inds[i][0], inds[i][1]]
        img_test[i]= int(y)

    
    if type == 'e':
        imagen = save_image(img_dest, "classified_image_by_SFCM", classified)
        print "runtime of FCM is: %f " % (tiempo_final - tiempo_inicial)
    elif type == 'gus':
        imagen = save_image(img_dest, "classified_image_by_SGK", classified)
        print "runtime of GUSTAFON_KESSEL is: %f " % (tiempo_final - tiempo_inicial)
    elif type == 'w':
        imagen = save_image(img_dest, "classified_image_by_SWFCM", classified)
        print "runtime of WFCM is: %f " % (tiempo_final - tiempo_inicial)    
    
    return (img_test,roi_test,count)
示例#49
0
import time
from spectral import *
import spectral.io.envi as envi
img3 = envi.open('./8Dec2013/band3.hdr')
l = img3.load()
m = img3.open_memmap()
s3 = m.astype('uint8')


img4 = envi.open('./8Dec2013/band4.hdr')
l4 = img4.load()
m4 = img4.open_memmap()
s4 = m4.astype('uint8')

img5 = envi.open('./8Dec2013/band5.hdr')
l5 = img5.load()
m5= img5.open_memmap()
s5 = m5.astype('uint8')

import numpy as np

rgb= np.dstack((s5,s4,s3))

v = imshow(rgb)
time.sleep(3)





示例#50
0
文件: 2013.py 项目: landsat/landsat-2
import time
from spectral import *
import spectral.io.envi as envi
import numpy as np


i = []
names = [2,3,4,5,6,7]
for n in names:
	img = envi.open('../londiani/8Dec2013/band%s.hdr'%n)
	l = img.load()
	m = img.open_memmap()
	i.append(m)


big = np.dstack((i[0],i[1],i[2],i[3],i[4],i[5]))
#rgb= np.dstack((s5,s4,s3))

(mm,c) = kmeans(big,20,20)
clusters = {}
for i in range(20):
    clusters[i] = np.sum(mm==i)

print clusters
a =sorted(clusters.items(), key=lambda clusters: clusters[1])
a.reverse()
clusters = a


pc = principal_components(big)
pc_090 = pc.reduce(fraction=0.99)
示例#51
0
import time
from spectral import *
import spectral.io.envi as envi
import numpy as np
import Image

i = []
names = [2,3,4,5,6,7]
for n in names:
	img = envi.open('./8Dec2013/band%s.hdr'%n)
	l = img.load()
	m = img.open_memmap()
	i.append(m)


big = np.dstack((i[0],i[1],i[2],i[3],i[4],i[5]))
#rgb= np.dstack((s5,s4,s3))

(mm,c) = kmeans(big,20,20)
clusters = {}
for i in range(20):
    clusters[i] = np.sum(mm==i)

print clusters
a =sorted(clusters.items(), key=lambda clusters: clusters[1])
a.reverse()
clusters = a


pc = principal_components(big)
pc_090 = pc.reduce(fraction=0.90)
示例#52
0
def load_signal_to_detect(detect_path, detect_hdr_name):
    dfin = osp.join(detect_path,detect_hdr_name)
    sli = envi.open(dfin)
    y = sli.spectra[0,:].tolist()
    return np.array(y)
示例#53
0
matplotlib.pyplot.show()

# scale for natural color display
rows, cols, bands = im.shape
print 'im shape: ', str(im.shape)
rgb = im[:, :, 2:5]
rgbScaled = (rgb * 255.0).astype('uint8')
view = matplotlib.pyplot.imshow(rgbScaled.astype('uint8'))
matplotlib.pyplot.show()

# PIL image module
from PIL import Image

# spectral python
import spectral.io.envi as envi
im = envi.open('/Users/vscholl/Downloads/sr_stacked_scaled/LC80840092015152LGN00/LC80840092015152LGN00_sr_stackedScaled.tif.hdr','/Users/vscholl/Downloads/sr_stacked_scaled/LC80840092015152LGN00/LC80840092015152LGN00_sr_stackedScaled.tif')
rgb = im[:,:,2:5]

# loop through bands
for i in range(0, bands):
    print i

# set a variable for each landsat band used in classification, for readability
coastal = im[:, :, 0].reshape([rows,cols])
blue = im[:, :, 1].reshape([rows,cols])
green = im[:, :, 2].reshape([rows,cols])
red = im[:, :, 3].reshape([rows,cols])
nir = im[:, :, 4].reshape([rows,cols])
swir1 = im[:, :, 5].reshape([rows,cols])
swir2 = im[:, :, 6].reshape([rows,cols])