【发布时间】:2020-01-23 17:13:20
【问题描述】:
我想加入附近的边界框,现在我能够检测到每个单词的边界框。
当前代码为每个字母提供边界框,我该如何修改使其给出一个边界框取决于公差
rect_box = []
im = cv2.imread('input.jpg')
grayImage = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
_,thresh = cv2.threshold(grayImage, 150, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(1,31))
dilated = cv2.dilate(thresh, kernel, iterations = 15) # dilate
contours0,_ = cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # get contours
contours = [cv2.approxPolyDP(cnt, 50, True) for cnt in contours0]
(contours, boundingBoxes) = sort_contours(contours, method="top-to-bottom")
contours, hierarchy = cv2.findContours(thresh, 1, 2)
for contour in contours:
[x,y,w,h] = cv2.boundingRect(contour)
if h>100 and w>100:
continue
if h<10 or w<10:
continue
pad_w, pad_h = int(0.05*w), int(0.15*h)
cv2.rectangle(im,(x-pad_w,y-pad_h),(x+w+pad_w,y+h+pad_h),(255,0,255),1,shift=0)
rect_box.append(((x-pad_w,y-pad_h),(x+w+pad_w,y+h+pad_h)))
cv2.imwrite("rectangle.png", im)
cv2.imshow('image', im)
cv2.waitKey(0)
cv2.destroyAllWindows()
# For joining words
out = []
prev = rect_box[0]
temp_list = [rect_box[0][0]]
for num in rect_box[0:]:
if num[0]-10 > prev[0]:
out += [temp_list]
temp_list = [num[0]]
else:
temp_list.append(num)
prev = num
out.append(temp_list)
for i in out:
i.pop(0)
out_1 = [x for x in out if x != []]
out_2 = []
for i in out_1:
min_v = list(map(min, zip(*i)))
max_v = list(map(max, zip(*i)))
out_2.append((min_v[0], min_v[1], max_v[2],max_v[3]))
for i in out_2:
x,y,w,h = i
print(x,y,w,h)
cv2.rectangle(im,(x,y),(w,y+h),(255,0,255),1,shift=0)
def sort_contours(cnts, method="left-to-right"):
# initialize the reverse flag and sort index
reverse = False
i = 0
# handle if we need to sort in reverse
if method == "right-to-left" or method == "bottom-to-top":
reverse = True
# handle if we are sorting against the y-coordinate rather than
# the x-coordinate of the bounding box
if method == "top-to-bottom" or method == "bottom-to-top":
i = 1
# construct the list of bounding boxes and sort them from top to
# bottom
boundingBoxes = [cv2.boundingRect(c) for c in cnts]
(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
key=lambda b:b[1][i], reverse=reverse))
# return the list of sorted contours and bounding boxes
return (cnts, boundingBoxes)
输入: 输出: 附加坐标后,我将它们分组在 y 轴的基础上,并取最小值,最大值。这给了我线条的边界框,就像我使用 x 轴对整个区域所做的一样。但是,不知何故,它没有按我想要的方式工作。
【问题讨论】:
-
你能贴出所有代码吗?缺少
sort_contours()函数。 -
@stephen 请检查
标签: python image opencv image-processing contour