Beispiel #1
0
    def perform(self,
                ndvi_threshold=125,
                data_ignore_value=-9999.,
                NIR=90,
                RED=55,
                min_threshold=0,
                max_threshold=1,
                noisy_bands=None):

        img = read_image(self._img_path)

        if noisy_bands == None:

            noise_rem = noise_removal(img,
                                      min_threshold=min_threshold,
                                      max_threshold=max_threshold)

            noisy_bands = noise_rem.show_noisy_bands()

        retained_bands = self._get_retained_bands(noisy_bands, img.img_bands)

        masking_pixel = [0.0 for i in range(len(retained_bands))]

        print('--------------- Performing Preprocessing ---------------\n')

        for index, each_partion in enumerate(self._list_of_partitions):

            print('\nPartition : {} / {} running...\n'.format(
                index + 1, self._total_partitions))

            sub_image = img.sub_image()[each_partion[0]:each_partion[1], :,
                                        retained_bands]

            sub_image[:, noisy_bands] = 0

            for index_row, each_row in enumerate(sub_image):

                for index_pixel, each_pixel in enumerate(each_row):

                    if (each_pixel[0]
                            == data_ignore_value) or (self._calculate_ndvi(
                                each_pixel[NIR], each_pixel[RED]) <
                                                      ndvi_threshold):
                        sub_image[index_row, index_pixel] = masking_pixel

            if not os.path.exists(self._save_path):
                os.makedirs(self._save_path)

            envi.save_image(self._save_path + self._image_name + '_part_' +
                            str(index + 1) + '.hdr',
                            sub_image,
                            force=True,
                            interleave='bil',
                            ext=None)

            del sub_image

        print('\nPreprocessing completed. Output directory : ' +
              self._save_path)
        print('\n\n---------------------------------------------------------')
    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')
Beispiel #3
0
def compute_t(a_50, b_50, c_50, img_matrix, t_matrix, init_state):
    k = 0.1
    t_method = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    t_max = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    t_decay = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    ss_max = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))

    for i in range(0, img_matrix.shape[0]):
        for j in range(0, img_matrix.shape[1]):
            threshold = k * max_variation[i][j] + c_50[i][j]
            if b_50[i][j] != 0:
                t0 = fsolve(f, 0,
                            [a_50[i][j], b_50[i][j], c_50[i][j], threshold])
                t_method[i][j] = t0
                indexx = np.argmax(img_matrix[i][j])
                t_max[i][j] = t_matrix[indexx]
                ss_max[i][j] = img_matrix[i][j][indexx]

                t_decay[i][j] = t_method[i][j] - t_max[i][j]
            if t0 <= 0 or t_decay[i][j] < 0:
                t_method[i][j] = -1
                t_decay[i][j] = -1
    # envi.save_image('t_method1_01.hdr', t_method)
    # envi.save_image('t_max1_01.hdr', t_max)
    # envi.save_image('t_decay1_01.hdr', t_decay)
    envi.save_image('ss_max.hdr', ss_max)
Beispiel #4
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')
Beispiel #5
0
def image_stactistic(a_50, b_50, c_50, img_matrix, t_matrix, init_state):
    t_max = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    t_min = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    ss_max = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    ss_min = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    max_avg = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))

    for i in range(0, img_matrix.shape[0]):
        for j in range(0, img_matrix.shape[1]):

            indexx = np.argmax(img_matrix[i][j])
            t_max[i][j] = t_matrix[indexx]
            ss_max[i][j] = img_matrix[i][j][indexx]
            # to avoid seize no data as t_min and ss_min
            img_temp = []
            t_temp = []
            for k in range(len(img_matrix[i][j])):
                if img_matrix[i][j][k] != 0:
                    img_temp.append(img_matrix[i][j][k])
                    t_temp.append(t_matrix[k])
            if len(img_temp) > 0:
                indexy = np.argmin(img_temp)
                t_min[i][j] = t_temp[
                    indexy] + 8.5  # because GOCI start from 8:30, but the time start from 0. As for t_max, I use seadas to change that
                ss_min[i][j] = img_temp[indexy]


