cv2.matchShapes和cv2.matchTemplate

博客主要探讨了cv2.matchShapes和cv2.matchTemplate的区别,这两个函数在图像处理领域有不同应用,了解它们的差异有助于更好地进行相关开发工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cv2.matchShapes和cv2.matchTemplate的区别是什么?

import cv2 import numpy as np # 读取深度图像原图 depth_image = cv2.imread("C:\\Users\\86199\\Desktop\\0331\\depth_image_00007.tiff", -1).astype(np.float32) image = cv2.imread("C:\\Users\\86199\\Desktop\\0331\\rgb_image_00007.png") # 深度值反转处理 max_depth, min_depth = depth_image.max(), depth_image.min() processed_depth = np.where(depth_image != 0, max_depth - depth_image, 0) # 二值化 thresh = processed_depth.max() - 16 _, binary_img = cv2.threshold(processed_depth, thresh, 255, cv2.THRESH_BINARY) binary_img = binary_img.astype(np.uint8) # 腐蚀 kernel = np.ones((20, 20), np.uint8) eroded_img = cv2.erode(binary_img, kernel, iterations=1) cv2.imwrite("eroded_img.png", eroded_img) # 查找轮廓 contours, _ = cv2.findContours(eroded_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 过滤绘制矩形参数 min_dimension = 20 # 最小长宽阈值(用以过滤非砖块区域) #绘制中心点 marker_size = 10 # 中心点标记大小 marker_thickness = 2 # 中心点标记粗细 for contour in contours: # 获取最小外接矩形 rect = cv2.minAreaRect(contour) width, height = rect[1] # 过滤小尺寸矩形(考虑旋转因素取最小边) if min(width, height) < min_dimension: continue # 绘制矩形 print([rect[0]]) box = cv2.boxPoints(rect).astype(int) cv2.drawContours(image, [box], 0, (0, 255, 0), 2) # 绘制中心点 center = tuple(map(int, rect[0])) # 将中心坐标转换为整数 # 绘制标记 cv2.drawMarker(image, center, (0,0,255), markerType=cv2.MARKER_CROSS, markerSize=marker_size, thickness=marker_thickness) # 保存结果 cv2.imwrite('final_result.jpg', image) print("砖块位置已保存为 final_result.jpg") 完善为在第一张图中找到的效果较好的轮廓,第二张图以第一张图找到的轮廓为模板轮廓去匹配
最新发布
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

code bean

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

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

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

打赏作者

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

抵扣说明:

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

余额充值