博客
关于我
kmeans图像分割
阅读量:80 次
发布时间:2019-02-26

本文共 1658 字,大约阅读时间需要 5 分钟。

 

# -*- coding: utf-8 -*-# @Author  : matthew# @Software: PyCharmimport cv2import matplotlib.pyplot as pltimport numpy as npdef seg_kmeans_gray():    img = cv2.imread('000129.jpg', cv2.IMREAD_GRAYSCALE)    # 展平    img_flat = img.reshape((img.shape[0] * img.shape[1], 1))    img_flat = np.float32(img_flat)    # 迭代参数    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TermCriteria_MAX_ITER, 20, 0.5)    flags = cv2.KMEANS_RANDOM_CENTERS    # 聚类    compactness, labels, centers = cv2.kmeans(img_flat, 2, None, criteria, 10, flags)    # 显示结果    img_output = labels.reshape((img.shape[0], img.shape[1]))    plt.subplot(121), plt.imshow(img, 'gray'), plt.title('input')    plt.subplot(122), plt.imshow(img_output, 'gray'), plt.title('kmeans')    plt.show()if __name__ == '__main__':    seg_kmeans_gray()

 

# -*- coding: utf-8 -*-# @Author  : matthew# @Software: PyCharmimport cv2import matplotlib.pyplot as pltimport numpy as npdef seg_kmeans_color():    img = cv2.imread('000129.jpg', cv2.IMREAD_COLOR)    # 变换一下图像通道bgr->rgb,否则很别扭啊    b, g, r = cv2.split(img)    img = cv2.merge([r, g, b])    # 3个通道展平    img_flat = img.reshape((img.shape[0] * img.shape[1], 3))    img_flat = np.float32(img_flat)    # 迭代参数    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TermCriteria_MAX_ITER, 20, 0.5)    flags = cv2.KMEANS_RANDOM_CENTERS    # 聚类    compactness, labels, centers = cv2.kmeans(img_flat, 2, None, criteria, 10, flags)    # 显示结果    img_output = labels.reshape((img.shape[0], img.shape[1]))    plt.subplot(121), plt.imshow(img), plt.title('input')    plt.subplot(122), plt.imshow(img_output, 'gray'), plt.title('kmeans')    plt.show()if __name__ == '__main__':    seg_kmeans_color()

 

转载地址:http://wjzk.baihongyu.com/

你可能感兴趣的文章
mysql 数据库存储引擎怎么选择?快来看看性能测试吧
查看>>
MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
查看>>
MySQL 数据库的高可用性分析
查看>>
MySQL 数据库设计总结
查看>>
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>