########################################################
# max_avg[i][j] = ss_max[i][j]- init_state[i][j]

    envi.save_image('D:/HOMEWORK/rslab/IOCS_2019/code/New_range/t_max.hdr',
                    t_max)
    # envi.save_image('t_min.hdr', t_min)
    envi.save_image('D:/HOMEWORK/rslab/IOCS_2019/code/New_range/ss_max.hdr',
                    ss_max)
Beispiel #6
0
    def remove_bands(self, hdr_file, list_of_noisy_bands=None):
        """
        Saves an image to disk.

        Arguments:

            `hdr_file` (str):

                Header file (with ".hdr" extension) name with path.

            `list_of_noisy_bands` (list) optional:

                If passed, bands specified in `list_of_noisy_bands` will be removed
                , otherwise noisy bands will be detected and removed automatically.


        """

        if list_of_noisy_bands == None:
            discarded_bands = self.show_noisy_bands()
        else:
            discarded_bands = list_of_noisy_bands

        retained_bands = list(
            set(np.arange(self._bands)) - set(discarded_bands))

        envi.save_image(hdr_file,
                        self._image.sub_image()[:, :, retained_bands],
                        interleave='bil',
                        force=True,
                        ext=None)
    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
Beispiel #8
0
def fy3d2modis_met(in_file, out_file_met, metadata_pickle):
    """
    缺少5通道和32通道
    :param in_file:
    :param out_file_met:
    :param metadata_pickle:  hdr 头信息
    :return:
    """
    datas = np.zeros((200, 2), dtype=np.int8)
    data_loader = ReadMersiL1(in_file)

    flag = data_loader.get_day_night_flag()
    flag[np.isnan(flag)] = 1
    flag[flag == 2] = 1  # Mix 设置为晚上
    datas[:, 0] = flag.astype(np.int8)

    mirror = np.zeros((200, 1), dtype=np.int8)
    mirror[mirror % 2 == 0] = 1
    datas[:, 1] = mirror

    metadatas = pickle.load(metadata_pickle)
    metadata = metadatas.get('met')
    _metadata = metadata.get('metadata')
    _interleave = metadata.get('interleave')
    envi.save_image(out_file_met,
                    datas,
                    metadata=_metadata,
                    interleave=_interleave,
                    force=True)
    print('>>> {}'.format(out_file_met))
    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
Beispiel #10
0
def fy3d2modis_1km(in_file, out_file_1km, metadata_pickle):
    """
    缺少5通道和32通道
    :param in_file:
    :param out_file_1km:
    :param metadata_pickle:  hdr 头信息
    :return:
    """
    datas = np.zeros((2000, 2048, 36), dtype=np.float)
    data_loader = ReadMersiL1(in_file)

    data_map = {
        1: 'CH_03',
        2: 'CH_04',
        3: 'CH_01',
        4: 'CH_02',
        5: 'CH_05',  # 没有5通道的对应通道,暂时使用CH_05
        6: 'CH_06',
        7: 'CH_07',
        8: 'CH_08',
        9: 'CH_09',
        10: 'CH_10',
        12: 'CH_11',
        13: 'CH_12',
        15: 'CH_14',
        16: 'CH_15',
        17: 'CH_16',
        18: 'CH_17',
        19: 'CH_18',
        20: 'CH_20',
        23: 'CH_21',
        26: 'CH_05',
        28: 'CH_22',
        29: 'CH_23',
        31: 'CH_24',
        32: 'CH_24',  # 没有5通道的对应通道,暂时使用CH_24
    }

    refs = data_loader.get_ref()
    for channel in refs:
        _data = refs[data_map[channel]]
        _data[np.isnan(_data)] = -1
        datas[:, :, channel - 1] = _data
    rads = data_loader.get_rad()
    for channel in rads:
        _data = rads[data_map[channel]]
        _data[np.isnan(_data)] = -1
        datas[:, :, channel - 1] = _data

    metadatas = pickle.load(metadata_pickle)
    metadata = metadatas.get('1000m')
    _metadata = metadata.get('metadata')
    _interleave = metadata.get('interleave')
    envi.save_image(out_file_1km,
                    datas,
                    metadata=_metadata,
                    interleave=_interleave,
                    force=True)
    print('>>> {}'.format(out_file_1km))
