Quantitative investment study notes 29 - "Python application of machine learning" course notes 03

Practical application clustering, image segmentation.
Using the image feature image into a plurality of non-overlapping regions.
Commonly used methods threshold segmentation, edge division, histogram method, a particular theory (cluster, wavelet analysis based, etc.).
Example: the image pixel color image using the cluster to divide the k-means clustering algorithm.
Output: the same cluster point denoted with the same color, different clusters of pixels represented in different colors.
The pixels read from picture library using PIL color conversion to the [0,1] range.
Open = F (filePath, "RB")
Data = []
IMG = Image.open (F)
m, = n-img.size
for I in Range (m):
for J in Range (n-):
X, Y, Z img.getpixel = ((I, J))
data.append ([X / 256.0, Y / 256.0, Z / 256.0])
f.close ()
are clustered pixel color data K-Means algorithm.
imgData, Row, the loadData COL = ( "test.jpg")
km = KMeans (= n_clusters. 3)
label = km.fit_predict (imgData)
label = label.reshape ([Row, COL])
label is a one-dimensional data, converted the image to be the same shape.
The final output to the picture with the following results:
Artwork

The processed images

本文代码:
https://github.com/zwdnet/MyQuant/blob/master/27

我发文章的四个地方,欢迎大家在朋友圈等地方分享,欢迎点“在看”。
我的个人博客地址:https://zwdnet.github.io
我的知乎文章地址: https://www.zhihu.com/people/zhao-you-min/posts
我的博客园博客地址: https://www.cnblogs.com/zwdnet/
我的微信个人订阅号:赵瑜敏的口腔医学学习园地

Guess you like

Origin www.cnblogs.com/zwdnet/p/12382092.html