sift+svm图像分类
2018-04-11 15:26
393 查看
注意:opencv版本为2.4.11,代码也没咋么修改,直接可以用
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 14 23:06:36 2015
@author: Brian
https://github.com/briansrls/SIFTBOW/blob/master/SIFTBOW.py
"""
import cv2
import numpy as np
import os
image_paths = []
path = "E:\\huang\\My_project\\data"
#list of our class names
training_names = os.listdir(path)
training_paths = []
names_path = []
#get full list of all training images
for p in training_names:
training_paths1 = os.listdir("E:\\huang\\My_project\\data\\"+p)
for j in training_paths1:
training_paths.append("E:\\huang\\My_project\\data\\"+p+"\\"+j)
names_path.append(p)
sift = cv2.SIFT()
print names_path
descriptors_unclustered = []
dictionarySize = 60
BOW = cv2.BOWKMeansTrainer(dictionarySize)
for p in training_paths:
image = cv2.imread(p)
gray = cv2.cvtColor(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
kp, dsc= sift.detectAndCompute(gray, None)
BOW.add(dsc)
#dictionary created
dictionary = BOW.cluster()
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50) # or pass empty dictionary
flann = cv2.FlannBasedMatcher(index_params,search_params)
sift2 = cv2.DescriptorExtractor_create("SIFT")
bowDiction = cv2.BOWImgDescriptorExtractor(sift2, cv2.BFMatcher(cv2.NORM_L2))
bowDiction.setVocabulary(dictionary)
print "bow dictionary", np.shape(dictionary)
#returns descriptor of image at pth
def feature_extract(pth):
im = cv2.imread(pth, 1)
gray = cv2.cvtColor(im, cv2.CV_LOAD_IMAGE_GRAYSCALE)
return bowDiction.compute(gray, sift.detect(gray))
train_desc = []
train_labels = []
i = 0
for p in training_paths:
train_desc.extend(feature_extract(p))
if names_path[i]=='gou':
train_labels.append(1)
if names_path[i]=='zhu':
train_labels.append(2)
i = i+1
print(train_desc)
print "svm items", len(train_desc), len(train_desc[0])
count=0
svm = cv2.SVM()
svm.train(np.array(train_desc), np.array(train_labels))
i=0
j=0
confusion = np.zeros((2,2))
def classify(pth):
feature = feature_extract(pth)
p = svm.predict(feature)
w = int(p)
confusion[train_labels[count]-1, w-1] = confusion[train_labels[count]-1, w-1] + 1
for p in training_paths:
#print(p)
classify(p)
count += 1
print(confusion)
def normalizeRows(M):
row_sums = M.sum(axis=1)
return M / row_sums
confusion = normalizeRows(confusion)
confusion = confusion.transpose()
print confusion
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 14 23:06:36 2015
@author: Brian
https://github.com/briansrls/SIFTBOW/blob/master/SIFTBOW.py
"""
import cv2
import numpy as np
import os
image_paths = []
path = "E:\\huang\\My_project\\data"
#list of our class names
training_names = os.listdir(path)
training_paths = []
names_path = []
#get full list of all training images
for p in training_names:
training_paths1 = os.listdir("E:\\huang\\My_project\\data\\"+p)
for j in training_paths1:
training_paths.append("E:\\huang\\My_project\\data\\"+p+"\\"+j)
names_path.append(p)
sift = cv2.SIFT()
print names_path
descriptors_unclustered = []
dictionarySize = 60
BOW = cv2.BOWKMeansTrainer(dictionarySize)
for p in training_paths:
image = cv2.imread(p)
gray = cv2.cvtColor(image, cv2.CV_LOAD_IMAGE_GRAYSCALE)
kp, dsc= sift.detectAndCompute(gray, None)
BOW.add(dsc)
#dictionary created
dictionary = BOW.cluster()
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50) # or pass empty dictionary
flann = cv2.FlannBasedMatcher(index_params,search_params)
sift2 = cv2.DescriptorExtractor_create("SIFT")
bowDiction = cv2.BOWImgDescriptorExtractor(sift2, cv2.BFMatcher(cv2.NORM_L2))
bowDiction.setVocabulary(dictionary)
print "bow dictionary", np.shape(dictionary)
#returns descriptor of image at pth
def feature_extract(pth):
im = cv2.imread(pth, 1)
gray = cv2.cvtColor(im, cv2.CV_LOAD_IMAGE_GRAYSCALE)
return bowDiction.compute(gray, sift.detect(gray))
train_desc = []
train_labels = []
i = 0
for p in training_paths:
train_desc.extend(feature_extract(p))
if names_path[i]=='gou':
train_labels.append(1)
if names_path[i]=='zhu':
train_labels.append(2)
i = i+1
print(train_desc)
print "svm items", len(train_desc), len(train_desc[0])
count=0
svm = cv2.SVM()
svm.train(np.array(train_desc), np.array(train_labels))
i=0
j=0
confusion = np.zeros((2,2))
def classify(pth):
feature = feature_extract(pth)
p = svm.predict(feature)
w = int(p)
confusion[train_labels[count]-1, w-1] = confusion[train_labels[count]-1, w-1] + 1
for p in training_paths:
#print(p)
classify(p)
count += 1
print(confusion)
def normalizeRows(M):
row_sums = M.sum(axis=1)
return M / row_sums
confusion = normalizeRows(confusion)
confusion = confusion.transpose()
print confusion
相关文章推荐
- 基于SIFT特征和SVM的图像分类
- 在opencv3中利用SVM进行图像目标检测和分类
- SVM+HOG对图像进行多分类(OpenCV实现)
- 基于SVM的图像分类
- 基于SVM的极化 SAR 图像分类算法思路
- python实现用SIFT+K-MEANS+SVM图片分类
- matlab版hog+svm图像二分类
- hog matlab代码1——实现hog+svm图像二分类
- opencv中的SVM图像分类(二)
- SVM手写数字图像分类
- 一个用BoW|Pyramid BoW+SVM进行图像分类的Matlab Demo
- 【opencv、机器学习】opencv中的SVM图像分类(二)
- 图像分类中的SVM多核学习
- opencv中的SVM图像分类(一)
- matlab实现hog+svm图像二分类
- opencv中的SVM图像分类(二)
- 利用SVM进行图像目标检测和分类
- 在opencv3中利用SVM进行图像目标检测和分类
- HOG+SVM进行图像分类
- SVM+HOG对图像进行分类(MATLAB实现)