opencv(36) 图像轮廓之五:轮廓匹配

1 cv2.pointPolygonTest() 查找图像中的点与轮廓线之间的最短距离

此函数查找图像中点与轮廓线之间的最短距离。当点在轮廓线外时,返回的距离为负,点在轮廓线内时返回的距离为正,点在轮廓线上返回的距离为零。

函数原型: retval = cv2.pointPolygonTest(contour,point,measureDist)

参数:

  1. contoure1:图像中的轮廓
  2. point: 图像中的点
  3. measureDist如果为True,函数估计从该点到最近轮廓边的带符号距离。否则,函数只检查点是否在轮廓线内。

返回值:

  1. retval:该函数确定点是在轮廓线内、外还是在边缘上(或与顶点重合)。相应地,它返回正(内部)、负(外部)或零(沿边)值。

measureDist为True时,返回值为该点与最近的轮廓边之间的有符号距离;

measureDist为False时,返回值分别为+1、-1和0

2 cv2.matchShape() 匹配(比较)两个轮廓

OpenCV附带了一个函数cv.matchShapes(),它使我们能够比较两个形状或两个轮廓,并返回一个显示相似性的度量。结果越低,匹配越好。它是基于hu矩值计算的。

函数原型: retval = cv2.matchShape(cnt1, cnt2,method,param)

参数:

  1. cnt1、cnt2:两个比较的的轮廓,数组
  2. method: 比较参数1和2相似度的方法,int 。opencv提供了三种如下:CV_CONTOURS_MATCH_I1  = 1;CV_CONTOURS_MATCH_I2  = 2;CV_CONTOURS_MATCH_I3  = 3
  3. param:float,目前还不支持,直接赋值0。

返回值:

  1. retval: float。结果越低,匹配越好。它是基于hu矩值计算的。

3 示例:

import cv2
import numpy as np

# 1 读取图像
img = cv2.imread('C:/Users/xxx/Downloads/matchImg.png',0)

# 2 二值化图像
ret, thresh = cv2.threshold(img, 127, 255,0)

# 3 查找轮廓
contours,hierarchy = cv2.findContours(thresh,2,1)

n=len(contours)       #轮廓个数
contoursImg=[]
cv2.imshow("original",img)  #显示原图


# 逐一在全黑背景上绘制并显示轮廓
for i in range(n):
    temp = np.zeros(img.shape, np.uint8)  # 生成黑背景
    contoursImg.append(temp)
    contoursImg[i] = cv2.drawContours(contoursImg[i], contours, i, (255, 255, 255), 3)  # 绘制轮廓
    cv2.imshow("contours[" + str(i) + "]", contoursImg[i])#显示轮廓
    
    # 计算点(100,100)到轮廓线的距离
    retval=cv2.pointPolygonTest(contours[i],(100,100),True)
    print(f"点(100,100)到轮廓线{i}之间的最短距离={retval}.")


# 逐一匹配各个轮廓的匹配度
for i in range(n):
    cnt1 = contours[i]
    for j in range(i+1,n):
        cnt2 = contours[j]
        ret = cv2.matchShapes(cnt1,cnt2,1,0.0)
        print(f'轮廓线{i}与轮廓线{j}的匹配结果 = {ret}.')
      
cv2.waitKey()
cv2.destroyAllWindows()

ret = cv2.matchShapes(cnt1,cnt2,1,0.0)
print( ret )

运行结果如下:

点(100,100)到轮廓线0之间的最短距离=-193.1035991378721.
点(100,100)到轮廓线1之间的最短距离=-124.1933975700802.
点(100,100)到轮廓线2之间的最短距离=-76.48529270389177.
点(100,100)到轮廓线3之间的最短距离=-3.0.
轮廓线0与轮廓线1的匹配结果 = 0.014466617923582503.
轮廓线0与轮廓线2的匹配结果 = 0.7722879434702737.
轮廓线0与轮廓线3的匹配结果 = 1.6770975646890678.
轮廓线1与轮廓线2的匹配结果 = 0.7578213255466912.
轮廓线1与轮廓线3的匹配结果 = 1.6626309467654852.
轮廓线2与轮廓线3的匹配结果 = 1.0329659577085004.

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于OpenCV的C++代码实现图像轮廓匹配中的轮廓筛选: ```cpp #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main() { // 读取模板图像和目标图像 Mat templateImg = imread("template.jpg", IMREAD_GRAYSCALE); Mat targetImg = imread("target.jpg", IMREAD_GRAYSCALE); // 定义轮廓存储变量 vector<vector<Point>> templateContours, targetContours; vector<Vec4i> templateHierarchy, targetHierarchy; // 寻找模板图像和目标图像的轮廓 findContours(templateImg, templateContours, templateHierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); findContours(targetImg, targetContours, targetHierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); // 定义轮廓筛选阈值 double minArea = 100; double maxArea = 1000; // 对模板图像轮廓进行筛选 vector<vector<Point>> filteredTemplateContours; for (int i = 0; i < templateContours.size(); i++) { double area = contourArea(templateContours[i]); if (area > minArea && area < maxArea) { filteredTemplateContours.push_back(templateContours[i]); } } // 对目标图像轮廓进行筛选 vector<vector<Point>> filteredTargetContours; for (int i = 0; i < targetContours.size(); i++) { double area = contourArea(targetContours[i]); if (area > minArea && area < maxArea) { filteredTargetContours.push_back(targetContours[i]); } } // 显示筛选后的模板图像和目标图像轮廓 Mat templateContoursImg = Mat::zeros(templateImg.size(), CV_8UC3); drawContours(templateContoursImg, filteredTemplateContours, -1, Scalar(0, 255, 0), 2); imshow("Filtered Template Contours", templateContoursImg); Mat targetContoursImg = Mat::zeros(targetImg.size(), CV_8UC3); drawContours(targetContoursImg, filteredTargetContours, -1, Scalar(0, 255, 0), 2); imshow("Filtered Target Contours", targetContoursImg); waitKey(0); return 0; } ``` 注:以上代码中的`template.jpg`和`target.jpg`分别为模板图像和目标图像,可以根据实际情况进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值