我有以下影像:

本質上,我想在檢測文本的同時得到以下結果:

我目前的做法
我正在使用
文本是Bodego IV(由您的代碼正確識別)。
更新:
掩蓋整個黑暗背景:
為了掩蓋整個背景,我們可以使用以下階段(在找到top_left和之后bottom_right):
- 從我們之前找到的邊界矩形裁剪 10% 邊距的 ROI。
- 找到藍色、綠色和紅色顏色通道的中值。
假設深色背景顏色幾乎是純色,并且中值接近背景顏色。 - 構建接近中間顏色的像素值蒙版。
- 在面具中找到輪廓。
- 找到具有最大面積的輪廓(過濾噪聲)。
- 找到最大輪廓的邊界矩形。
- 用黑色填充邊界矩形。
代碼示例:
import numpy as np
import cv2
img = cv2.imread('00025.jpg')
top_left = np.array([100, 0], np.int32) # top_left we found earlier
bottom_right = np.array([562, 114], np.int32) # bottom_right we found earlier
cols = bottom_right[0] - top_left[0] # ROI width
rows = bottom_right[1] - top_left[1] # ROI height
prct10 = np.array([cols//10, rows//10], np.int32) # About 10% of width and 10% of height.
top_left = np.maximum(top_left - prct10, 0) # Subtract 10% from top left coordinate, and clip to [0, 0]
bottom_right = np.minimum(bottom_right prct10, np.array(img.shape)[1::-1]-1) # Add 10% to bottom right coordinate and clip to [img.shape[1]-1, img.shape[0]-1]
roi = img[top_left[1]:bottom_right[1], top_left[0]:bottom_right[0], :] # Crop the relevant ROI (with 10% margins from each side).
# Compute the median of B,G,R of ROI - supposed to be the BGR color of the solid background.
# Note: Due to JPEG compression, the background is not completely solid.
med = np.round(np.median(roi, axis=(0,1))).astype(np.int32)
mask = cv2.inRange(roi, np.maximum(med-5, 0), np.minimum(med 5, 255)) # Build a mask of pixels value close to the median color.
# Find contours in the mask
cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[0]
# Find contour with the maximum area (filtering noise).
c = max(cnts, key=cv2.contourArea)
rect = cv2.boundingRect(c) # Find bounding rectangle.
cv2.rectangle(roi, rect, (0, 0, 0), -1)
#img[top_left[1]:bottom_right[1], top_left[0]:bottom_right[0], :] = 0 # Fill the area with zeros.
cv2.imshow('mask', mask) # Show mask (for testing).
cv2.imshow('img', img) # Show image (for testing).
cv2.waitKey()
cv2.destroyAllWindows()
面具:

輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/514990.html
