Python K-means实现简单图像聚类的示例代码

作者:xiongxyowo 时间:2023-06-30 10:40:58 

这里直接给出第一个版本的直接实现:


import os
import numpy as np
from sklearn.cluster import KMeans
import cv2
from imutils import build_montages
import matplotlib.image as imgplt

image_path = []
all_images = []
images = os.listdir('./images')

for image_name in images:
   image_path.append('./images/' + image_name)
for path in image_path:
   image = imgplt.imread(path)
   image = image.reshape(-1, )
   all_images.append(image)

clt = KMeans(n_clusters=2)
clt.fit(all_images)
labelIDs = np.unique(clt.labels_)

for labelID in labelIDs:
   idxs = np.where(clt.labels_ == labelID)[0]
   idxs = np.random.choice(idxs, size=min(25, len(idxs)),
replace=False)
   show_box = []
   for i in idxs:
       image = cv2.imread(image_path[i])
       image = cv2.resize(image, (96, 96))
       show_box.append(image)
   montage = build_montages(show_box, (96, 96), (5, 5))[0]

title = "Type {}".format(labelID)
   cv2.imshow(title, montage)
   cv2.waitKey(0)

主要需要注意的问题是对K-Means原理的理解。K-means做的是对向量的聚类,也就是说,假设要处理的是224×224×3的RGB图像,那么就得先将其转为1维的向量。在上面的做法里,我们是直接对其展平:


image = image.reshape(-1, )

那么这么做的缺陷也是十分明显的。例如,对于两张一模一样的图像,我们将前者向左平移一个像素。这么做下来后两张图像在感官上几乎没有任何区别,但由于整体平移会导致两者的图像矩阵逐像素比较的结果差异巨大。以橘子汽车聚类为例,实验结果如下:

Python K-means实现简单图像聚类的示例代码

Python K-means实现简单图像聚类的示例代码

可以看到结果是比较差的。因此,我们进行改进,利用ResNet-50进行图像特征的提取(embedding),在特征的基础上聚类而非直接在像素上聚类,代码如下:


import os
import numpy as np
from sklearn.cluster import KMeans
import cv2
from imutils import build_montages
import torch.nn as nn
import torchvision.models as models
from PIL import Image
from torchvision import transforms

class Net(nn.Module):
   def __init__(self):
       super(Net, self).__init__()
       resnet50 = models.resnet50(pretrained=True)
       self.resnet = nn.Sequential(resnet50.conv1,
                                   resnet50.bn1,
                                   resnet50.relu,
                                   resnet50.maxpool,
                                   resnet50.layer1,
                                   resnet50.layer2,
                                   resnet50.layer3,
                                   resnet50.layer4)

def forward(self, x):
       x = self.resnet(x)
       return x

net = Net().eval()

image_path = []
all_images = []
images = os.listdir('./images')

for image_name in images:
   image_path.append('./images/' + image_name)
for path in image_path:
   image = Image.open(path).convert('RGB')
   image = transforms.Resize([224,244])(image)
   image = transforms.ToTensor()(image)
   image = image.unsqueeze(0)
   image = net(image)
   image = image.reshape(-1, )
   all_images.append(image.detach().numpy())

clt = KMeans(n_clusters=2)
clt.fit(all_images)
labelIDs = np.unique(clt.labels_)

for labelID in labelIDs:
idxs = np.where(clt.labels_ == labelID)[0]
idxs = np.random.choice(idxs, size=min(25, len(idxs)),
replace=False)
show_box = []
for i in idxs:
image = cv2.imread(image_path[i])
image = cv2.resize(image, (96, 96))
show_box.append(image)
montage = build_montages(show_box, (96, 96), (5, 5))[0]

title = "Type {}".format(labelID)
cv2.imshow(title, montage)
cv2.waitKey(0)

可以发现结果明显改善:

Python K-means实现简单图像聚类的示例代码

Python K-means实现简单图像聚类的示例代码

来源:https://blog.csdn.net/qq_40714949/article/details/120854418

标签:Python,K-means,图像聚类
0
投稿

猜你喜欢

  • nodejs开发——express路由与中间件

    2024-05-11 10:18:04
  • 关于多种方式完美解决Python pip命令下载第三方库的问题

    2023-02-25 13:40:11
  • 配置 SQL Server 2005 以允许远程连接的方法

    2024-01-13 12:58:40
  • 关于网站导航设计的探讨

    2008-02-28 13:20:00
  • python中numpy.zeros(np.zeros)的使用方法

    2023-10-08 17:40:07
  • ubutu 16.04环境下,PHP与mysql数据库,网页登录验证实例讲解

    2023-11-22 08:18:27
  • Maven中央仓库正式成为Oracle官方JDBC驱动程序组件分发中心(推荐)

    2024-01-26 05:26:27
  • 微信小程序自定义tabbar实现突出样式详解流程

    2024-06-14 05:09:52
  • 详谈js遍历集合(Array,Map,Set)

    2024-04-16 09:29:53
  • JS 用6N±1法求素数 实例教程

    2024-04-16 08:44:59
  • 关于Python下载大文件时哪种方式速度更快

    2021-03-15 05:46:37
  • python递归计算N!的方法

    2021-11-11 21:11:31
  • Sqlserver 高并发和大数据存储方案

    2024-01-17 22:45:56
  • Python使用pickle进行序列化和反序列化的示例代码

    2022-11-17 10:46:22
  • python3 使用OpenCV计算滑块拼图验证码缺口位置(场景示例)

    2023-02-27 07:07:18
  • Django 中使用流响应处理视频的方法

    2021-01-12 20:55:13
  • python strip() 函数和 split() 函数的详解及实例

    2021-07-03 09:34:56
  • Python Matplotlib绘制动图平滑曲线

    2022-12-28 22:23:40
  • MySQL中Innodb的事务隔离级别和锁的关系的讲解教程

    2024-01-13 18:20:21
  • asp如何正确显示数据库里同时存在的GB码和BIG5编码?

    2010-06-28 18:26:00
  • asp之家 网络编程 m.aspxhome.com