简体   繁体   English

使用OpenCV提取HoG功能

[英]Extracting HoG Features using OpenCV

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. 我正在尝试使用OpenCV的HoG API提取功能,但我似乎无法找到允许我这样做的API。

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. 我想要做的是从我的所有数据集(一组正面和负面图像)中使用HoG提取特征,然后训练我自己的SVM。

I peeked into HoG.cpp under OpenCV, and it didn't help. 我在OpenCV下偷看了HoG.cpp,但没有用。 All the codes are buried within complexities and the need to cater for different hardwares (eg Intel's IPP) 所有代码都埋没在复杂性中,需要满足不同的硬件需求(例如英特尔的IPP)

My question is: 我的问题是:

  1. Is there any API from OpenCV that I can use to extract all those features / descriptors to be fed into a SVM ? 我是否可以使用OpenCV中的任何API来提取所有要提供给SVM的功能/描述符? If there's how can I use it to train my own SVM ? 如果我可以用它来训练我自己的SVM?
  2. If there isn't, are there any existing libraries out there, which could accomplish the same thing ? 如果没有,是否有任何现有的库,可以完成同样的事情?

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 到目前为止,我实际上是将一个现有的库(http://hogprocessing.altervista.org/)从Processing(Java)移植到C ++,但它仍然非常慢,检测时间至少为16秒

Has anyone else successfully to extract HoG features, how did you go around it ? 有没有其他人成功提取HoG功能,你是如何解决它的? And do you have any open source codes which I could use ? 你有任何我可以使用的开源代码吗?

Thanks in advance 提前致谢

You can use hog class in opencv as follows 您可以在opencv中使用hog类,如下所示

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

This function computes the hog features for you 此功能为您计算生猪功能

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

The HOG features computed for grayImg are stored in ders vector to make it into a matrix, which can be used later for training. 计算的HOG特征grayImg存储在ders矢量,从而使之成为一个矩阵,其可以在以后用于训练中使用。

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

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

Now your HOG features are stored in Hogfeat matrix. 现在你的HOG功能存储在Hogfeat矩阵中。

You can also set the window size, cell size and block size by using object hog as follows: 您还可以使用对象hog设置窗口大小,单元格大小和块大小,如下所示:

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;

Hope it is useful :) 希望它有用:)

I also wrote the program of 2 hog feature comparing with the help of the above article. 在上述文章的帮助下,我还编写了2猪的功能程序。 And I apply this method to check ROI region changing or not. 我应用此方法来检查ROI区域是否发生变化。 Please refer to the page here. 请参阅此处的页面。 source code and simple introduction 源代码和简单介绍

Here is GPU version as well. 这也是GPU版本。

cv::Mat temp;
gpu::GpuMat gpu_img, descriptors;

cv::gpu::HOGDescriptor gpu_hog(win_size, Size(16, 16), Size(8, 8), Size(8, 8), 9,
                               cv::gpu::HOGDescriptor::DEFAULT_WIN_SIGMA, 0.2, gamma_corr,
                               cv::gpu::HOGDescriptor::DEFAULT_NLEVELS);
gpu_img.upload(img);
gpu_hog.getDescriptors(gpu_img, win_stride, descriptors, cv::gpu::HOGDescriptor::DESCR_FORMAT_ROW_BY_ROW);
            descriptors.download(temp);

OpenCV 3 provides some changes to the way GPU algorithms (ie CUDA) can be used by the user, see the Transition Guide - CUDA . OpenCV 3对用户可以使用GPU算法(即CUDA)的方式进行了一些更改,请参阅转换指南 - CUDA

To update the answer from user3398689 to OpenCV 3, here is a snipped code: 要将user3398689的答案更新为OpenCV 3,这是一个剪切代码:

#include <opencv2/core/cuda.hpp>
#include <opencv2/cudaimgproc.hpp>

[...]

/* Suppose you load an image in a cv::Mat variable called 'src' */

int img_width  = 320;
int img_height = 240;
int block_size = 16;
int bin_number = 9;

cv::Ptr<cv::cuda::HOG> cuda_hog = cuda::HOG::create(Size(img_width, img_height),
                                                    Size(block_size, block_size),
                                                    Size(block_size/2, block_size/2),
                                                    Size(block_size/2, block_size/2),
                                                    bin_number);

/* The following commands are optional: default values applies */
cuda_hog->setDescriptorFormat(cuda::HOG::DESCR_FORMAT_COL_BY_COL);
cuda_hog->setGammaCorrection(true);
cuda_hog->setWinStride(Size(img_width_, img_height_));

cv::cuda::GpuMat image;
cv::cuda::GpuMat descriptor;

image.upload(src);

/* May not apply to you */
/* CUDA HOG works with intensity (1 channel) or BGRA (4 channels) images */
/* The next function call convert a standard BGR image to BGRA using the GPU */
cv::cuda::GpuMat image_alpha;
cuda::cvtColor(image, image_alpha, COLOR_BGR2BGRA, 4);

cuda_hog->compute(image_alpha, descriptor);

cv::Mat dst;
image_alpha.download(dst);

You can then use the descriptors in 'dst' variable as you prefer like, eg, as suggested by G453. 然后,您可以根据需要使用'dst'变量中的描述符,例如,如G453所建议的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM