'''
@author: Zhu li
@method: SIFT -> k-means -> SVM
'''
import cv2
import numpy as np
TrainSetInfo = {
"car" : 40,
"city" : 20,
"dog" : 30,
"earth" : 15,
"fireworks" : 20,
"flowers" : 20,
"fruits" : 20,
"glass" : 20,
"gold" : 15,
"gun" : 20,
"plane" : 40,
"sky" : 30,
"worldcup" : 40
}
TestSetInfo = {
"car" : 119,
"city" : 59,
"dog" : 49,
"earth" : 24,
"fireworks" : 54,
"flowers" : 63,
"fruits" : 78,
"glass" : 52,
"gold" : 44,
"gun" : 44,
"plane" : 102,
"sky" : 78,
"worldcup" : 131
}
def calcSiftFeature(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT(200) # max number of SIFT points is 200
kp, des = sift.detectAndCompute(gray, None)
return des
def calcFeatVec(features, centers):
featVec = np.zeros((1, 50))
for i in range(0, features.shape[0]):
fi = features[i]
diffMat = np.tile(fi, (50, 1)) - centers
sqSum = (diffMat**2).sum(axis=1)
dist = sqSum**0.5
sortedIndices = dist.argsort()
idx = sortedIndices[0] # index of the nearest center
featVec[0][idx] += 1
return featVec
def initFeatureSet():
for name, count in TrainSetInfo.items():
dir = "TrainSet/" + name + "/"
featureSet = np.float32([]).reshape(0,128)
print "Extract features from training set" + name + "..."
for i in range(1, count + 1):
filename = dir + name + " (" + str(i) + ").jpg"
img = cv2.imread(filename)
des = calcSiftFeature(img)
featureSet = np.append(featureSet, des, axis=0)
featCnt = featureSet.shape[0]
print str(featCnt) + " features in " + str(count) + " images\n"
# save featureSet to file
filename = "Temp/features/" + name + ".npy"
np.save(filename, featureSet)
def learnVocabulary():
wordCnt = 50
for name, count in TrainSetInfo.items():
filename = "Temp/features/" + name + ".npy"
features = np.load(filename)
print "Learn vocabulary of " + name + "..."
# use k-means to cluster a bag of features
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 0.1)
flags = cv2.KMEANS_RANDOM_CENTERS
compactness, labels, centers = cv2.kmeans(features, wordCnt, criteria, 20, flags)
# save vocabulary(a tuple of (labels, centers)) to file
filename = "Temp/vocabulary/" + name + ".npy"
np.save(filename, (labels, centers))
print "Done\n"
def trainClassifier():
trainData = np.float32([]).reshape(0, 50)
response = np.float32([])
dictIdx = 0
for name, count in TrainSetInfo.items():
dir = "TestSet/" + name + "/"
labels, centers = np.load("Temp/vocabulary/" + name + ".npy")
print "Init training data of " + name + "..."
for i in range(1, count + 1):
filename = dir + name + " (" + str(i) + ").jpg"
img = cv2.imread(filename)
features = calcSiftFeature(img)
featVec = calcFeatVec(features, centers)
trainData = np.append(trainData, featVec, axis=0)
res = np.repeat(np.float32([dictIdx]), count)
response = np.append(response, res)
dictIdx += 1
print "Done\n"
print "Now train svm classifier..."
trainData = np.float32(trainData)
response = response.reshape(-1, 1)
svm = cv2.SVM()
svm.train_auto(trainData, response, None, None, None) # select best params
svm.save("svm.clf")
print "Done\n"
def classify():
svm = cv2.SVM()
svm.load("svm.clf")
total = 0; correct = 0; dictIdx = 0
for name, count in TestSetInfo.items():
crt = 0
dir = "TestSet/" + name + "/"
labels, centers = np.load("Temp/vocabulary/" + name + ".npy")
print "Classify on TestSet " + name + ":"
for i in range(1, count + 1):
filename = dir + name + " (" + str(i) + ").jpg"
img = cv2.imread(filename)
features = calcSiftFeature(img)
featVec = calcFeatVec(features, centers)
case = np.float32(featVec)
if (dictIdx == svm.predict(case)):
crt += 1
print "Accuracy: " + str(crt) + " / " + str(count) + "\n"
total += count
correct += crt
dictIdx += 1
print "Total accuracy: " + str(correct) + " / " + str(total)
if __name__ == "__main__":
initFeatureSet()
learnVocabulary()
trainClassifier()
classify()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
对图片的分类主要包含以下四个步骤:1.用尺度不变特征转换(SIFT)算法来提取训练集中图片的特征值。2.用K-means算法将这些特征值聚成n类。这n类中的每一类就相当于是图片的单词,所有的n个类别构成词汇表。3.对训练集中的图片构造词汇表,就是将图片中的特征值归到不同的类中,然后统计每一类的特征值的频率。4. 用支持向量机(SVM)训练一个多类分类器,将每张图片的词汇表作为特征向量。对于未知类别的图片,计算它的词汇表,使用训练的SVM分类器进行分类。
资源推荐
资源详情
资源评论
收起资源包目录
基于支持向量机的图片分类程序 (1255个子文件)
svm.clf 133KB
fireworks (9).jpg 41KB
flowers (28).jpg 39KB
fruits (45).jpg 39KB
fireworks (4).jpg 38KB
flowers (50).jpg 37KB
flowers (47).jpg 36KB
fireworks (16).jpg 35KB
flowers (16).jpg 35KB
fruits (51).jpg 35KB
fruits (52).jpg 34KB
flowers (60).jpg 34KB
flowers (8).jpg 34KB
glass (5).jpg 33KB
flowers (62).jpg 32KB
flowers (21).jpg 32KB
flowers (11).jpg 32KB
flowers (15).jpg 32KB
worldcup (86).jpg 32KB
fruits (50).jpg 32KB
flowers (33).jpg 31KB
fireworks (52).jpg 31KB
fireworks (29).jpg 31KB
fruits (5).jpg 31KB
flowers (38).jpg 31KB
flowers (3).jpg 30KB
flowers (26).jpg 30KB
flowers (46).jpg 30KB
fruits (13).jpg 30KB
flowers (2).jpg 30KB
flowers (13).jpg 29KB
fruits (27).jpg 29KB
flowers (63).jpg 29KB
flowers (17).jpg 29KB
flowers (44).jpg 29KB
flowers (37).jpg 29KB
flowers (10).jpg 29KB
fireworks (11).jpg 29KB
flowers (10).jpg 28KB
flowers (34).jpg 28KB
fireworks (27).jpg 28KB
flowers (31).jpg 28KB
fireworks (48).jpg 28KB
gold (2).jpg 28KB
fireworks (50).jpg 28KB
fruits (1).jpg 28KB
flowers (35).jpg 28KB
fireworks (15).jpg 28KB
fireworks (30).jpg 28KB
flowers (9).jpg 28KB
fruits (70).jpg 28KB
fireworks (10).jpg 27KB
flowers (7).jpg 27KB
fireworks (28).jpg 27KB
flowers (48).jpg 27KB
glass (40).jpg 27KB
fireworks (12).jpg 27KB
flowers (55).jpg 27KB
fruits (44).jpg 27KB
flowers (42).jpg 27KB
fireworks (18).jpg 27KB
flowers (41).jpg 27KB
gold (3).jpg 27KB
flowers (12).jpg 27KB
glass (43).jpg 27KB
flowers (6).jpg 27KB
flowers (14).jpg 27KB
worldcup (1).jpg 27KB
glass (16).jpg 27KB
flowers (58).jpg 27KB
flowers (59).jpg 26KB
gold (6).jpg 26KB
flowers (49).jpg 26KB
worldcup (102).jpg 26KB
flowers (5).jpg 26KB
fireworks (6).jpg 26KB
car (111).jpg 26KB
worldcup (97).jpg 26KB
flowers (39).jpg 26KB
flowers (11).jpg 26KB
car (9).jpg 26KB
worldcup (15).jpg 26KB
fireworks (25).jpg 26KB
fruits (64).jpg 26KB
city (1).jpg 26KB
flowers (61).jpg 26KB
fruits (11).jpg 25KB
worldcup (17).jpg 25KB
car (47).jpg 25KB
flowers (9).jpg 25KB
fireworks (4).jpg 25KB
fireworks (17).jpg 25KB
worldcup (27).jpg 25KB
worldcup (55).jpg 25KB
fruits (63).jpg 25KB
flowers (4).jpg 25KB
gold (24).jpg 25KB
worldcup (93).jpg 25KB
gold (1).jpg 25KB
worldcup (124).jpg 25KB
共 1255 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
资源评论
- Jupiter7Martin2018-11-06打不开 说损坏了
wanshibugong19900205
- 粉丝: 1
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功