【发布时间】:2020-06-25 19:01:23
【问题描述】:
所以我得到了一个盒子的图像,盒子里有许多大小不一的刻度线,就像一把尺子。如下图:
到目前为止,我的情况是,通过边缘检测,我只能将外部矩形检测为矩形,但不能检测矩形内的任何刻度线。代码如下:
import numpy as np
import cv2
image = cv2.imread('images\Ruler.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3, 3), 0)
edges = cv2.Canny(blur, 50, 200)
cnts, hierarchy = cv2.findContours(edges, cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE)
corner_points = []
for index, cnt_points in enumerate(cnts):
perimeter = cv2.arcLength(cnts[index], True)
approx = cv2.approxPolyDP(cnts[index], 0.02 * perimeter, True)
corner_points.append(approx)
x, y, w, h = cv2.boundingRect(corner_points[index])
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
print(corner_points)
cv2.imshow("Contour", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
我希望生成的图像类似于下图所示:
正如您所见,不仅检测到刻度线(红色轮廓)和外部矩形(绿色轮廓),而且您还能够将刻度线与外部矩形区分开来。我也在尝试获取刻度线角点的像素位置,以及在我的代码中看到的我将角点存储到“角点 = []”
我也不确定刻度线是否被视为粗线或矩形。因此位置角点可以是刻度线“线”的两个端点,也可以是刻度线“矩形”的 4 个顶点。
【问题讨论】:
标签: python opencv rectangles edge-detection