python 对图像进行聚类分析

复制代码
import cv2
import numpy as np
from sklearn.cluster import KMeans
import time

# 中文路径读取
def cv_imread(filePath, cv2_falg=cv2.COLOR_BGR2RGB):   
    cv_img = cv2.imdecode(np.fromfile(filePath, dtype=np.uint8), cv2_falg)    
    return cv_img

# 自定义装饰器计算时间
def compute_time(func):
    def compute(*args, **kwargs):
        st = time.time()
        result = func(*args, **kwargs)
        et = time.time()
        print('消费时间 %.6f s' % (et - st))
        return result

    return compute



@compute_time
def kmeans_img(image,  num_clusters, show=False):
    # 如果图像是灰度图(单通道),将其转换为三通道
    if len(image.shape) == 2:
        image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
    
    # 将图像的形状进行调整以便进行 K 均值聚类,提高训练速度
    pixels = cv2.resize(image.copy(), None, fx=0.05, fy=0.05, interpolation=cv2.INTER_LINEAR)
    pixels = np.float32(pixels.reshape((-1, 3)))
    
    segmented_pixels = np.float32(image.reshape((-1, 3)))

    # 初始化 KMeans 模型并拟合数据
    kmeans = KMeans(n_clusters=num_clusters)
    kmeans.fit(pixels)

    # 获取每个像素所属的簇标签
    labels = kmeans.predict(segmented_pixels)

    # 根据簇标签,将图像像素值转换为簇中心值
    segmented_image = kmeans.cluster_centers_[labels]
    segmented_image = np.uint8(segmented_image.reshape(image.shape))
    
    if show:
        plt.figure(figsize=(10, 5))

        plt.subplot(1, 2, 1)
        plt.title('Original Image')
        plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
        plt.axis('off')

        plt.subplot(1, 2, 2)
        plt.title('Segmented Image')
        plt.imshow(segmented_image)
        plt.axis('off')

        plt.tight_layout()
        plt.show()
    
    return segmented_image

image_path =r"C:\Users\pc\Pictures\test\快.png"
image = cv_imread(image_path)
kmeans_img(image,4, show=True)

使用opencv内设的kmeans函数:直接原图进行训练,然后获取每个像素点的类,速度慢。上述方法对图像进行一个缩放后,训练模型,然后用模型再预测原图的每个像素点,速度快。

复制代码
def kmeans_img(image, num_clusters, show=True):
    # 如果图像是灰度图(单通道),将其转换为三通道
    if len(image.shape) == 2:
        image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
    # image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    print(image.shape)
    # 将图像的形状进行调整以便进行 K 均值聚类
    pixels = image.reshape((-1, 3))
    pixels = np.float32(pixels)


    # 设定 kmeans 参数并运行算法
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.2)

    _, labels, centers = cv2.kmeans(pixels, num_clusters, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

    # 将图像像素值转换为簇中心值
    centers = np.uint8(centers)
    segmented_image = centers[labels.flatten()]
    segmented_image = segmented_image.reshape(image.shape)
    
    if show:
        # 显示原始图像和分割后的图像
        plt.figure(figsize=(10, 5))

        plt.subplot(1, 2, 1)
        plt.title('Original Image')
        plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
        plt.axis('off')

        plt.subplot(1, 2, 2)
        plt.title('Segmented Image')
        plt.imshow(segmented_image)
        plt.axis('off')

        plt.tight_layout()
        plt.show()
    return segmented_image
相关推荐
贵州数擎科技有限公司11 小时前
NumPy 从数组操作理解深度学习的计算本质
人工智能·numpy
程序媛徐师姐12 小时前
Python基于OpenCV的马赛克画的设计与实现【附源码、文档说明】
python·opencv·django·马赛克绘画·python马赛克绘画系统·马赛克画·python马赛克画
sali-tec14 小时前
C# 基于OpenCv的视觉工作流-章49-人脸检测
图像处理·人工智能·opencv·算法·计算机视觉
贵州晓智信息科技14 小时前
NumPy 从数组操作理解深度学习的计算本质
人工智能·深度学习·numpy
zzwq.17 小时前
数据分析三件套:Numpy、Pandas、Matplotlib
数据分析·numpy·pandas
木心术11 天前
设备管理网管系统:详细下一步行动指南
前端·人工智能·opencv
kcuwu.1 天前
Python数据分析三剑客导论:NumPy、Pandas、Matplotlib 从入门到入门
python·数据分析·numpy
superior tigre1 天前
NumPy 基础使用方法(基础+矩阵运算+Attention)
线性代数·矩阵·numpy
秋月的私语1 天前
遥感影像拼接线优化工具:基于Qt+GDAL+OpenCV的从零到一实践
开发语言·qt·opencv
不懒不懒1 天前
【基于OpenCV+Dlib的人脸相关检测实战:疲劳、年龄性别、表情全实现】
人工智能·opencv·计算机视觉