【问题标题】:How to detect text on an X-Ray image with OpenCV如何使用 OpenCV 检测 X 射线图像上的文本
【发布时间】: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


    【解决方案1】:

    这是一个简单的方法:

    1. 获取二进制图像。加载图像,创建空白掩码,转换为grayscale,Gaussian blur,然后Otsu's threshold

    2. 将文本合并到单个轮廓中。 由于我们想将文本提取为一个整体,因此我们执行morphological operations 将单个文本轮廓连接到单个轮廓中。

    3. 提取文本。我们find contours 然后使用cv2.contourArea 的轮廓区域和cv2.arcLength + cv2.approxPolyDP 的纵横比进行过滤。如果轮廓通过过滤器,我们会找到旋转的边界框并将其绘制到我们的蒙版上。

    4. 隔离文本。我们执行cv2.bitwise_and 操作来提取文本。


    这是该过程的可视化。使用此截图输入图像(因为您提供的输入图像已连接为一个图像):

    输入图像->二值图像

    Morph close -> 检测到的文本

    孤立的文字

    其他图片的结果

    输入图像->二值图像+变形关闭

    检测到的文字->孤立的文字

    代码

    import cv2
    import numpy as np
    
    # Load image, create mask, grayscale, Gaussian blur, Otsu's threshold
    image = cv2.imread('1.png')
    original = image.copy() 
    blank = np.zeros(image.shape[:2], dtype=np.uint8)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray, (5,5), 0)
    thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
    
    # Merge text into a single contour
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
    close = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=3)
    
    # Find contours
    cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    for c in cnts:
        # Filter using contour area and aspect ratio
        x,y,w,h = cv2.boundingRect(c)
        area = cv2.contourArea(c)
        ar = w / float(h)
        if (ar > 1.4 and ar < 4) or ar < .85 and area > 10 and area < 500:
            # Find rotated bounding box
            rect = cv2.minAreaRect(c)
            box = cv2.boxPoints(rect)
            box = np.int0(box)
            cv2.drawContours(image,[box],0,(36,255,12),2)
            cv2.drawContours(blank,[box],0,(255,255,255),-1)
    
    # Bitwise operations to isolate text
    extract = cv2.bitwise_and(thresh, blank)
    extract = cv2.bitwise_and(original, original, mask=extract)
    
    cv2.imshow('thresh', thresh)
    cv2.imshow('image', image)
    cv2.imshow('close', close)
    cv2.imshow('extract', extract)
    cv2.waitKey()
    

    【讨论】:

    • 太棒了,这行得通!我不得不使用阈值和区域阈值,但随后它成功检测到所有文本。如何从初始图像中着色/删除找到的框?
    • 什么意思?检测到的框以绿色突出显示。如果要删除文本,而不是将其着色为绿色,而是将其着色为黑色,并将 thickness 参数设置为 -1
    【解决方案2】:

    我使用以下命令删除了文本(在上面的代码之后):

    gray2 = cv2.cvtColor(extract, cv2.COLOR_BGR2GRAY)
    blur2 = cv2.GaussianBlur(gray2, (5,5), 0)
    thresh2 = cv2.threshold(blur2, 0, 255, cv2.THRESH_BINARY)[1]
    test = cv2.inpaint(original, thresh2, 7, cv2.INPAINT_TELEA)
    

    【讨论】:

      猜你喜欢
      • 2021-10-04
      • 2020-03-09
      • 2016-03-28
      • 2021-08-30
      • 1970-01-01
      • 2014-08-14
      • 2017-01-02
      • 2020-11-19
      • 2020-07-29
      相关资源
      最近更新 更多