利用KMeans进行遥感NDWI进行聚类分割

(1)解释

KMeans算法是一种非监督式的聚类算法,于1967年由J. MacQueen提出,聚类的依靠是欧式距离,其核心思想就是将样本划分为几个类别,类里面的数据与类中心的距离最小。类的标签采用类里面样本的均值。

这里利用KMeans进行遥感NDWI归一化水体指数进行简单的聚类分析,主要目的就是聚类出流域和非流域,簇类数为2。手动分割阈值为-0.06,效果和KMeans差不多,若是人为调参太麻烦,可以考虑KMeans进行分割,分割效果如下。

在这里插入图片描述

此程序可以进行常规遥感图像的聚类,但可能代码需做小幅度调整。

(2)源码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@author: 楠楠星球
@time: 2024/5/13 15:12 
@file: kmeans.py-->test
@project: pythonProject
@# ------------------------------------------(one)--------------------------------------
@# ------------------------------------------(two)--------------------------------------
"""
from matplotlib.image import imread
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from sklearn.cluster import KMeans, k_means

# img =imread('NDWI.tif')
img = Image.open('NDWI.tif') #读取的landsat全色影像,若是彩色图像请在此句后面加上.convert("RGB")
NDWI = Image.open('ndwi_006.tif')
img = np.array(img) #转为矩阵
img_bands = 1   #图像的波段或者深度
image = img.reshape(-1, img_bands) #更改图像维度


seg_images = [] #存放处理结果
n_clusters = 2  #要聚类的簇类数

# 随机生成颜色矩阵
colors = [np.random.randint(0, 255, size=(1, img_bands)) for _ in range(n_clusters)]
# 利用KMeans类进行聚类处理,n_clusters表示簇类数,random_state表示随机种子,n_init='auto'为了防止报错,调用.fit()方法进行处理
Kmeans_res = KMeans(n_clusters=n_clusters,random_state=1000, n_init='auto').fit(image)
# 获取簇的质心
cluster_centers = Kmeans_res.cluster_centers_

# 也可利用k_means函数进行处理
# Kmeans_res = Cluster(X=image,n_clusters = 8,random_state=40,n_init='auto')
# cluster_centers = Kmeans_res[0]

# 获取簇类中元素的标签
cluster_labels = cluster_centers[Kmeans_res.labels_]
same = np.unique(cluster_labels, axis=0) #查找每一个簇类的标签

num = 0 #记数
for color in colors:
	for index,row in enumerate(cluster_labels):
		equal = np.array_equal(row, same[num])
		if equal == True:
			cluster_labels[index] = colors[num][0]
		else:
			continue
	num += 1
cluster_image = cluster_labels.reshape(img.shape)
seg_images.append(cluster_image.astype(np.uint8))

plt.figure(figsize=(10,5))
plt.subplot(131)
plt.imshow(img,cmap='gray')
plt.title("NDWI_ori_img")
plt.subplot(132)
if n_clusters == 2:
	plt.imshow(cluster_image/255, cmap='gray')
else:
	plt.imshow(cluster_image/255)
plt.title("NDWI_Kmeans_img")

plt.subplot(133)
plt.imshow(NDWI,cmap='gray')
plt.title('NDWI_img--number:-0.06')
plt.show()
  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楠楠星球

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值