栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

无监督学习洁化简单图像

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

无监督学习洁化简单图像

最近刚开始接触机器学习 用无监督学习的方法处理了一下自己的头像 整体思路是将图片的每一个像素点作为一个元素 利用K均值聚类将将这些像素点分为两类 黑的全黑白的全白 也可以换各种颜色 先看原图

 这是处理之后的图

 换个颜色

 代买如下

import matplotlib.pyplot as plt
import numpy as np
from sklearn.cluster import KMeans
class img():
 def __init__(self,imgname):
 self.imgname imgname
 def img_read(self):
 data plt.imread(self.imgname)
 return data
 def img_to_pixel(self):
 data self.img_read()
 shape data.shape
 pixel []
 for i in range(shape[0]):
 for j in range(shape[1]):
 pixel.append(data[i,j,:])
 return pixel
 def cluster_KMeans(self):
 pixel self.img_to_pixel()
 imgdata self.img_read()
 shape imgdata.shape
 cluster KMeans(n_clusters 2)
 cluster.fit(pixel)
 label cluster.labels_.reshape(shape[0],shape[1],1)
 img_label np.concatenate([imgdata,label],axis 2)
 return img_label
 def plot_img(self):
 img_label self.cluster_KMeans()
 shape img_label.shape
 fig,ax plt.subplots()
 for i in range(shape[0]):
 for j in range(shape[1]):
 if img_label[i,j,3]! 0:
 img_label[i,j,:3] np.array([[[0, 0, 0]]])
 else:
 img_label[i, j, :3] np.array([[[255,255,255]]])
 ax.imshow(img_label[:,:,:3])
 plt.show()
if __name__ __main__ :
 img img( 1.jpg )
 img.plot_img()

 

 

 

 

 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/267811.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号