【发布时间】:2020-03-09 15:27:26
【问题描述】:
我想检测 X 射线图像上的文本。目标是将定向边界框提取为矩阵,其中每一行是检测到的边界框,每一行包含所有四个边的坐标,即 [x1, x2, y1, y2]。我正在使用 python 3 和 OpenCV 4.2.0。
这是一个示例图像:
应该检测到字符串“test word”、“a”和“b”。
我关注了这个关于creating rotated boxes for contours 的OpenCV 教程和这个关于detecting a text area in an image 的stackoverflow 答案。
生成的边界框应如下所示:
我能够检测到文本,但结果包括很多没有文本的框。
这是我目前尝试过的:
img = cv2.imread(file_name)
## Open the image, convert it into grayscale and blur it to get rid of the noise.
img2gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
ret, mask = cv2.threshold(img2gray, 180, 255, cv2.THRESH_BINARY)
image_final = cv2.bitwise_and(img2gray, img2gray, mask=mask)
ret, new_img = cv2.threshold(image_final, 180, 255, cv2.THRESH_BINARY) # for black text , cv.THRESH_BINARY_INV
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
dilated = cv2.dilate(new_img, kernel, iterations=6)
canny_output = cv2.Canny(dilated, 100, 100 * 2)
cv2.imshow('Canny', canny_output)
## Finds contours and saves them to the vectors contour and hierarchy.
contours, hierarchy = cv2.findContours(canny_output, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Find the rotated rectangles and ellipses for each contour
minRect = [None] * len(contours)
for i, c in enumerate(contours):
minRect[i] = cv2.minAreaRect(c)
# Draw contours + rotated rects + ellipses
drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8)
for i, c in enumerate(contours):
color = (255, 0, 255)
# contour
cv2.drawContours(drawing, contours, i, color)
# rotated rectangle
box = cv2.boxPoints(minRect[i])
box = np.intp(box) # np.intp: Integer used for indexing (same as C ssize_t; normally either int32 or int64)
cv2.drawContours(img, [box], 0, color)
cv2.imshow('Result', img)
cv2.waitKey()
我是否需要通过 OCR 运行结果以确保它是否为文本?我应该尝试哪些其他方法?
PS:我对计算机视觉还很陌生,还不熟悉大多数概念。
【问题讨论】:
标签: python image opencv image-processing computer-vision