Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在文本周围有边框的图像中使用MSER提取字符很困难_Python_Opencv_Image Processing_Ocr_Mser - Fatal编程技术网

Python 在文本周围有边框的图像中使用MSER提取字符很困难

Python 在文本周围有边框的图像中使用MSER提取字符很困难,python,opencv,image-processing,ocr,mser,Python,Opencv,Image Processing,Ocr,Mser,我正在尝试开发一个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

我正在尝试开发一个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)
这对于大多数图像都很好,但对于某些类似的图像:

外部边界也被检测为区域,轮廓在遮罩中绘制,以便边界内的所有区域都被检测为文本区域。因此,内部轮廓没有任何影响。如何防止这种情况发生,以便只检测到文本? 检测到外壳: 因此,面具:

您可以在上设置一个阈值,以便它忽略覆盖图像中某个区域以上的所有形状

for contour in hulls:
    if cv.contourArea(contour) < ThresholdArea:
        continue

    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)

使用此代码的结果:

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)

这很管用,但你能帮我再做一件事吗?我怎样把每个字母分开?有些字母被检测为单个区域,我如何改进检测以分离字母?我建议您就此提出单独的问题。请看这里的单个字符提取:@Link那么,opencv的findContours功能在查找区域和轮廓方面是否比MSER算法更有效?本教程仅使用该函数。您知道该函数使用哪种算法吗?@Link您的解决方案会导致问题,而不仅仅是检测到外部边界,忽略其他轮廓,直到出现问题。m被划分为多个等高线,正如我在下面的评论中所说的,尝试调整上面的代码以看到不同的结果。我应该如何调整它?我应该尝试改变什么以获得更好的结果?