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.]
使用 Python 和 OpenCV 进行图像聚类
最新推荐文章于 2024-09-15 22:31:42 发布
本文介绍如何利用Python和OpenCV进行图像聚类处理。通过实例展示了从原始图像到聚类图像的转换过程,探讨了在计算机视觉领域的应用。
摘要由CSDN通过智能技术生成