gpt4 book ai didi

python - 在文本周围有边框的图像中使用MSER难以提取字符

转载 作者:行者123 更新时间:2023-12-02 16:45:20 24 4
gpt4 key购买 nike

我正在尝试开发OCR系统。我正在尝试使用MSER以便从图像中提取字符,然后将字符传递到CNN中以识别这些字符。这是我的字符提取代码:

import cv2
import numpy as np

# create MSER object
mser = cv2.MSER_create()

# read the image
img = cv2.imread('textArea01.png')

# convert to gray scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# store copy of the image
vis = img.copy()

# detect regions in the image
regions,_ = mser.detectRegions(gray)

# find convex hulls of the regions and draw them onto the original image
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]

cv2.polylines(vis, hulls, 1, (0, 255, 0))

# create mask for the detected region
mask = np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8)
mask = cv2.dilate(mask, np.ones((150, 150), np.uint8))

for contour in hulls:

cv2.drawContours(mask, [contour], -1, (255, 255, 255), -1)

#this is used to find only text regions, remaining are ignored
text_only = cv2.bitwise_and(img, img, mask=mask)


cv2.imshow('img', vis)
cv2.waitKey(0)
cv2.imshow('mask', mask)
cv2.waitKey(0)
cv2.imshow('text', text_only)
cv2.waitKey(0)

这对于大多数图像都可以正常工作,但是对于某些图像来说像这样:
enter image description here

外边界也被检测为区域,并在蒙版中绘制轮廓,以便将边界内的所有区域检测为文本区域。因此,内部轮廓不起作用。如何防止这种情况,以便仅检测到文本?
检测到的船体:
enter image description here
结果是面具:
enter image description here

最佳答案

我的结果使用此代码:

import cv2
import numpy as np

img = cv2.imread("img.png")

# grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', gray)

# binary
# ret, thresh = cv2.threshold(gray, 250, 255, cv2.THRESH_BINARY_INV)
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 35, 180)
cv2.imshow('threshold', thresh)

# dilation
kernel = np.ones((1, 1), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cv2.imshow('dilated', img_dilation)

# find contours
# cv2.findCountours() function changed from OpenCV3 to OpenCV4: now it have only two parameters instead of 3
cv2MajorVersion = cv2.__version__.split(".")[0]
# check for contours on thresh
if int(cv2MajorVersion) >= 4:
ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
else:
im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])

for i, ctr in enumerate(sorted_ctrs):
# Get bounding box
x, y, w, h = cv2.boundingRect(ctr)

# Getting ROI
roi = img[y:y + h, x:x + w]

# show ROI
# cv2.imshow('segment no:'+str(i),roi)
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1)

# if you want to save the letters without green bounding box, comment the line above
if w > 5:
cv2.imwrite('C:\\Users\\PC\\Desktop\\output\\{}.png'.format(i), roi)

cv2.imshow('marked areas', img)

cv2.waitKey(0)

result

关于python - 在文本周围有边框的图像中使用MSER难以提取字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59464096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com