免费视频|新人指南|投诉删帖|广告合作|地信网APP下载

查看: 487|回复: 8
收起左侧

Python tif Kmeans K均值聚类

[复制链接]

3

主题

3405

铜板

4

好友

助理工程师

Rank: 5Rank: 5

积分
111
发表于 2022-7-31 09:18 | 显示全部楼层 |阅读模式
import rasterio as rio
from rasterio.plot import show
from sklearn import cluster
from osgeo import gdal, gdal_array
import matplotlib.pyplot as plt
import matplotlib.colors as mc
import numpy as np
# Open the image
elhas_raster = rio.open("C:/Users/Administrator/Desktop/WKL_Eastern/Li_noNAN_V2/stacking/L58.tif")
print(elhas_raster.meta)

# Read, enhance and show the image
elhas_arr = elhas_raster.read()
vmin, vmax = np.nanpercentile(elhas_arr, (5,95))  # 5-95% contrast stretch
fig, ax = plt.subplots(figsize=[20,20], ncols=1,nrows=1)
show(elhas_raster, cmap='gray', vmin=vmin, vmax=vmax, ax=ax)
ax.set_axis_off()
# fig.savefig("elhas.jpg", bbox_inches='tight')
# plt.show()

# print the shape of the original image
elhas_arr.shape
# create an empty array with same dimension and data type
imgxyb = np.empty((elhas_raster.height, elhas_raster.width, elhas_raster.count), elhas_raster.meta['dtype'])
imgxyb.shape

# loop through the raster's bands to fill the empty array
for band in range(imgxyb.shape[2]):
    imgxyb[:,:,band] = elhas_raster.read(band+1)
print(imgxyb.shape)
# convet to 1d array
img1d = imgxyb[:,:,:58].reshape((imgxyb.shape[0]*imgxyb.shape[1],imgxyb.shape[2]))

img1d.shape
# create an object of the classifier and train it
cl = cluster.KMeans(n_clusters=3)
param = cl.fit(img1d)
cl.labels_
# get the labels of the classes and reshape it x-y-bands shape order (one band only)
img_cl = cl.labels_
img_cl = img_cl.reshape(imgxyb[:,:,0].shape)
img_cl.shape
# Create a custom color map to represent our different 4 classes
#cmap = mc.LinearSegmentedColormap.from_list("", ["black","red","green","yellow"])
# Save result
ds = gdal.Open("C:/Users/Administrator/Desktop/WKL_Eastern/Li_noNAN_V2/stacking/L58.tif")
band = ds.GetRasterBand(1)
arr = band.ReadAsArray()
[cols, rows] = arr.shape
format = "GTiff"
driver = gdal.GetDriverByName(format)

outDataRaster = driver.Create("C:/Users/Administrator/Desktop/WKL_Eastern/Li_noNAN_V2/stacking/L58_Kmeans2.tif", rows, cols, 1, gdal.GDT_Byte)
outDataRaster.SetGeoTransform(ds.GetGeoTransform())
outDataRaster.SetProjection(ds.GetProjection())

outDataRaster.GetRasterBand(1).WriteArray(img_cl)
outDataRaster.FlushCache()
del outDataRaster
# Show the resulting array and save it as jpg image
# plt.figure(figsize=[20,20])
# plt.imshow(img_cl, cmap=cmap)
# plt.axis('off')
# plt.savefig("C:/Users/Administrator/Desktop/WKL_Eastern/Li_noNAN_V2/stacking/L09_Kmeans1.tif", bbox_inches='tight')
# plt.show()
print("OK")

3

主题

3405

铜板

4

好友

助理工程师

Rank: 5Rank: 5

积分
111
 楼主| 发表于 2022-7-31 09:22 | 显示全部楼层
:mg:mg:mg:mg
回复 支持 反对

使用道具 举报

26

主题

1万

铜板

96

好友

地信贵宾

Rank: 13Rank: 13Rank: 13Rank: 13

积分
474369

精华勋章宣传勋章爱心勋章优秀斑主地信元老灌水勋章荣誉会员勋章活跃勋章贡献勋章

QQ
发表于 2022-8-1 10:41 手机频道 | 显示全部楼层
haohaoxuexi
回复 支持 反对

使用道具 举报

0

主题

2万

铜板

2

好友

黄金会员

Rank: 23Rank: 23Rank: 23Rank: 23Rank: 23Rank: 23Rank: 23

积分
4896
发表于 2022-8-11 14:32 | 显示全部楼层
膜拜大神,进来学习。
回复 支持 反对

使用道具 举报

33

主题

3万

铜板

19

好友

钻石会员

Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26

积分
7331

活跃勋章

发表于 2022-12-28 16:51 | 显示全部楼层
进来学习
回复

使用道具 举报

2

主题

1万

铜板

7

好友

钻石会员

Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26

积分
6419
发表于 2023-1-10 09:23 | 显示全部楼层
没事来逛逛
回复 支持 反对

使用道具 举报

4

主题

4392

铜板

1

好友

地信院士

Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15

积分
2027
发表于 2024-2-28 10:22 | 显示全部楼层
感谢分享!
回复

使用道具 举报

0

主题

3万

铜板

9

好友

钻石会员

Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26

积分
5549
发表于 2024-3-4 22:46 | 显示全部楼层
感谢楼主分享
回复 支持 反对

使用道具 举报

0

主题

3万

铜板

9

好友

钻石会员

Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26Rank: 26

积分
5549
发表于 2024-3-4 22:47 | 显示全部楼层
感谢楼主分享
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

在线客服
快速回复 返回顶部 返回列表