如何使用 OpenCV 检测 X 射线图像上的文本

Adr*_*ebs 6 python opencv image image-processing computer-vision

我想检测 X 射线图像上的文本。目标是将定向边界框提取为矩阵,其中每行都是检测到的边界框,每行包含所有四个边的坐标,即 [x1, x2, y1, y2]。我正在使用 python 3 和 OpenCV 4.2.0。

这是一个示例图像:

在此输入图像描述

应检测字符串“test word”、“a”和“b”。

我遵循了有关为轮廓创建旋转框的OpenCV 教程和有关检测图像中的文本区域的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()
Run Code Online (Sandbox Code Playgroud)

我需要通过 OCR 运行结果来确定它是否是文本吗?我还应该尝试哪些其他方法?

PS:我对计算机视觉还很陌生,还不熟悉大多数概念。

nat*_*ncy 5

这是一个简单的方法:

  1. 获取二值图像。加载图像,创建空白蒙版,转换为灰度高斯模糊,然后大津阈值

  2. 将文本合并到单个轮廓中。由于我们想要将文本提取为一个整体,因此我们执行形态学操作以将各个文本轮廓连接成单个轮廓。

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

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


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

输入图像->二值图像

变形关闭->检测到的文本

孤立的文本

与其他图像的结果

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

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

代码

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()
Run Code Online (Sandbox Code Playgroud)