私は、X線画像上のテキストを検出します。目標は、各列が境界ボックスと各行が4つ全ての縁すなわちの座標[X1、X2、Y1、Y2]を含んでいる検出されたマトリックスとして指向バウンディングボックスを抽出することです。私は、Python 3とOpenCVの4.2.0を使用しています。
ここではサンプル画像には、次のとおりです。
ストリング「試験単語」、「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()
私はそれがテキストであるかどうかを確認するために、OCRを通じて結果を実行する必要がありますか?私は他にどのようなアプローチを試してみてください?
PS:私はまだかなりのコンピュータビジョンに新しく、最も概念に精通していませんよ。
ここでは単純なアプローチがあります:
バイナリイメージを取得します。ロード画像、マスクブランクへの変換作成グレースケール、ガウスぼかし、その後、大津のしきい値を
単一輪郭にテキストをマージします。私たちは一枚としてテキストを抽出したいので、我々は実行モルフォロジー演算を、単一の輪郭に個々のテキストの輪郭を接続します。
抽出テキスト。我々は、輪郭を見つける次いで、輪郭領域を使用してフィルタ
cv2.contourArea
し、アスペクト比用いcv2.arcLength
+をcv2.approxPolyDP
。輪郭がフィルタを通過した場合、我々は回転バウンディングボックスを見つけて、私たちのマスクの上にこれを描きます。アイソテキスト。私たちは、実行
cv2.bitwise_and
テキストを抽出する操作を。
ここでは、プロセスの可視化です。(自分の与えられた入力画像を1枚の画像のように接続されていたので)このscreenshotted入力画像を使用します:
入力画像->
のバイナリイメージ
モーフ近くに->
検出されたテキスト
分離されたテキスト
他の画像との結果
入力画像->
のバイナリイメージ+モーフ近いです
検出されたテキストの->
単離されたテキスト
コード
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()