用c++语言完成基于Hu矩的轮廓匹配
时间: 2023-11-22 16:51:47 浏览: 42
轮廓匹配是图像处理中的一项重要任务,它可以用于物体识别、目标跟踪等应用。基于Hu矩的轮廓匹配是其中一种方法,下面介绍如何用C++语言实现。
1. 首先,我们需要计算轮廓的Hu矩。Hu矩是一种旋转不变的矩形描述符,可以用来描述轮廓的形状特征。定义如下:
```c++
double huMoment(int p, int q, cv::Mat& image)
{
double moment = 0;
for (int i = 0; i < image.rows; i++)
{
for (int j = 0; j < image.cols; j++)
{
moment += pow(i, p) * pow(j, q) * image.at<uchar>(i, j);
}
}
return moment;
}
double huInvariantMoment(int p, int q, cv::Mat& image)
{
double moment = huMoment(p, q, image);
double m00 = huMoment(0, 0, image);
double eta = moment / pow(m00, ((p + q) / 2.0 + 1));
return eta;
}
```
2. 接着,我们需要读入轮廓模板和待匹配图像,并提取它们的轮廓。
```c++
cv::Mat templateImage = cv::imread("template.jpg", cv::IMREAD_GRAYSCALE);
cv::Mat matchImage = cv::imread("match.jpg", cv::IMREAD_GRAYSCALE);
std::vector<std::vector<cv::Point>> templateContours;
std::vector<std::vector<cv::Point>> matchContours;
cv::findContours(templateImage, templateContours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
cv::findContours(matchImage, matchContours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
```
3. 对于每个轮廓,计算其7个Hu矩,并用它们来表示轮廓的形状特征。
```c++
std::vector<double> templateMoment(7);
std::vector<double> matchMoment(7);
for (int i = 0; i < 7; i++)
{
templateMoment[i] = huInvariantMoment(i, 0, templateContours[0]);
matchMoment[i] = huInvariantMoment(i, 0, matchContours[0]);
}
```
4. 计算轮廓之间的匹配分数。我们可以使用欧氏距离或相关系数来计算两个矩向量之间的相似度。
```c++
double score = 0;
for (int i = 0; i < 7; i++)
{
score += pow(matchMoment[i] - templateMoment[i], 2);
}
score = sqrt(score);
// or
double meanTemplateMoment = std::accumulate(templateMoment.begin(), templateMoment.end(), 0.0) / templateMoment.size();
double meanMatchMoment = std::accumulate(matchMoment.begin(), matchMoment.end(), 0.0) / matchMoment.size();
double cov = 0;
double varTemplate = 0;
double varMatch = 0;
for (int i = 0; i < 7; i++)
{
cov += (templateMoment[i] - meanTemplateMoment) * (matchMoment[i] - meanMatchMoment);
varTemplate += pow(templateMoment[i] - meanTemplateMoment, 2);
varMatch += pow(matchMoment[i] - meanMatchMoment, 2);
}
double corr = cov / sqrt(varTemplate * varMatch);
```
至此,基于Hu矩的轮廓匹配就完成了。可以根据实际需求选择不同的相似度度量来计算轮廓之间的匹配分数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)