我编写了一个函数,该函数基于Canny边缘检测器计算图像的Hu矩:
void HuMoments(Mat gray_image, QString j)
{
//image en gris et détecteur de Canny et contours et moments de Hu (forme)qui sont en pratique utilisés
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat detected_edges;
int lowThreshold=80;
//int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;
RNG rng(12345);
blur( gray_image, detected_edges, Size(3,3) );
// Canny detector
Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );
findContours( detected_edges, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
/// Draw contours
// Mat drawing = Mat::zeros( detected_edges.size(), CV_8UC3 );
// for( int i = 0; i< contours.size(); i++ )
// {
// Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
// drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
// }
vector<Moments> mu(contours.size());
for (int i = 0; i < contours.size(); i++)
{
mu[i] = moments(contours[i], false);
}
double hu[contours.size()][7];
for (int i = 0; i < contours.size(); i++)
{
// cout << "Contour: " << i << " Area: " << contourArea(contours[i]) << " Length: " << arcLength(contours[i], true) << "\n";
HuMoments(mu[i], hu[i]);
// for (int j = 0; j < 7; j++)
// {
// cout << "Contour: " << i << " Hu: " << j << " Result: " << hu[i][j] << "\n";
// }
// cout << "\n";
}
}
我想基于这些Hu Moments在图像数据库中进行检索。但是,我不知道如何比较Hu Moments,因为它们的大小为contours.size()
* 7,而且我猜contours.size()
可能在一个图像之间变化,是吗?
所以我想知道如何比较可能完全不同的图像的Hu Moments。
预先感谢