【发布时间】:2019-10-09 13:20:51
【问题描述】:
我的目标是尝试生成一种可以计算盒子堆栈的算法。请记住,我无法对阈值进行硬编码,因为这些框可以有不同的颜色,因此我无法将其转换为二进制图像。
盒子
我尝试将其转换为灰度并使用精巧的边缘检测器来获取所有边缘,如下图所示:
kernel1 = np.ones((5, 5), np.uint8)
kernel2 = np.ones((3, 3), np.uint8)
#kernel3 = np.ones((5, 5), np.uint8)
img = cv2.dilate(img, kernel1, iterations=1)
img = cv2.erode(img, kernel2, iterations=1)
cv2.imshow("blur", img)
# img = cv2.erode(img, kernel1, iterations=1)
# img = cv2.dilate(img, kernel2, iterations=1)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel1)
canny = cv2.Canny(opening, 30, 120)
精明
之后,我使用 houghlines 函数获取所有线条。我制定了一个算法来删除行列表中的所有垂直线。下图显示了使用此代码的结果
lines = cv2.HoughLinesP(canny, 1, np.pi / 200, 90, minLineLength=20, maxLineGap=10)
for line in range(0, len(lines)):
x1, y1, x2, y2 = lines[line][0]
# cv2.line(show, (x1, y1), (x2, y2), (255, 0, 0), 2)
# cv2.imshow('first', show)
result = []
# cannot delete directly from the array because inside the for loop
# use dummy "result[]" to keep the lines that needed
# return the result back to the array after the for loop
print(len(lines))
for line in range(0, len(lines)):
x1, y1, x2, y2 = lines[line][0]
if x1 == x2:
continue
angle = math.atan(float((y2 - y1)) / float((x2 - x1)))
angle = angle * 180 / math.pi
# print(angle)
if abs(angle) <= 5 and ((y1 or y2) < (show.shape[0] - 30)):
result.append(lines[line][0])
lines = result
cv2.waitKey(0)
print(len(lines))
data = []
for line in range(0, len(result)):
x1, y1, x2, y2 = lines[line]
cv2.line(show, (x1, y1), (x2, y2), (0, 255, 0), 2)
#cv2.imshow('show2', show)
data.append((y1 + y2) / 2)
结果
我想要的结果是这样的:
我已经有一个用于对线条进行分组的 K-mean 聚类,所以我不介意线条相互堆叠。但就目前而言,我需要哪些预处理技能或技术来实现预期结果,以便我可以数箱子的堆叠?
我遇到的计划和问题:
所以我的计划是转换为灰度并使用精明的边缘勾勒出边缘。这里有一个问题,盒子上的文字也是草绘的。我试图通过使用膨胀来删除文本,但这个过程也模糊了我想要的边缘。我不知道如何获取这些边缘线,但不知道从文本中检测到的线。
【问题讨论】:
标签: python opencv image-processing