基于聚类的实时图像分割,我最近也是自己瞎玩,但是这样的情况比较糟糕,实时性差的不行不行的,问题比较多 接着需要优化方法和代码了
# Des:This is a machine learning 0f cluster program!
# Date:2020-3-12
# Author:Gaofeng
import numpy as np
import PIL.Image as image
from sklearn.cluster import KMeans
import cv2
if __name__ == "__main__":
try:
capture=cv2.VideoCapture(0) # 0是代表摄像头编号,只有一个的话默认为0
while(True):
ref,orinal_image=capture.read()
data = []
img = image.fromarray(cv2.cvtColor(orinal_image, cv2.COLOR_BGR2RGB))
row, col = img.size
for i in range(row):
for j in range(col):
x,y,z = img.getpixel((i,j))
data.append([x/256.0, y/256.0, z/256.0])
imgData = np.mat(data)
label = KMeans(n_clusters=3).fit_predict(imgData)
label = label.reshape([row,col])
pic_new =