我已经按照我解决问题的方式制作了一个脚本。这可能不是最好的方法,但我希望这会有所帮助,或者给你一个关于如何进行的新观点。
首先,我会将图像转换为 HSV 颜色空间,因为转换为二进制不是最好的方法,因为 tekst 周围有很多噪音。cv2.inRange转换后,您可以使用阈值提取文本。然后我搜索轮廓并将它们绘制在一个新的空白蒙版上。

接下来是执行一个开口,将附近的轮廓合并成一个大轮廓。打开之后是膨胀以去除左上角剩余的字符T。

接下来,我将再次搜索轮廓并绘制一个边界矩形。如果轮廓是一个完美的正方形,那么您将看不到该矩形,但如果轮廓被移动,它将形成一个内部有两个较小矩形(相反颜色)的矩形,如下所示:

最后用阈值大小再次搜索轮廓并将它们绘制在图像上。
结果:

代码:
# Import modules
import cv2
import numpy as np
# Read the image and transform to HSV colorspace.
img = cv2.imread('ID.png')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# Extract the red text.
lower_red = np.array([150,150,50])
upper_red = np.array([200,255,255])
mask_red = cv2.inRange(hsv, lower_red, upper_red)
# Search for contours on the mask.
_, contours, hierarchy = cv2.findContours(mask_red,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
# Mask for processing.
mask = np.ones(img.shape, np.uint8)*255
# Iterate through contours and draw them on mask.
for cnt in contours:
cv2.drawContours(mask, [cnt], -1, (0,0,0), -1)
# Perform opening to unify contours.
kernel = np.ones((15,15),np.uint8)
opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
# Perform dilation to remove some noises.
kernel_d = np.ones((2,2),np.uint8)
dilation = cv2.dilate(opening,kernel_d,iterations = 1)
# Seraching for contours on the new mask.
gray_op = cv2.cvtColor(dilation, cv2.COLOR_BGR2GRAY)
_, threshold_op = cv2.threshold(gray_op, 150, 255, cv2.THRESH_BINARY_INV)
_, contours_op, hierarchy_op = cv2.findContours(threshold_op, cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
# Iterate through contours and draw a bounding rectangle.
for cnt in contours_op:
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(threshold_op,(x,y),(x+w,y+h),(255,255,255),1)
# Seraching for contours again on the new mask.
_, contours_f, hierarchy_f = cv2.findContours(threshold_op, cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
# Iterate through contours and add size for thresholding out the rest.
for cnt in contours_f:
size = cv2.contourArea(cnt)
if size < 1000:
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),-1)
# Display the result.
cv2.imshow('img', img)
对于第二张图片,它不起作用,因为图片的复杂性不同。
在第二张图片上,我会尝试对其进行扩张,所以唯一剩下的就是底部的 3 条(或在移位的情况下为 4 条)线,并计算轮廓的数量。如果存在 4 个轮廓,则将其移动。

或者第二个图像的第二种方法。将轮廓拆分为 3 个单独的轮廓,cv2.reactangle()并计算它们到您创建的线的最小距离。这样,即使拆分发生在底线移动之前,您也可以计算出。
第二张图片的代码:
# Import modules
import cv2
import numpy as np
import scipy
from scipy import spatial
# Read image
img_original = cv2.imread('ID_sec4.png')
img = img_original.copy()
# Get height and weight of the image
h1, w1, ch = img.shape
# Draw line somewhere in the bottom of the image
cv2.line(img, (10, h1-10), (w1-10, h1-10), (0,0,0), 3)
# Search for contours and select the biggest one (the pattern)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, threshold = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)
_, contours, hierarchy = cv2.findContours(threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt_big = max(contours, key=cv2.contourArea)
# Draw white rectangles to seperate the extreme left and extreme right side of the contour
x, y, w, h = cv2.boundingRect(cnt_big)
cv2.rectangle(img,(0,0),(x+20,y+h+20),(255,255,255),2)
cv2.rectangle(img,(w1,0),(w, y+h+20),(255,255,255),2)
# Search for contours again
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY_INV)
_, cnts, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
# Iterate over the list and calculate minimum distance from the line (line you drew)
# and contours then make a bounding box if it fits the criteria
for i in cnts:
reshape1 = np.reshape(i, (-1,2))
ref = max(cnts, key=lambda cnts: cv2.boundingRect(cnts)[1])
reshape2 = np.reshape(ref, (-1,2))
tree = spatial.cKDTree(reshape2)
mindist, minid = tree.query(reshape1)
distances = np.reshape(mindist, (-1,1))
under_min = [m for m in distances if 1 < m < 70]
if len(under_min) > 1:
x, y, w, h = cv2.boundingRect(i)
cv2.rectangle(img_original,(x-10,y-10),(x+w+10,y+h+10),(0,255,0),2)
# Display the result
cv2.imshow('img', img_original)
结果:



希望它有点帮助。干杯!