Beispiel #11
0
def save_as_envi(img_matrix,total_image_num):
    # let img_matrix[t][row][col] become img_matrix[row][col][t] because the third index will become the band numbers.
    img_matrix2 = np.zeros([row,col,total_image_num]) # 8 images * 8 days
    for t in range(img_matrix.shape[0]):
        for i in range(img_matrix.shape[1]):
            for j in range(img_matrix.shape[2]):
                img_matrix2[i][j][t] = img_matrix[t][i][j] 
    envi.save_image('D:/HOMEWORK/rslab/IOCS_2019/code/New_range/img_matrix_all.hdr',img_matrix2)
Beispiel #12
0
 def save_envi(self, path, img, metadata={}):
     from spectral.io import envi
     basename = os.path.basename(path)
     dest = os.path.join(tempdir, basename)
     envi.save_image(dest, img, metadata=metadata, force=True)
     self.upload_file(f"{dest.split('.')[0]}.img",
                      f"{path.split('.')[0]}.raw")
     return self.upload_file(dest, path)
Beispiel #13
0
def save_envi_image(Spec, filename):
    md = {
        'lines': Spec.shape[0],
        'samples': Spec.shape[1],  ## pixel count per line
        'bands': Spec.shape[2],  ## wavelength count
        'data type': 5
    }  ##5 =dble precision, 4= float, 12 = uint16
    envi.save_image(filename + '.hdr', Spec, metadata=md, force=True)
    return
def tiff2envi(ofile):

  # loop over _refl and _mask file
  for o in [ofile + '_refl.tif',ofile + '_mask.tif']:
    # open tiff file
    dataset = gdal.Open(o, GA_ReadOnly)
    # read the tiff data
    data = dataset.ReadAsArray()

    # set up metadata for envi file

    md = {'lines': dataset.RasterYSize,
          'samples': dataset.RasterXSize,
          'bands': dataset.RasterCount}
    # save envi file

    envi.save_image(o.replace('tif','hdr'),data,metadata=md,\
                        dtype='float32',interleave='bip',force=True)
Beispiel #15
0
def write_envi(img,hdr_file):
    """
    save image as envi

    :param img:
    :param hdr_file:
    :return:
    """
    header = dict(HDR_HEADER)
    header["lines"] = str(img.shape[0])
    header["samples"] = str(img.shape[0]*img.shape[1])
    header["data type"] = envi.dtype_to_envi[img.dtype.char]
    header["description"] = "Imagen: "+os.path.basename(hdr_file)
    envi.save_image(hdr_file,
                    img,metadata=header,
                    dtype=img.dtype.char,
                    force=True,
                    interleave=header["interleave"],
                    byteorder=header['byte order'])
Beispiel #16
0
def crop_and_save(input_path, output_path, output_path_rgb, rgb_bands, r):
    img = envi.open(input_path)
    # Crop datacube with ROI
    img_subset = img.read_subimage(rows=range(r[1], r[1] + r[3]),
                                   cols=range(r[0], r[0] + r[2]))
    envi.save_image(
        output_path,
        img_subset,
        force=True  # Overwrites already existing file
    )
    print("Successfully cropped hyperspectral datacube to", output_path,
          "cropped at", r)

    # Crop RGB with ROI
    im = cv2.imread(output_path_rgb)
    output_path_rgb = output_path[:-3] + 'png'
    cropped_rgb = im[r[1]:r[1] + r[3], r[0]:r[0] + r[2]]
    cv2.imwrite(output_path_rgb, cropped_rgb)
    print("Successfully cropped RGB to", output_path_rgb, "cropped at", r)
def tiff2envi(ofile):

    # loop over _refl and _mask file
    for o in [ofile + '_refl.tif', ofile + '_mask.tif']:
        # open tiff file
        dataset = gdal.Open(o, GA_ReadOnly)
        # read the tiff data
        data = dataset.ReadAsArray()

        # set up metadata for envi file

        md = {
            'lines': dataset.RasterYSize,
            'samples': dataset.RasterXSize,
            'bands': dataset.RasterCount
        }
        # save envi file

        envi.save_image(o.replace('tif','hdr'),data,metadata=md,\
                            dtype='float32',interleave='bip',force=True)
Beispiel #18
0
    def fnCRISM_createCRImg(self, imgName, bgFileName):
        """
        This function can be used to create a continuum removed version of the TER images

        :param imgName: the simple fileName
        :param bgFileName: the associated continuum file
        ----------------------------------------------------------------------------------------------------------------
        OUTPUT
        ----------------------------------------------------------------------------------------------------------------
        :return: outFileName: The name of the file with the convex background
        """

        'The image name is'
        imgHdrName = imgName.replace(".IMG", ".HDR")
        imgHdrName = imgHdrName.replace(".img", ".hdr")
        'Now load the image'
        img = envi.open(imgHdrName, imgName)
        cube = img.load()
        [rows, cols, bands] = img.shape

        'The background image is'
        bgHdrName = bgFileName.replace(".img", ".hdr")
        bgHdrName = bgHdrName.replace(".IMG", ".HDR")
        'Now load the image'
        bgImg = envi.open(bgHdrName, bgFileName)
        header = envi.read_envi_header(bgHdrName)
        bgCube = bgImg.load()

        cube_cr = np.empty((rows, cols, bands), dtype=np.float32)
        cube_cr[:] = np.nan
        cube_cr[:, :, self.strtBand:self.stopBand] = \
            (cube[:, :, self.strtBand:self.stopBand]) / (bgCube[:, :, self.strtBand:self.stopBand])
        outFileName = bgHdrName.replace('_Bg.hdr', '_CR.hdr')
        envi.save_image(outFileName,
                        cube_cr,
                        dtype='single',
                        force=True,
                        interleave='bil',
                        metadata=header)

        return outFileName.replace('.hdr', '.img')
