C++两个图像对比,并输出轮廓

#include <opencv2/opencv.hpp>
#include <string>
#include <iostream>



// 计算图像的灰度均值
void cal_mean_stddev(std::string path) {
	cv::Mat src = cv::imread(path);
	cv::Mat gray, mat_mean, mat_stddev;
	cvtColor(src, gray, cv::COLOR_RGB2GRAY); // 转换为灰度图
	meanStdDev(gray, mat_mean, mat_stddev);
	double m, s;
	m = mat_mean.at<double>(0, 0);
	s = mat_stddev.at<double>(0, 0);
	std::cout << path << "的灰度均值是:" << m << std::endl;
	std::cout << path << "的标准差是:" << s << std::endl;
}

//显示位深的辅助函数
std::string Type2String(int type)
{
	std::string strType;
	uchar depth = type & CV_MAT_DEPTH_MASK;
	uchar chans = 1 + (type >> CV_CN_SHIFT);
	switch (depth)
	{
	case CV_8U:
		strType = "CV_8U"; break;
	case CV_8S:
		strType = "CV_8S"; break;
	case CV_16U:
		strType = "CV_16U"; break;
	case CV_16S:
		strType =
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
下面是一个示例代码,用于匹配并截取两个不规则图像轮廓。这里使用了 OpenCV 库。 ```c++ #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main(int argc, char** argv) { // 读取原始图像 Mat src1 = imread("image1.png"); Mat src2 = imread("image2.png"); // 转换为灰度图像 Mat gray1, gray2; cvtColor(src1, gray1, COLOR_BGR2GRAY); cvtColor(src2, gray2, COLOR_BGR2GRAY); // 二值化处理 Mat bin1, bin2; threshold(gray1, bin1, 128, 255, THRESH_BINARY); threshold(gray2, bin2, 128, 255, THRESH_BINARY); // 查找轮廓 vector<vector<Point>> contours1, contours2; vector<Vec4i> hierarchy1, hierarchy2; findContours(bin1, contours1, hierarchy1, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); findContours(bin2, contours2, hierarchy2, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); // 匹配轮廓 Mat match_result; drawContours(match_result, contours1, -1, Scalar(255, 0, 0), 1); drawContours(match_result, contours2, -1, Scalar(0, 255, 0), 1); for (int i = 0; i < contours1.size(); i++) { for (int j = 0; j < contours2.size(); j++) { double match = matchShapes(contours1[i], contours2[j], CONTOURS_MATCH_I2, 0); if (match < 0.1) { // 截取匹配的轮廓 Rect rect = boundingRect(contours2[j]); Mat roi = src2(rect); // 在原始图像上绘制截取结果 drawContours(src1, contours1, i, Scalar(0, 0, 255), 2); rectangle(src1, rect, Scalar(0, 255, 0), 2); // 显示结果 imshow("Match result", match_result); imshow("Result", src1); waitKey(); return 0; } } } cout << "No match found!" << endl; return -1; } ``` 在这段代码中,我们首先读取了两个原始图像,并将它们转换为灰度图像进行后续处理。然后,我们使用 `findContours` 函数查找图像中的轮廓,并使用 `drawContours` 函数绘制轮廓。接下来,我们使用 `matchShapes` 函数对两个图像中的轮廓进行匹配,如果匹配度小于阈值,则截取匹配的轮廓并在原始图像上绘制结果。最后,我们使用 `imshow` 函数将结果显示出来。 需要注意的是,这里的匹配方式是使用 `matchShapes` 函数对轮廓进行匹配,如果需要更精确的匹配结果,可以考虑使用其他匹配算法,如基于特征点的匹配算法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI炮灰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值