opencv学习日记——基于Hu矩的轮廓匹配

opencv学习日记——基于Hu矩的轮廓匹配

图像结果(达不到预期)

在这里插入图片描述

代码部分

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

using namespace std;
using namespace cv;

void findcontours(Mat &img, vector<vector<Point>> &contours)
{
    Mat gray, binary;
    vector<Vec4i> hierachy;
    //变灰度图
    cvtColor(img, gray, COLOR_BGR2GRAY);
    //二值化
    threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
    //发现轮廓
    findContours(binary, contours, hierachy, 0, 2);
}
int main()
{
    Mat img1 = imread("自定义.png");
    Mat img2 = imread("1.png");

    if (img1.empty() || img2.empty())
    {
        cout << "输入为空";
        return -1;
    }

    // resize(img2, img2, Size(), 0.5, 0.5);
    // imwrite("正方形.png", img2);
    imshow("正方形", img2);
    vector<vector<Point>> contours1;
    vector<vector<Point>> contours2;
    findcontours(img1, contours1);
    findcontours(img2, contours2);
    //计算几何矩,中心矩,
    Moments mm2 = moments(contours2[0]);
    //计算hu矩(旋转不变性)
    Mat hu2;
    HuMoments(mm2, hu2);
    //
    for (int z = 0; z < contours1.size(); z++ )
    {
        Moments mm1 = moments(contours1[z]);
        Mat hu1;
        HuMoments(mm1, hu1);

        double dist;
        //计算匹配
        //matchShapes(hu矩, hu矩, 匹配方法)
        dist = matchShapes(hu1, hu2, 1, 0);
        if (dist < 1)
        {
            cout << "yes";
            drawContours(img1, contours1, z, Scalar(0, 150, 255), 3, 8);
        }
        imshow ("matchimg", img1);
        waitKey(0);
        return 0;
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hu是一种对图像进行形状匹配的方法,它是一组可旋转、平移和缩放不变的图像特征。在Python中,我们可以使用OpenCV库来计算Hu,以下是Hu轮廓匹配的基本步骤: 1. 读取待匹配图像和模板图像,并进行灰度化。 2. 对待匹配图像和模板图像进行二值化处理。 3. 使用findContours()函数找到待匹配图像和模板图像的轮廓。 4. 计算待匹配图像和模板图像的Hu。 5. 使用matchShapes()函数计算待匹配图像和模板图像的形状相似度。 下面是一个示例代码,演示了如何使用OpenCV计算Hu并进行轮廓匹配: ```python import cv2 # 读取待匹配图像和模板图像 img1 = cv2.imread('image1.jpg') img2 = cv2.imread('image2.jpg') # 灰度化 gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # 二值化 ret1, thresh1 = cv2.threshold(gray1, 127, 255, cv2.THRESH_BINARY) ret2, thresh2 = cv2.threshold(gray2, 127, 255, cv2.THRESH_BINARY) # 轮廓提取 contours1, hierarchy1 = cv2.findContours(thresh1, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) contours2, hierarchy2 = cv2.findContours(thresh2, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # 计算Hu moments1 = cv2.moments(contours1[0]) hu1 = cv2.HuMoments(moments1).flatten() moments2 = cv2.moments(contours2[0]) hu2 = cv2.HuMoments(moments2).flatten() # 计算形状相似度 match_value = cv2.matchShapes(contours1[0], contours2[0], cv2.CONTOURS_MATCH_I1, 0) # 输出结果 print('Hu moments of image1:', hu1) print('Hu moments of image2:', hu2) print('Match value:', match_value) ``` 在上面的示例代码中,我们首先读取了两张图像并进行了灰度化和二值化处理,然后使用findContours()函数找到了两张图像的轮廓。接着,我们分别计算了两张图像的Hu,并使用matchShapes()函数计算了它们的形状相似度。最后,我们输出了Hu和形状相似度的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值