写个图像形状匹配算法代码 用c++
时间: 2023-03-12 22:02:59 浏览: 77
答:以下是一个简单的图像形状匹配算法的C语言实现:#include <stdio.h>int main(void)
{
// 声明图像形状变量
int shape1, shape2; // 读取图像形状
scanf("%d %d", &shape1, &shape2); // 判断形状是否匹配
if (shape1 == shape2)
printf("形状匹配!");
else
printf("形状不匹配!"); return 0;
}
相关问题
opencv的形状匹配及算法C++
OpenCV中的形状匹配算法可以通过计算轮廓的Hu矩来实现,具体步骤如下:
1. 读取并二值化图像,提取目标轮廓:
```c++
Mat src = imread("image.jpg");
Mat gray, binary;
cvtColor(src, gray, COLOR_BGR2GRAY);
threshold(gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(binary, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);
```
2. 计算目标轮廓的Hu矩:
```c++
Moments mu = moments(contours[0], false);
double hu[7];
HuMoments(mu, hu);
```
3. 读取并二值化模板图像,提取模板轮廓:
```c++
Mat tpl = imread("template.jpg");
Mat gray_tpl, binary_tpl;
cvtColor(tpl, gray_tpl, COLOR_BGR2GRAY);
threshold(gray_tpl, binary_tpl, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
vector<vector<Point>> contours_tpl;
vector<Vec4i> hierarchy_tpl;
findContours(binary_tpl, contours_tpl, hierarchy_tpl, RETR_LIST, CHAIN_APPROX_SIMPLE);
```
4. 计算模板轮廓的Hu矩:
```c++
Moments mu_tpl = moments(contours_tpl[0], false);
double hu_tpl[7];
HuMoments(mu_tpl, hu_tpl);
```
5. 计算目标轮廓和模板轮廓的Hu矩的差异(即Hu矩距离):
```c++
double dist = matchShapes(contours[0], contours_tpl[0], CONTOURS_MATCH_I1, 0);
```
6. 根据Hu矩距离判断目标轮廓和模板轮廓的相似度:
```c++
if (dist < 0.1) {
cout << "匹配成功!" << endl;
} else {
cout << "匹配失败!" << endl;
}
```
完整的代码如下:
```c++
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main() {
Mat src = imread("image.jpg");
Mat gray, binary;
cvtColor(src, gray, COLOR_BGR2GRAY);
threshold(gray, binary, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(binary, contours, hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);
Moments mu = moments(contours[0], false);
double hu[7];
HuMoments(mu, hu);
Mat tpl = imread("template.jpg");
Mat gray_tpl, binary_tpl;
cvtColor(tpl, gray_tpl, COLOR_BGR2GRAY);
threshold(gray_tpl, binary_tpl, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
vector<vector<Point>> contours_tpl;
vector<Vec4i> hierarchy_tpl;
findContours(binary_tpl, contours_tpl, hierarchy_tpl, RETR_LIST, CHAIN_APPROX_SIMPLE);
Moments mu_tpl = moments(contours_tpl[0], false);
double hu_tpl[7];
HuMoments(mu_tpl, hu_tpl);
double dist = matchShapes(contours[0], contours_tpl[0], CONTOURS_MATCH_I1, 0);
if (dist < 0.1) {
cout << "匹配成功!" << endl;
} else {
cout << "匹配失败!" << endl;
}
return 0;
}
```
基于opencv、c++,请写出一份基于轮廓形状的视频图片模板匹配代码。依照模板图片物品的形状,来匹配视频中可能出现的对应物品。
当然,下面是一份基于轮廓形状的视频图片模板匹配代码,使用OpenCV和C++编写:
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
// 读取模板图片
Mat templateImage = imread("template.png", IMREAD_GRAYSCALE);
if (templateImage.empty())
{
cout << "无法读取模板图片!" << endl;
return -1;
}
// 创建窗口
namedWindow("模板匹配", WINDOW_NORMAL);
// 打开视频文件
VideoCapture cap("video.mp4");
if (!cap.isOpened())
{
cout << "无法打开视频文件!" << endl;
return -1;
}
Mat frame, grayFrame;
while (cap.read(frame))
{
// 将当前帧转换为灰度图像
cvtColor(frame, grayFrame, COLOR_BGR2GRAY);
// 进行模板匹配
Mat result;
matchTemplate(grayFrame, templateImage, result, TM_CCOEFF_NORMED);
// 设置匹配阈值
double threshold = 0.8;
threshold = threshold * 255;
// 寻找匹配位置
vector<Point> locations;
for (int y = 0; y < result.rows; y++)
{
for (int x = 0; x < result.cols; x++)
{
if (result.at<float>(y, x) >= threshold)
{
locations.push_back(Point(x, y));
}
}
}
// 绘制矩形框标记匹配位置
for (const Point& location : locations)
{
rectangle(frame, location, Point(location.x + templateImage.cols, location.y + templateImage.rows), Scalar(0, 255, 0), 2);
}
// 显示结果
imshow("模板匹配", frame);
// 按下ESC键退出
if (waitKey(1) == 27)
{
break;
}
}
// 释放资源
cap.release();
destroyAllWindows();
return 0;
}
```
请将模板图片保存为`template.png`,视频文件保存为`video.mp4`,然后使用C++编译器编译运行此代码。代码中的模板匹配方法使用了`TM_CCOEFF_NORMED`算法,你可以根据实际需求选择其他算法。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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_lunwen.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)