Beispiel #19
0
def main():
    parser = argparse.ArgumentParser(description='Transform an ENVI image')
    parser.add_argument('hdrfile')
    parser.add_argument('--dynamic_tie_points', '-d', action='store_true')
    args = parser.parse_args()
    hdrfile = args.hdrfile
    out_filename = hdrfile[:-4] + '_parameter.hdr'
    if os.path.exists(out_filename):
        print(f'{out_filename} already exists. Quitting...')
        return
    img = envi.open(hdrfile).load()
    lines, samples, bands = img.shape
    print(f'{lines} lines, {samples} samples, {bands} bands')
    dt = img.dtype
    ties = DEFAULT_TIE_POINTS
    glass = GLASS_BAND_POINTS
    depth_wavelengths = BAND_DEPTH_POINTS
    dynamic_tie_ranges = None
    if args.dynamic_tie_points:
        dynamic_tie_ranges = TIE_POINT_RANGES
    center_range = BAND_CENTER_RANGE
    wavelengths = np.array(img.metadata['wavelength'], dtype=dt)
    if DATA_IGNORE_VALUE_NAME in img.metadata:
        ignore_value = dt.type(img.metadata[DATA_IGNORE_VALUE_NAME])
        print(f'{DATA_IGNORE_VALUE_NAME} = {ignore_value}')
    else:
        ignore_value = None
        print(f'{DATA_IGNORE_VALUE_NAME} not set.')
    md = metadata(img.metadata, hdrfile, depth_wavelengths)
    interleave = img.metadata['interleave']
    out = transform_image(img,
                          wavelengths=wavelengths,
                          ties=ties,
                          glass=glass,
                          depth_wavelengths=depth_wavelengths,
                          dynamic_tie_ranges=dynamic_tie_ranges,
                          center_range=center_range,
                          ignore_value=ignore_value)
    envi.save_image(out_filename, out,
                    metadata=md, interleave=interleave)
    def hsiSpatialSmoothing(self, crImgName, filtersize=5):
        """
        This function can be used to perform a spatial smoothing on an Hyperspectral image and save it.

        :param crImgName: the name of the file we want to smooth
        :param filtersize: the filter size
        :return: the name of the saved smoothed image
        """
        crHdrName = crImgName.replace('.img', '.hdr')
        header = envi.read_envi_header(crHdrName)
        'Read in the background image'
        crImg = envi.open(crHdrName, crImgName)
        crCube = crImg.load()
        [rows, cols, bands] = crCube.shape
        t1 = crImgName.rfind('/')
        temp = crImgName[(t1+1):]
        if ((temp.find('FRT') != -1) or (temp.find('FRS') != -1)):
            strtCol = 29
            stopCol = -7
        else:
            if ((temp.find('HRL') != -1) or (temp.find('HRS') != -1)):
                strtCol = 15
                stopCol = -4

        crCube = crCube[:, strtCol:stopCol, 4:244]

        'Initialize matrix to nans'
        crCube_smoothed = np.empty((rows, cols, bands), dtype=float)
        crCube_smoothed[:] = np.nan

        for ii in range(240):
            bandImg = np.squeeze(crCube[:, :, ii])
            bandImg_smooth = ndimage.uniform_filter(bandImg, size=filtersize)
            crCube_smoothed[:, strtCol:stopCol, ii + 4] = bandImg_smooth

        outFileName = crImgName.replace('.img', ('_smoothed' + str(filtersize) + '.hdr'))
        envi.save_image(outFileName, crCube_smoothed, dtype=np.float32, force=True,
                        interleave='bil', metadata=header)

        return outFileName
Beispiel #21
0
def save():
    file_name = filedialog.askopenfilename(initialdir=os.path.expanduser("/"),
                                           filetypes=(("ENVI", "*.envi"),
                                                      ("All files", "*")))
    save_dir = filedialog.askdirectory()
    myfile = envi.open(file_name + ".hdr")
    imageArray = 10000 * myfile[:, :, :]
    save_file = envi.save_image(save_dir + file_name.split("/")[-1] + ".hdr",
                                imageArray,
                                dtype=np.int16,
                                metadata=myfile.metadata,
                                force=True)
    return save_file
    def crismImgSmooth(self, imgName, filterSize=5):
        """
        This function smooths each band of the hyperspectral image band by band

        :param imgName: Name of the image to be smoothed
        :param filterSize: The kernel size of the boxcar(uniform) filter
        :return:
        """
        imgHdrName = imgName.replace('.img', '.hdr')
        header = envi.read_envi_header(imgHdrName)

        'Read in the background image'
        crImg = envi.open(imgHdrName, imgName)
        crCube = crImg.load()
        [rows, cols, bands] = crCube.shape

        'Initialize matrix to nans'
        crCube_smoothed = np.empty((rows, cols, bands), dtype=float)
        crCube_smoothed[:] = np.nan

        for ii in range(self.strtBand, self.stopBand, 1):
            bandImg = np.squeeze(crCube[self.strtRow:(rows + self.stopRow),
                                        self.strtCol:(cols + self.stopCol),
                                        ii])
            bandImg_smooth = ndimage.uniform_filter(bandImg, size=filterSize)
            crCube_smoothed[self.strtRow:(rows + self.stopRow),
                            self.strtCol:(cols + self.stopCol),
                            ii] = bandImg_smooth

        outFileName = imgName.replace(
            '_CRnR.img', ('_smoothed' + str(filterSize) + '_CR.hdr'))
        envi.save_image(outFileName,
                        crCube_smoothed,
                        dtype=np.float32,
                        force=True,
                        interleave='bil',
                        metadata=header)

        return outFileName.replace('.hdr', '.img')
    def hsiFlip(self, imgName):
        """
        This function can be used to flip the image upside down. This is often required in the case of CRISM images as
        the data is the pds is arranged in the reverse order

        :param imgName: The address of the image to be flipped
        ----------------------------------------------------------------------------------------------------------------
        OUTPUT
        ----------------------------------------------------------------------------------------------------------------
        :return: outFileName: The name of the file with the convex background
        """

        imgHdrName = imgName.replace(".img", ".hdr")
        'Now load the image'
        img = envi.open(imgHdrName, imgName)
        header = envi.read_envi_header(imgHdrName)
        cube = img.load()
        [_, _, bands] = img.shape

        'Get the wavelength information'
        wvl = header['wavelength']
        wvl = np.asarray(wvl, dtype=np.float32)

        'Flip the image and the wavelengths'
        cube_flip = np.flip(cube, axis=2)
        wvl = np.flip(wvl, axis=0)
        header['wavelength'] = wvl

        if header['default bands']:
            defaultBands = np.asarray(header['default bands'], dtype=np.int)
            defaultBands = bands - defaultBands
            header['default bands'] = defaultBands

        'Save the flipped data'
        outFileName = imgName.replace(".img", "_flip.hdr")
        envi.save_image(outFileName, cube_flip, dtype='single', force=True, interleave='bil', metadata=header)

        return outFileName
Beispiel #24
0
def compute_t4(a_50, b_50, c_50, img_matrix, t_matrix, init_state):
    # this function is used to fullfish Prof. Wang's request (20190905)
    k1 = 0.5  # the only varibale to adjust
    k2 = 1 - k1
    t_method = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    t_max = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    t_decay = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    for i in range(0, img_matrix.shape[0]):
        for j in range(0, img_matrix.shape[1]):
            threshold = k1 * max_variation[i][j] + k2 * init_state[i][j]
            t0 = -1
            if b_50[i][j] != 0:
                t0 = fsolve(f, 0,
                            [a_50[i][j], b_50[i][j], c_50[i][j], threshold])
                t_method[i][j] = t0
                indexx = np.argmax(img_matrix[i][j])
                t_max[i][j] = t_matrix[indexx]
                t_decay[i][j] = t_method[i][j] - t_max[i][j]
            if t0 <= 0 or t_decay[i][j] < 0:
                t_method[i][j] = -1
                t_decay[i][j] = -1
    envi.save_image('D:/HOMEWORK/rslab/IOCS_2019/code/New_range/td50.hdr',
                    t_method)
Beispiel #25
0
def fy3d2modis_geo(in_file, out_file_geo, metadata_pickle):
    """
    缺少5通道和32通道
    :param in_file:
    :param out_file_geo:
    :param metadata_pickle:  hdr 头信息
    :return:
    """
    datas = np.zeros((2000, 2048, 8), dtype=np.float)
    data_loader = ReadMersiL1(in_file)

    data_map = {
        0: data_loader.get_latitude,
        1: data_loader.get_longitude,
        2: data_loader.get_sensor_zenith,
        3: data_loader.get_sensor_azimuth,
        4: data_loader.get_solar_zenith,
        5: data_loader.get_solar_azimuth,  # 没有5通道的对应通道,暂时使用CH_05
        6: data_loader.get_hight,
        7: data_loader.get_land_sea_mask,
    }

    for channel in data_map:
        _data = data_map[channel]()
        _data[np.isnan(_data)] = -1
        datas[:, :, channel] = _data

    metadatas = pickle.load(metadata_pickle)
    metadata = metadatas.get('geo')
    _metadata = metadata.get('metadata')
    _interleave = metadata.get('interleave')
    envi.save_image(out_file_geo,
                    datas,
                    metadata=_metadata,
                    interleave=_interleave,
                    force=True)
    print('>>> {}'.format(out_file_geo))
