acgeospatial / satellite_imagery_python Goto Github PK
View Code? Open in Web Editor NEWSample scripts and notebooks on processing satellite imagery Python Geospatial raster
Sample scripts and notebooks on processing satellite imagery Python Geospatial raster
I submitted an issue a few months ago querying how to run the analysis on multiple images at a time, I think I have managed to write a function to do this however when saving the cluster rasters the code overwrites the previous image. Do you know how this could be resolved? I have attached the code for the function below.
Many thanks,
Chloe
def k_means_segmentation(folder):
images = []
for filename in os.listdir(folder):
img_ds = gdal.Open(os.path.join(folder,filename), gdal.GA_ReadOnly)
ds = gdal.Open(os.path.join(folder, filename))
band = ds.GetRasterBand(2)
arr = band.ReadAsArray()
[cols, rows] = arr.shape
img = np.zeros((img_ds.RasterYSize, img_ds.RasterXSize, img_ds.RasterCount),
gdal_array.GDALTypeCodeToNumericTypeCode(img_ds.GetRasterBand(1).DataType))
for b in range(img.shape[2]):
img[:, :, b] = img_ds.GetRasterBand(b + 1).ReadAsArray()
images.append(img)
for i in images:
new_shape = (i.shape[0] * i.shape[1], i.shape[2])
X = i[:, :, :8].reshape(new_shape)
k_means = cluster.KMeans(n_clusters=5)
k_means.fit(X)
X_cluster = k_means.labels_
X_cluster = X_cluster.reshape(i[:, :, 0].shape)
format = "GTiff"
driver = gdal.GetDriverByName(format)
filename = "/testing_results/k_means.tif"
outDataRaster = driver.Create(filename, rows, cols, 1, gdal.GDT_Byte)
outDataRaster.SetGeoTransform(ds.GetGeoTransform())##sets same geotransform as input
outDataRaster.SetProjection(ds.GetProjection())##sets same projection as input
outDataRaster.GetRasterBand(1).WriteArray(X_cluster)
outDataRaster.FlushCache() ## remove from memory
del outDataRaster
Hi @acgeospatial thanks for this great repo!
I have a question regarding your Clustering_KMeans-Sentinel2.ipynb notebook - do you have any code to run the same clustering algorithm on many images at once as a sort of loop?
I would like to perform this classification on multiple images and having another method than doing each one individually would greatly reduce my workload!
Many thanks,
Chloe
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.