Extract text from image using MSER in opencv python

I want to detect text in an image using mser and remove all non-text areas. Using the code below, I was able to detect the text:

import cv2
import sys


mser = cv2.MSER_create()
img = cv2.imread('signboard.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
vis = img.copy()
regions, _ = mser.detectRegions(gray)
hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]
cv2.polylines(vis, hulls, 1, (0, 255, 0))
cv2.imshow('img', vis)
if cv2.waitKey(0) == 9:
    cv2.destroyAllWindows()

How to remove all non-text areas and get only a binary image with text? I searched a lot, but could not find example code for this using python and opencv.

+4
source share
1 answer

You can get a binary image using the found outlines. Just draw the filled outlines on a blank img in white.

mask = np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8)
for contour in hulls:
    cv2.drawContours(mask, [contour], -1, (255, 255, 255), -1)

: . drawContours

, :

text_only = cv2.bitwise_and(img, img, mask=mask)
+5

Source: https://habr.com/ru/post/1677926/


All Articles