您可以在 opencv 中使用 hog 类,如下所示
HOGDescriptor hog;
vector<float> ders;
vector<Point> locs;
此函数为您计算 hog 特征
hog.compute(grayImg, ders, Size(32, 32), Size(0, 0), locs);
计算出来的 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);
现在您的 HOG 特征存储在 Hogfeat 矩阵中。
您还可以使用 object 设置窗口大小、单元格大小和块大小,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;
希望它有用:)