-
Notifications
You must be signed in to change notification settings - Fork 0
/
meanshift.py
66 lines (58 loc) · 2.07 KB
/
meanshift.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'''
Version: 1.0 meanshift
Author: xiawei
Date: 2022-09-11 10:27:29
LastEditors: xiawei
LastEditTime: 2022-12-22 19:56:29
Description:基于均值漂流算法,查找轮廓,在原图上绘制轮廓
'''
import cv2
import numpy as np
img = cv2.imread('1.png')
sp = 20
sr = 30
mean_img = cv2.pyrMeanShiftFiltering(img, sp, sr)
canny_img = cv2.Canny(mean_img, 150, 300)
# 查找轮廓api,原始图像,查找轮廓方式RETR_EXTERNAL为之查找外围,List从里到外从右到左,CCOMP有层级关系没搞懂
# 从大到小从右到左,这里的contours为根据CHAIN_APPROX_SIMPLE设定的点。
# 返回轮廓个数和坐标值
contours, _ = cv2.findContours(
canny_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓,-1为绘制所有轮廓,颜色,线宽。
print('masker', _)
cv2.drawContours(img, contours, -1, (0, 0, 255), 2)
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.imshow('img', img)
cv2.namedWindow('mean_img', cv2.WINDOW_NORMAL)
cv2.imshow('mean_img', mean_img)
cv2.namedWindow('canny_img', cv2.WINDOW_NORMAL)
cv2.imshow('canny_img', canny_img)
cv2.waitKey()
''' 采用meanshift对2d数据聚类,并预测(80,60)的类别,计算准确率'''
'''import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.neighbors import KNeighborsClassifier
from sklearn.cluster import MeanShift, estimate_bandwidth
data = pd.read_csv('data.csv')
print(data.head())
X = data.drop(['labels'], axis=1)
y = data.loc[:, 'labels']
# establish a KNN model
# KNN为监督式模型,必须告知输入是几类
KNN = KNeighborsClassifier(n_neighbors=3)
KNN.fit(X, y)
y_predict_KNN_test = KNN.predict([[80, 60]])
print('y_predict_KNN_test', y_predict_KNN_test)
y_predict_knn = KNN.predict(X)
knn_score = accuracy_score(y, y_predict_knn)
print('knn_score', knn_score)
# 下面是mean shift
# 用estimate_bandwidth
bw = estimate_bandwidth(X, n_samples=500)
print(bw)
MS = MeanShift(bw)
MS.fit(X)
y_predict_ms = MS.predict(X)
print(pd.value_counts(y_predict_ms), pd.value_counts(y))
# 此时的预测结果和原始数据类别标签对不上,需要手动更正标签
'''