Python中基于PCA的图像聚类与降维

2024-04-18 09:34:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在对图像数据集进行K均值聚类。首先,我加载图像并存储在numpy数组中。总共有24张图片。我创建的img_数组的形状是(243004003)。运行K意味着,我把它转换成二维数组,现在的形状是(24360000)。我得到了一个0.57的修正后的蓝度分数。在

我不知道,如何使用主成分分析和降维,然后比较图像的聚类精度。在

现在,我需要使用PCA来将维度降到400维,200维,50维,5维,最后是2维。在

    path = "C://Users/shivam/Desktop/data//"
    os.chdir(path)
    for f in os.listdir('.'):
        if f.endswith('.jpg'):
        img = Image.open(f)
        data = np.asarray( img, dtype='uint8' )
        img_array.append(data)
   df = pd.DataFrame({'image_arrays':img_array})
   df['id'] = range(1, len(df) + 1)

   label_list = ['nature','nature','nature','nature','nature','nature','sunset','sunset','sunset','sunset','sunset','sunset','sunset','sunset','sunset','water','water','water','water','water','water','water','water','water']

   df.head()

   img_arr_2D = img_arr.reshape(24,120000) 

   import numpy as np
   import matplotlib.pyplot as plt
   from matplotlib import style
   style.use("ggplot")
   from sklearn.cluster import KMeans

   X = img_array_2D
   kmeans = KMeans(n_clusters=3, max_iter= 100)
   kmeans.fit(X)

   centroid = kmeans.cluster_centers_
   labels = kmeans.labels_

   colors = ["g.","r.","c."]

   for i in range(len(X)):
       print ("coordinate:" , X[i], "label:", labels[i])
       plt.plot(X[i][0],X[i][1],colors[labels[i]],markersize=10)
       plt.show()

   classes = [0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2]
   labels = kmeans.labels_

   from sklearn.metrics.cluster import adjusted_rand_score
   adjusted_rand_score(classes, labels)

Tags: from图像importdfimgdatalabelsplt
1条回答
网友
1楼 · 发布于 2024-04-18 09:34:26

即使使用PCA,结果也不会有太大的改善(要注意,PCA与变量的数量比例很差,您需要数百万张图像才能在这里获得可靠的结果)。在

图像的像素表示不适合作为输入。你想要的东西,例如,同一张颠倒的图片有相同的表现。在

所以你需要进行特征提取。在

你可以做彩色直方图,一袋袋的视觉文字,但要达到最先进的水平,你需要深入学习。在

相关问题 更多 >