python-opencv3 kmeans图像分类
来源:互联网 发布:数控车床编程实例 简单 编辑:程序博客网 时间:2024/05/02 09:56
参考:http://blog.csdn.net/willduan1/article/details/70335146
异常:
reshape_feature = des.reshape(-1, 1)
AttributeError: 'tuple' object has no attribute 'reshape'
原因:
错误写法:
des = sift.detectAndCompute(gray, None) # 检测并计算描述符
正确写法:Kp,des=sift.detectAndCompute(gray,None)#检测并计算描述符
注意:detect和Compute可以一起写,也可以分开写,
import os, codecsimport cv2import numpy as npfrom sklearn.cluster import KMeansdef get_file_name(path): ''''' Args: path to list; Returns: path with filenames ''' filenames = os.listdir(path) path_filenames = [] filename_list = [] for file in filenames: if not file.startswith('.'): path_filenames.append(os.path.join(path, file)) filename_list.append(file) return path_filenamesdef knn_detect(file_list, cluster_nums, randomState=None): features = [] files = file_list sift = cv2.xfeatures2d.SIFT_create() for file in files: print(file) img = cv2.imread(file) img = cv2.resize(img, (32, 32), interpolation=cv2.INTER_CUBIC) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) print(gray.dtype) des = sift.detectAndCompute(gray, None) # 检测并计算描述符 # Kp,des=sift.detectAndCompute(gray,None)#检测并计算描述符 # des =sift.detect(gray, None)# sift.detectAndCompute(gray, None) # # # 找到后可以计算关键点的描述符 # Kp, des = sift.compute(gray, des) if des is None: file_list.remove(file) continue reshape_feature = des.reshape(-1, 1) features.append(reshape_feature[0].tolist()) input_x = np.array(features) kmeans = KMeans(n_clusters=cluster_nums, random_state=randomState).fit(input_x) return kmeans.labels_, kmeans.cluster_centers_def res_fit(filenames, labels): files = [file.split('/')[-1] for file in filenames] return dict(zip(files, labels))def save(path, filename, data): file = os.path.join(path, filename) with codecs.open(file, 'w', encoding='utf-8') as fw: for f, l in data.items(): fw.write("{}\t{}\n".format(f, l))def main(): path_filenames = get_file_name("./picture/") labels, cluster_centers = knn_detect(path_filenames, 2) res_dict = res_fit(path_filenames, labels) save('./', 'knn_res.txt', res_dict)if __name__ == "__main__": main()
阅读全文
0 0
- python-opencv3 kmeans图像分类
- python-opencv3 kmeans图像分类
- Kmeans 图像分割 by python
- Python OpenCV3 图像操作
- OpenCV3.0 Examples学习笔记(13)-kmeans.cpp-kmeans函数实现对图像位置进行聚类
- python 图像分类
- 利用opencv3读取caffe mode,对图像进行分类
- 利用opencv3读取tensorflow model,对图像进行分类
- OpenCV3.3深度学习模块(DNN)应用-图像分类
- Python小白进阶4:基于Kmeans的图像分割
- Python Kmeans
- python-kmeans
- KNN 图像分类python实现
- kmeans算法分割图像
- OpenCV3计算机视觉Python语言实现(三):使用OpenCV3处理图像
- K均值(kmeans)分类
- python计算机视觉-图像内容分类
- Caffe笔记:python图像识别与分类
- Codeforces 785D Anton and School
- python 练习一
- ubuntu环境下vlcj报错:A fatal error has been detected #C [libc.so.6+0x121024] _IO_file_underflow+0x64
- 关于WebService 的用法demo 总结
- python-opencv3 kmeans图像分类
- python-opencv3 kmeans图像分类
- POJ.2236 Wireless Network (并查集)
- Ubuntu环境下vjudge系统搭建
- 流控制语句-break与label联用
- 尚不清晰或者未学习的东西
- iOS学习笔记-090.彩票05——购彩大厅2_自定义蒙版、popMenu、UIView分类抽取
- 常用参考表对照表
- 作业
- 试密码(20)