Beispiel #26
0
def do_regression5(img_matrix, t_matrix,row,col):
    # start from the point right after the max point for problematic area.
    a_matrix = np.zeros((row, col))
    b_matrix = np.zeros((row, col))
    c_matrix = np.zeros((row, col))
    ls = np.zeros((row, col))
    for i in range(row):
        for j in range(col):
            ss_temp = []
            t_temp = [] #this array restore the hour 
            for t in range(total_image_num):
                if img_matrix[t][i][j] > 0:
                    ss_temp.append(img_matrix[t][i][j]) # temporary matrix to secure the sequence of value in different time
                    t_temp.append(t_matrix[t]) # temporary matrix to secure the hours correlate to the m_temp
            ## find the max value
            t_use = [] # matrix used to regression
            ss_use = []
            if len(t_temp) >5:
                for n in range(ss_temp.index(max(ss_temp))+1, len(ss_temp)):
                    t_use.append(t_temp[n])
                    ss_use.append(ss_temp[n])
                            # regression: concentration = a*e^(-b*t)+c
            if len(t_use)>5 :
                try:
                    [popt, pcov] = curve_fit(regression_form, t_use, ss_use,maxfev = 200,p0=[1,0.011,ss_use[len(ss_use)-1]],bounds=([0,0,0.5],[np.inf,1.5,10]))
                    a_matrix[i][j] = popt[0]
                    b_matrix[i][j] = popt[1]
                    c_matrix[i][j] = popt[2]
                    
                    for n in range(ss_temp.index(max(ss_temp))+1, len(ss_temp)):
                        y = np.float64(a_matrix[i][j]*np.exp(-1*b_matrix[i][j]*t_temp[n])+c_matrix[i][j])
                        ls[i][j] = ls[i][j] + np.power((y-ss_temp[n]),2)

                except:
                    a_matrix[i][j] =0
                    b_matrix[i][j] = 0
                    c_matrix[i][j] = 0
                    continue

    envi.save_image('a2.hdr', b_matrix)
    envi.save_image('b2.hdr', b_matrix)
    envi.save_image('c2.hdr', c_matrix)
Beispiel #27
0
def compute_t3(a_50, b_50, c_50, img_matrix, t_matrix, init_state):
    # threshold is based on 4 days mean plus a constant
    t_method = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    t_max = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    t_decay = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    for i in range(0, img_matrix.shape[0]):
        for j in range(0, img_matrix.shape[1]):
            threshold = 0.5 + init_state[i][j]
            if b_50[i][j] != 0:
                t0 = fsolve(f, 0,
                            [a_50[i][j], b_50[i][j], c_50[i][j], threshold])
                t_method[i][j] = t0
                indexx = np.argmax(img_matrix[i][j])
                t_max[i][j] = t_matrix[indexx]
                t_decay[i][j] = t_method[i][j] - t_max[i][j]
            if t0 <= 0 or t_decay[i][j] < 0:
                t_method[i][j] = -1
                t_decay[i][j] = -1
    envi.save_image('t_method3_05.hdr', t_method)
    envi.save_image('t_max3_05.hdr', t_max)
    envi.save_image('t_decay3_05.hdr', t_decay)
Beispiel #28
0
def compute_t2(a_50, b_50, c_50, img_matrix, t_matrix, init_state):
    # threshold is based on 4 days mean
    k = 0.1
    t_method = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    t_max = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    t_decay = np.zeros((img_matrix.shape[0], img_matrix.shape[1]))
    for i in range(0, img_matrix.shape[0]):
        for j in range(0, img_matrix.shape[1]):
            threshold = k * max_variation[i][j] + init_state[i][j]
            if b_50[i][j] != 0:
                t0 = fsolve(f, 0,
                            [a_50[i][j], b_50[i][j], c_50[i][j], threshold])
                t_method[i][j] = t0
                indexx = np.argmax(img_matrix[i][j])
                t_max[i][j] = t_matrix[indexx]
                t_decay[i][j] = t_method[i][j] - t_max[i][j]
            if t0 <= 0 or t_decay[i][j] < 0:
                t_method[i][j] = -1
                t_decay[i][j] = -1
    envi.save_image('t_method2_01.hdr', t_method)
    envi.save_image('t_max2_01.hdr', t_max)
    envi.save_image('t_decay2_01.hdr', t_decay)
Beispiel #29
0
def image_statistic(img_matrix,img_row,img_col):
    #### generate mean and variance
    img_mean = np.zeros((row, col))
    img_variance = np.zeros((row, col))
    img_max_difference = np.zeros((row, col))
#### input a 3D array, make it into 2D image
    for i in range(row):
        for j in range(col):
            m_temp = []
            for t in range(total_image_num):
                if img_matrix[t][i][j] > 0:
                    m_temp.append(img_matrix[t][i][j])
            if len(m_temp)>3:
                img_mean[i][j] = np.mean(m_temp)
                img_variance[i][j] = np.var(m_temp)
                img_max_difference[i][j] = max(m_temp)-min(m_temp)
            

    ####Save as image in ENVI format
    envi.save_image('D:/HOMEWORK/rslab/IOCS_2019/code/New_range/Mean.hdr', img_mean)
    envi.save_image('D:/HOMEWORK/rslab/IOCS_2019/code/New_range/Variance.hdr', img_variance)
    envi.save_image('D:/HOMEWORK/rslab/IOCS_2019/code/New_range/max_difference.hdr', img_max_difference)
Beispiel #30
0
import os

if __name__ == "__main__":
    srcFolder = '/Volume1/data/CRISM/jezeroCoRegistration/Registered Images/FRT00005C5E_trail/'

    for r, d, f in os.walk(srcFolder):
        for file in f:
            if file.find('_warp.dat') != -1:
                if file.find('.enp') == -1:
                    'The image name is'
                    imgName = os.path.join(r, file)
                    hdrName = imgName.replace('.dat', '.hdr')

                    'Read in and load image'
                    img = envi.open(hdrName, imgName)
                    cube = np.asarray(img.load())
                    cube[cube >= 1] = 0

                    'Read in the header'
                    hdr = envi.read_envi_header(hdrName)
                    hdr['data ignore value'] = 0

                    'Write this updated file'
                    outName = imgName.replace('.dat', '_zf.hdr')
                    envi.save_image(outName,
                                    cube,
                                    dtype=np.float32,
                                    force=True,
                                    interleave='bil',
                                    metadata=hdr)
Beispiel #31
0
import spectral.io.envi as envi
import numpy as np

imgName = '/Volume1/data/CRISM/AMS/crism_fandango/UMass_Pipeline/HRL000040FF/HRL000040FF_07_IF183L_TRR3_atcr_sabcondv3_1_Lib1112_1_4_5_l1_gadmm_a_v2_ca_MS_smoothed5_CR_micaMaps_BestGuess_v1.img'
hdrName = imgName.replace('.img', '.hdr')
img = envi.open(hdrName, imgName)
header = envi.read_envi_header(hdrName)
cube = img.load()
[rows, cols, bands] = cube.shape

cubeRed = np.empty((rows, cols, (bands - 1)))
cubeRed[:, :, :1] = cube[:, :, :1]
cubeRed[:, :, 2] = np.squeeze(cube[:, :, 2] + cube[:, :, 3])
cubeRed[:, :, 3:] = cube[:, :, 4:]

endMem_Name = header['band names']
endMem_Name.remove(u'Mg-Carbonate-2 [5x5 AVG]')
header['band names'] = endMem_Name

imgName = imgName.replace('_v1.img', '_v2.hdr')
envi.save_image(imgName,
                cubeRed,
                dtype='single',
                force=True,
                interleave='bil',
                metadata=header)