使用OpenCV提取HoG特征



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

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

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

我的问题是:

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

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

其他人成功提取HoG特征了吗?你是怎么做到的?你有我可以使用的开源代码吗?

提前感谢

您可以在opencv中使用hog类,如下所示

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

此函数为您计算猪的特征

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

grayImg计算的HOG特征存储在ders向量中,使其成为矩阵,稍后可用于训练。

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

现在,您的HOG特征存储在Hogfat矩阵中。

您还可以使用对象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;

希望它有用:)

在上面文章的帮助下,我还编写了2 hog特性的程序。并且我将这种方法应用于检查ROI区域是否改变。请参阅此处的页面。源代码和简单介绍

这也是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对用户使用GPU算法(即CUDA)的方式进行了一些更改,请参阅过渡指南-CUDA。

要将答案从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);

然后,您可以随心所欲地使用"dst"变量中的描述符,例如G453所建议的。

最新更新