我的目标是尝试生成一个可以计算盒子堆栈的算法。请记住,我不能硬编码阈值,因为框可能有不同的颜色,所以我不能将其转换为二进制图像。
盒子
我尝试的是将其转换为灰度,并使用精明的边缘检测器来获得所有的边缘,如下图所示:
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)
精明
在那之后,我使用houghline函数来获得所有的线。我已经做了一个算法来删除行列表中的所有垂直线。下图显示了使用此代码的结果
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均值聚类来对线进行分组,所以我不介意线彼此堆叠。但就目前而言,我需要哪些预处理技能或技术来实现我的预期结果,以便我可以计算堆叠的盒子?
计划和我面临的问题:
所以我计划的是我转换成灰度,并使用精明的边缘来勾画出边缘。这里有一个问题,盒子上的文本也是草图。我试图使用膨胀来删除文本,但这个过程也模糊了我想要的边缘。我不知道如何获得这些边缘线条,但不知道从文本中检测到的线条。
发布于 2019-10-09 23:01:40
你可以尝试用水平感兴趣区域(比如Rect(0,y,image_w,10) )垂直扫描你的边缘图像,并计算其中的nonZero像素。它将为您提供沿水平轴的像素密度直方图。然后,您可能需要平滑它并找到峰值。这些峰将给出分隔线。
https://stackoverflow.com/questions/58305151
复制相似问题