且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 OpenCV 提取 HoG 特征

更新时间:2023-02-27 11:18:31

在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;

希望有用:)