资源简介
bow python实现,测试过了,可以正常运行
代码片段和文件信息
# -*- coding: utf-8 -*-
“““
#!/usr/local/bin/python2.7
#python findFeatures.py -t dataset/train/
“““
import argparse as ap
import cv2
import imutils
import numpy as np
import os
from sklearn.externals import joblib
from scipy.cluster.vq import *
from sklearn import preprocessing
from rootsift import RootSIFT
import math
# Get the path of the training set
parser = ap.ArgumentParser()
parser.add_argument(“-t“ “--trainingSet“ help=“Path to Training Set“ required=“True“)
args = vars(parser.parse_args())
# Get the training classes names and store them in a list
train_path = args[“trainingSet“]
#train_path = “dataset/train/“
training_names = os.listdir(train_path)
numWords = 1000
# Get all the path to the images and save them in a list
# image_paths and the corresponding label in image_paths
image_paths = []
for training_name in training_names:
image_path = os.path.join(train_path training_name)
image_paths += [image_path]
# Create feature extraction and keypoint detector objects
fea_det = cv2.FeatureDetector_create(“SIFT“)
des_ext = cv2.DescriptorExtractor_create(“SIFT“)
# List where all the descriptors are stored
des_list = []
for i image_path in enumerate(image_paths):
im = cv2.imread(image_path)
print “Extract SIFT of %s image %d of %d images“ %(training_names[i] i len(image_paths))
kpts = fea_det.detect(im)
kpts des = des_ext.compute(im kpts)
# rootsift
#rs = RootSIFT()
#des = rs.compute(kpts des)
des_list.append((image_path des))
# Stack all the descriptors vertically in a numpy array
#downsampling = 1
#descriptors = des_list[0][1][::downsampling:]
#for image_path descriptor in des_list[1:]:
# descriptors = np.vstack((descriptors descriptor[::downsampling:]))
# Stack all the descriptors vertically in a numpy array
descriptors = des_list[0][1]
for image_path descriptor in des_list[1:]:
descriptors = np.vstack((descriptors descriptor))
# Perform k-means clustering
print “Start k-means: %d words %d key points“ %(numWords descriptors.shape[0])
voc variance = kmeans(descriptors numWords 1)
# Calculate the histogram of features
im_features = np.zeros((len(image_paths) numWords) “float32“)
for i in xrange(len(image_paths)):
words distance = vq(des_list[i][1]voc)
for w in words:
im_features[i][w] += 1
# Perform Tf-Idf vectorization
nbr_occurences = np.sum( (im_features > 0) * 1 axis = 0)
idf = np.array(np.log((1.0*len(image_paths)+1) / (1.0*nbr_occurences + 1)) ‘float32‘)
# Perform L2 normalization
im_features = im_features*idf
im_features = preprocessing.normalize(im_features norm=‘l2‘)
joblib.dump((im_features image_paths idf numWords voc) “bof.pkl“ compress=3)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 496225 2015-06-27 16:04 bow\bof.pkl
文件 2804 2015-07-21 10:55 bow\findFeatures.py
文件 707 2015-07-21 10:55 bow\rootsift.py
文件 2200 2015-07-21 10:56 bow\search.py
文件 13155 2015-07-21 10:53 bow\使用必读.docx
目录 0 2015-07-21 11:04 bow\dataset\train
目录 0 2015-07-21 10:33 bow\dataset
目录 0 2015-07-21 10:56 bow
----------- --------- ---------- ----- ----
515091 8
- 上一篇:realweibo.py
- 下一篇:区域生长 python版 dicom图像
相关资源
- python实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- SIFT源码python实现
- 《升级》扑克牌游戏——Python实现
- 蚁狮算法(Ant Lion AlgorithmPython实现和
- 编译原理词法分析器、语法分析器p
- 计算机语言学n-gram算法的python实现
- Python实现高斯投影正反算
- 用python实现sm2国密算法
- 混合地理加权回归python实现代码
- 步态识别python实现
- 《常用数据挖掘算法总结及Python实现
- 《机器学习》第2章中候选消除CANDID
- python实现用户画像
- 人工智能算法合集-python实现
- 许多点之间连线最短 python实现
- Fama三因子选股的python实现
- python实现EKF的CTRV模型
- 机器学习实战 Python实现
- 卷积神经网络的Python实现【试读】1
- 机器学习实战python实现
- GTK+、glade学习C、Python实现
- Python-即时通讯Python实现web版多人聊天
- 用Python实现一个软件自动升级系统
- python实现的卷积神经网络CNN无框架
- 字符型图片数字验证码识别完整过程
- 图像分割Grabcut算法-GUI程序-python实现
- python实现游戏外星人入侵
- 常用数据挖掘算法总结及Python实现 文
- 常用数据挖掘算法总结及Python实现(
评论
共有 条评论