关键词搜索

源码搜索 ×
×

Python文档阅读笔记-OpenCV中Match Shapes

发布2021-09-21浏览1469次

详情内容

OpenCV使用cv2.matchShapes()用来对比2个图像是否相似。返回值越低说明2张图片越相似。这个是基于hu-moment值算出来的。

代码如下:

  1. import cv2
  2. import numpy as np
  3. img1 = cv2.imread('star.jpg',0)
  4. img2 = cv2.imread('star2.jpg',0)
  5. ret, thresh = cv2.threshold(img1, 127, 255,0)
  6. ret, thresh2 = cv2.threshold(img2, 127, 255,0)
  7. contours,hierarchy = cv2.findContours(thresh,2,1)
  8. cnt1 = contours[0]
  9. contours,hierarchy = cv2.findContours(thresh2,2,1)
  10. cnt2 = contours[0]
  11. ret = cv2.matchShapes(cnt1,cnt2,1,0.0)
  12. print ret

如下测试图:

 

结果为:

A图形匹配自身体结果为0.0

A图形匹配B图形结果为0.001946

A图形匹配C图形结果为0.326911

相关解析:

①threshold(img, threshold, maxval, type):

        Ⅰ:threshold设定的阈值;

        Ⅱ:maxval当灰度值大于(或小于)阈值时将该灰度赋值成的值;

        Ⅲ:type规定的是当前二值化的方式。

②findContours():查找轮廓,第一个参数是输入图像,第二个参数是轮廓检索模式,第三参数是轮廓近似方法。

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载