从图像opencv python中提取数字

时间:2018-08-18 14:12:14

标签: python opencv

我正在尝试从下面的图像中提取数字/数字:

image from which to extract digits

首先我在OCR上进行了尝试,但是输出不相关,所以我计划将opencvpython一起使用

我写了以下代码:

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 )

我得到的输出是:

output result image

目前,我至少站在某个地方完成任务,但是我无法决定进一步提取内容的方法,我在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 )

,我得到的输出图像为: enter image description here

再次根据contours逻辑,我无法指出如何在图像中找到所需数字的位置。

我真的很困惑,如何从图像中提取数字。

我正在寻找一些提示,指南甚至代码来完成任务

预先感谢

0 个答案:

没有答案