关于opencv里使用svm的问题

南星醉月 2017-12-06 11:52:19
使用《OpenCV3计算机视觉 python语言实现(原书第二版)》的时候遇到的问题 使用bow 用opencv里的cv2.BOWKMeansTrainer构建了一个对象之后,bow_kmeans_trainer.add(extract_sift(path(pos,i), extract, detect))使用添加函数报错

#extract_bow = get_bow_extractor(extract, matcher)
print ("building BOWKMeansTrainer...")
bow_kmeans_trainer = cv2.BOWKMeansTrainer(12)
extract_bow = cv2.BOWImgDescriptorExtractor(extract, matcher)

print ("adding features to trainer")
for i in range(SAMPLES):
print (i)
bow_kmeans_trainer.add(extract_sift(path(pos,i), extract, detect))
#bow_kmeans_trainer.add(extract_sift(path(neg,i), extract, detect))



小白,第一次使用opencv 忘指教
...全文
252 3 点赞 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
南星醉月 2017-12-12
问题已经解决 移到博客查看或者http://www.cnblogs.com/xuanyuyt/p/6399327.html这位博主的文章
  • 打赏
  • 举报
回复
南星醉月 2017-12-07
# -*- coding: utf-8 -*-
"""
Created on Mon Dec  4 10:57:20 2017

@author: Ramos
"""

import cv2
import numpy as np

datapath = "../detectionData/TrainImages/"
SAMPLES = 75

def path(cls,i):
    return "%s/%s%d.pgm"  % (datapath,cls,i+1)

def get_flann_matcher():
  flann_params = dict(algorithm = 1, trees = 5)
  return cv2.FlannBasedMatcher(flann_params, {})

def get_bow_extractor(extract, match):
  return cv2.BOWImgDescriptorExtractor(extract, match)

def get_extract_detect():
  return cv2.xfeatures2d.SIFT_create(), cv2.xfeatures2d.SIFT_create()

def extract_sift(fn, extractor, detector):
  im = cv2.imread(fn,0)
  return extractor.compute(im, detector.detect(im))[1]
    
def bow_features(img, extractor_bow, detector):
  return extractor_bow.compute(img, detector.detect(img))

def car_detector():
  pos, neg = "pos-", "neg-"
  detect, extract = get_extract_detect()
  matcher = get_flann_matcher()
  #extract_bow = get_bow_extractor(extract, matcher)
  print ("building BOWKMeansTrainer...")
  bow_kmeans_trainer = cv2.BOWKMeansTrainer(12)
  extract_bow = cv2.BOWImgDescriptorExtractor(extract, matcher)

  print ("adding features to trainer")
  for i in range(SAMPLES):
    print (i)
    bow_kmeans_trainer.add(extract_sift(path(pos,i), extract, detect))
    #bow_kmeans_trainer.add(extract_sift(path(neg,i), extract, detect))
    
  vocabulary = bow_kmeans_trainer.cluster()
  extract_bow.setVocabulary(vocabulary)

  traindata, trainlabels = [],[]
  print ("adding to train data")
  for i in range(SAMPLES):
    print (i)
    traindata.extend(bow_features(cv2.imread(path(pos, i), 0), extract_bow, detect))
    trainlabels.append(1)
    traindata.extend(bow_features(cv2.imread(path(neg, i), 0), extract_bow, detect))
    trainlabels.append(-1)

  svm = cv2.ml.SVM_create()
  svm.setType(cv2.ml.SVM_C_SVC)
  svm.setGamma(1)
  svm.setC(35)
  svm.setKernel(cv2.ml.SVM_RBF)

  svm.train(np.array(traindata), cv2.ml.ROW_SAMPLE, np.array(trainlabels))
  return svm, extract_bow
全部代码
  • 打赏
  • 举报
回复
相关推荐
发帖
脚本语言
加入

3.7w+

社区成员

JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
申请成为版主
帖子事件
创建了帖子
2017-12-06 11:52
社区公告

CSDN 脚本语言社区接受专栏投稿(专栏会在顶部创建专属你的栏目),投稿需满足以下要求:

  • 脚本语言技术相关;
  • 文章持续更新,保持活跃;
  • 内容清晰明了,干货为主;
  • 文章排版有序,有条有理。

本社区开通招聘专栏,发布招聘信息请联系版主,发布者需要保证招聘信息真实有效,CSDN 平台和版主不对招聘内容负责!

联系方式:私聊版主、发送邮件、QQ联系等均可: