使用 OpenCV 提取 HoG 特征 [英] Extracting HoG Features using OpenCV

查看:34
本文介绍了使用 OpenCV 提取 HoG 特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 OpenCV 的 HoG API 提取特征,但我似乎找不到允许我这样做的 API.

I am trying to extract features using OpenCV's HoG API, however I can't seem to find the API that allow me to do that.

我想做的是使用 HoG 从我的所有数据集(一组正负图像)中提取特征,然后训练我自己的 SVM.

What I am trying to do is to extract features using HoG from all my dataset (a set number of positive and negative images), then train my own SVM.

我在 OpenCV 下查看了 HoG.cpp,但没有帮助.所有代码都隐藏在复杂性和迎合不同硬件(例如英特尔的 IPP)的需要中

I peeked into HoG.cpp under OpenCV, and it didn't help. All the codes are buried within complexities and the need to cater for different hardwares (e.g. Intel's IPP)

我的问题是:

  1. 是否有任何来自 OpenCV 的 API 可用于提取所有这些特征/描述符以输入到 SVM 中?如果有我如何使用它来训练我自己的 SVM?
  2. 如果没有,是否有任何现有的库可以完成同样的事情?

到目前为止,我实际上正在将现有的库 (http://hogprocessing.altervista.org/) 从 Processing (Java) 移植到 C++,但它仍然非常慢,检测至少需要 16 秒左右

So far, I am actually porting an existing library (http://hogprocessing.altervista.org/) from Processing (Java) to C++, but it's still very slow, with detection taking around at least 16 seconds

有没有人成功提取HoG特征,你是怎么解决的?你有任何我可以使用的开源代码吗?

Has anyone else successfully to extract HoG features, how did you go around it ? And do you have any open source codes which I could use ?

提前致谢

推荐答案

在opencv中可以使用hog class如下

You can use hog class in opencv as follows

HOGDescriptor hog;
vector<float> ders;
vector<Point> locs;

此函数为您计算 hog 特征

This function computes the hog features for you

hog.compute(grayImg, ders, Size(32, 32), Size(0, 0), locs);

grayImg计算的HOG特征存储在ders向量中,形成一个矩阵,以后可以用于训练.

The HOG features computed for grayImg are stored in ders vector to make it into a matrix, which can be used later for training.

Mat Hogfeat(ders.size(), 1, CV_32FC1);

for(int i=0;i<ders.size();i++)
    Hogfeat.at<float>(i,0)=ders.at(i);

现在您的 HOG 特征存储在 Hogfeat 矩阵中.

Now your HOG features are stored in Hogfeat matrix.

你也可以通过object hog来设置窗口大小、单元格大小和块大小,如下:

You can also set the window size, cell size and block size by using object hog as follows:

hog.blockSize = 16;
hog.cellSize = 4;
hog.blockStride = 8;

// This is for comparing the HOG features of two images without using any SVM 
// (It is not an efficient way but useful when you want to compare only few or two images)
// Simple distance
// Consider you have two HOG feature vectors for two images Hogfeat1 and Hogfeat2 and those are same size.

double distance = 0;
for(int i = 0; i < Hogfeat.rows; i++)
    distance += abs(Hogfeat.at<float>(i, 0) - Hogfeat.at<float>(i, 0));

if (distance < Threshold)
    cout<<"Two images are of same class"<<endl;
else
    cout<<"Two images are of different class"<<endl;

希望有用:)

这篇关于使用 OpenCV 提取 HoG 特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