Skip to content

Instantly share code, notes, and snippets.

@PratyushTripathy
Created January 24, 2020 17:38
Show Gist options
  • Save PratyushTripathy/3c6ae90c7b2442e6878543828d52e998 to your computer and use it in GitHub Desktop.
Save PratyushTripathy/3c6ae90c7b2442e6878543828d52e998 to your computer and use it in GitHub Desktop.
import os, cv2
from pyrsgis import raster
import numpy as np
inFile = r"E:\CNN_Builtup\l5_Bangalore2011_raw.tif"
normFile = inFile.replace('.tif', '_normalised.tif')
histEqlFile = inFile.replace('.tif', '_histEqualised.tif')
ds, arr = raster.read(inFile)
normBands = np.random.randint(1, size = (ds.RasterCount, ds.RasterYSize, ds.RasterXSize))
histEqlBands = np.random.randint(1, size = (ds.RasterCount, ds.RasterYSize, ds.RasterXSize))
def normalise(band):
min_val, max_val = band.min(), band.max()
outBand = (band - min_val)/(max_val - min_val)
outBand = (outBand * 255).astype(int)
return(outBand)
for n, b in enumerate(arr):
normBands[n, :, :] = normalise8bit(b)
histEqlBands[n, :, :] = cv2.equalizeHist(b.astype(np.uint8))
# Export any one of the below
raster.export(normBands, ds, filename=normFile, bands='all')
raster.export(eqlBands, ds, filename=histEqlBands, bands='all')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment