行业知识
OpenCV LineMod算法调用
2026-12-03 16:58:34
来源:互联网转载
index.php/tags-41973.html" >���究好几天,终于搞清楚了opencv linemod如何调用,直接上代码
输入:
import cv2 # 导入OpenCV库,ksdg用的opencv4.7,另外因为linemod在opencv扩展库中,需要安装opencv-contrib-python# 读取图像src = cv2.imread("duck.png") # 从文件中读取图像,当作模板mask = cv2.imread("duck_mask.png", cv2.IMREAD_GRAYSCALE) # 读取掩码图像# 初始化需要匹配的图像,等于原始图像dst = src# 创建Linemod的检测器.# 注意源码中默认特征点为63个,最大也只支持63个特征点。# 而且由于默认的尺度金字塔为5,8。所以图像的尺寸需要是80(5*8*2)的倍数否则匹配会报错detector = cv2.linemod.getDefaultLINE()# 添加一个模板class_id = "duck"ret, bbox = detector.addTemplate([src], class_id, mask)# 在dst中匹配matches, quantized_images = detector.match([dst], 40, [class_id], None, None)# 遍历所有匹配结果for item in matches: template = detector.getTemplates(item.class_id, item.template_id) for i in range(0, len(detector.getModalities())): for f in template[i].features: # 将模板特征点加上匹配偏移量,绘制圆圈 x = item.x + f.x y = item.y + f.y cv2.circle(dst, (x, y), 5, (0, 255, 0), -1) # 在图像中画圆圈 # 在匹配到的位置绘制相似度 cv2.putText(dst, str(item.similarity), (item.x, item.y), cv2.FONT_HERSHEY_DUPLEX, 0.5, (0, 0, 0))# 输出结果cv2.imwrite("output.jpg", dst) 输出:
上一篇:MobileViT模型简介
下一篇:xp怎么禁用服务器?