【发布时间】:2018-08-31 15:27:56
【问题描述】:
我有一张 geotiff 灰度图像,它给了我 (4377, 6172) 二维数组。在第一部分中,我正在为我的压缩算法考虑 (:1024, :1024) 值(总值为 -> 1024 * 1024 = 1048576)。通过该算法,我通过该算法在 finalmatrix list var 中获得了总共 4 个值。在此之后,我对这些值应用 K-means 算法。下面是一个程序:
import numpy as np
from osgeo import gdal
from sklearn import cluster
import matplotlib.pyplot as plt
dataset =gdal.Open("1.tif")
band = dataset.GetRasterBand(1)
img = band.ReadAsArray()
finalmat = [255, 0, 2, 2]
#Converting list to array for dimensional change
ay = np.asarray(finalmat).reshape(-1,1)
fig = plt.figure()
k_means = cluster.KMeans(n_clusters=2)
k_means.fit(ay)
cluster_means = k_means.cluster_centers_.squeeze()
a_clustered = k_means.labels_
print('# of observation :',ay.shape)
print('Cluster Means : ', cluster_means)
a_clustered.shape= img.shape
fig=plt.figure(figsize=(125,125))
ax = plt.subplot(2,4,8)
plt.axis('off')
xlabel = str(1) , ' clusters'
ax.set_title(xlabel)
plt.imshow(a_clustered)
plt.show()
fig.savefig('kmeans-1 clust ndvi08jan2010_guj 12 .png')
在上述程序中,a_clustered.shape= img.shape 行出现错误。我得到的错误如下:
Error line:
a_clustered.shape= img.shape
ValueError: cannot reshape array of size 4 into shape (4377,6172)
<matplotlib.figure.Figure at 0x7fb7c63975c0>
实际上,我想通过我得到的压缩值来可视化原始图像上的聚类。可以给点建议吗
【问题讨论】:
标签: python-3.x cluster-analysis geotiff