我正在尝试从下面的图像中提取数字/数字:
image from which to extract digits
首先我在OCR上进行了尝试,但是输出不相关,所以我计划将opencv
与python
一起使用
我写了以下代码:
import numpy as np
import cv2
from matplotlib import pyplot as plt
#cv2.IMREAD_COLOR = 1 , Loads a color image. Any transparency of image will be neglected. It is the default flag.
#cv2.IMREAD_GRAYSCALE = 0 , Loads image in grayscale mode
#cv2.IMREAD_UNCHANGED = -1 , Loads image as such including alpha channel
image = cv2.imread( './in.png' , 1 )
gray = cv2.cvtColor( image, cv2.COLOR_BGR2GRAY )
ret1, thresh1 = cv2.threshold( gray, 127, 255, cv2.THRESH_BINARY )
image[ thresh1 == 0 ] = 255
kernal = cv2.getStructuringElement( cv2.MORPH_ELLIPSE, ( 5,5 ) )
erosion = cv2.erode( image, kernal, iterations = 1 )
cv2.imwrite( 'res.png', erosion )
我得到的输出是:
目前,我至少站在某个地方完成任务,但是我无法决定进一步提取内容的方法,我在contours
上搜索并找到了opencv
,因此我尝试使用此代码:
from imutils.perspective import four_point_transform
from imutils import contours
import imutils
import cv2
#cv2.IMREAD_COLOR = 1 , Loads a color image. Any transparency of image will be neglected. It is the default flag.
#cv2.IMREAD_GRAYSCALE = 0 , Loads image in grayscale mode
#cv2.IMREAD_UNCHANGED = -1 , Loads image as such including alpha channel
image = cv2.imread( './input.png' , 1 )
image = imutils.resize(image, height=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 50, 200, 255)
cv2.imwrite( 'res.png', edged )
再次根据contours
逻辑,我无法指出如何在图像中找到所需数字的位置。
我真的很困惑,如何从图像中提取数字。
我正在寻找一些提示,指南甚至代码来完成任务
预先感谢