基于图像处理的目标对象各边缘长度测量实现问题咨询
图像目标单边缘长度测量的实现现状
我目前尝试用机器视觉技术测量图像中目标对象的各条边缘长度,现有实现仅能获取目标的整体外接尺寸,无法完成特定单条边缘的长度计算,以下是当前使用的实现代码、对应的输入输出效果说明。
现有实现代码
from scipy.spatial import distance as dist from imutils import perspective from imutils import contours import numpy as np import imutils import cv2 image = cv2.imread("IMAGE PATH") gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (7, 7), 0) edged = cv2.Canny(gray, 50, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) cnts = cv2.findContours(edged.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) # 可取消注释开启轮廓面积过滤,筛除小面积干扰轮廓 # cnts = [x for x in cnts if cv2.contourArea(x) > 500] (cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None for c in cnts: if cv2.contourArea(c) < 500: continue orig = image.copy() # 计算当前轮廓对应的最小外接旋转矩形 box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # 统一矩形顶点排列顺序,方便后续边长计算 box = perspective.order_points(box) cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) # 遍历矩形的四个顶点并绘制标记 for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) ################################像素长度换算逻辑待补全########################
当前实现的输入输出效果
- 输入:包含待测量目标的原始场景图像
- 输出:可自动识别有效轮廓、勾画出目标的最小外接旋转矩形、标记矩形四个顶点,尚未实现单条边缘的长度计算逻辑
内容的提问来源于stack exchange,提问作者Henil Chopda
相关产品推荐
相关产品推荐

