全国服务热线:

15861139266

使用 Python 和 OpenCV 进行图像聚类,苏州上位机培训,苏州机器视觉培训,苏州工业机器人培训
发布时间:2024-01-24 13:36:30 点击次数:186

import numpy as np

import cv2

import matplotlib.pyplot as plt

image=cv2.imread('../input/hillstation/hillstation.jpg')

plt.imshow(image)  #original image

<matplotlib.image.AxesImage at 0x7f8efaabf890>


i=image.sum(axis=2) #convert the shape of image in 2 dimensions

i.shape

(183, 275)

img=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

plt.imshow(img) #convert the image into RGB

<matplotlib.image.AxesImage at 0x7f8efa9e9550>


vector=img.reshape((-1,3)) #convert the mXNX3 image into kX3 matrix where k=mXn and each row will be a vector in 3 dimensions space

vector

array([[ 55,  61, 111],

       [ 55,  61, 111],

       [ 55,  61, 113],

       ...,

       [ 42,  40,  25],

       [ 35,  33,  18],

       [ 28,  26,  13]], dtype=uint8)

vector=np.float32(vector) #convert the uint8 values to float values. k-means method to opencv

vector

array([[ 55.,  61., 111.],

       [ 55.,  61., 111.],

       [ 55.,  61., 113.],

       ...,

       [ 42.,  40.,  25.],

       [ 35.,  33.,  18.],

       [ 28.,  26.,  13.]], dtype=float32)


#clustering into multiple labels as the picture has multiple colours.

c=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10,1.0)


# first parameter is used for stop the criteria if the accuracy is achieved

# second parameter is used for stop the algorithm after he specified number of iterations


# 'c' is the iteration termination process. When the iteration is satisfied, the algorithm will stop.

k=5 #number of clusters

attempts=10 #number of times the algorithm is executed using different labelings.

ret,label,center=cv2.kmeans(vector,k,None,c,attempts, cv2.KMEANS_PP_CENTERS)


#cv2.kmeans_pp_centers is used to specify how initial centers are taken

center=np.uint8(center)

res=center[label.flatten()] #access the label to regenerate the image

im=res.reshape(img.shape)


#visualization

x=8

y=6

plt.figure(figsize=(x,y))

plt.subplot(1,2,1)

plt.imshow(img)

plt.title('original image')

plt.axis(False)

plt.subplot(1,2,2)

plt.imshow(im)

plt.title('Clustered image')

plt.axis(False)

(-0.5, 274.5, 182.5, -0.5)

21.jpg

原始图像与聚类图像


立即咨询
  • 品质服务

    服务贴心周到

  • 快速响应

    全天24小时随时沟通

  • 专业服务

    授权率高,保密性强

  • 完善售后服务

    快速响应需求,及时性服务

直播课程
软件开发基础课程
上位机软件开发课
机器视觉软件开发课
专题课
联系方式
电话:15861139266
邮箱:75607082@qq.com
地址:苏州吴中区木渎镇尧峰路69号
关注我们

版权所有:江苏和讯自动化设备有限公司所有 备案号:苏ICP备2022010314号-1

技术支持: 易动力网络