无法从图片中提取数字

时间:2019-06-06 12:22:31

标签: python-3.x opencv image-processing

我正在开发一个应用程序,以便在Python 3中使用opencv从图像中读取数字。我首先将图像转换为灰度,然后应用膨胀和腐蚀去除一些噪音,然后应用阈值以仅获得黑色和白色,然后将图像写入本地磁盘以执行某些操作……,然后应用tesseract识别python的编号。

我需要从图像中提取数字。我是openCV的新手。有人知道其他任何方法来获得结果吗?

我已经分享了下面的图片链接,我试图从该图片中提取图片。预先感谢

https://drive.google.com/file/d/141y-3okLPGP_STje14ukSqSHcgtwMdRO/view?usp=sharing

import cv2
import numpy as np
import pytesseract
from PIL import Image
from pytesseract import image_to_string

# Path of working folder on Disk
src_path = "/Users/sougata.a.roy/Desktop/Images/"

def get_string(img_path):
    # Read image with opencv
    img = cv2.imread(img_path)

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

    # Apply dilation and erosion to remove some noise
    kernel = np.ones((1, 1), np.uint8)
    img = cv2.dilate(img, kernel, iterations=1)
    img = cv2.erode(img, kernel, iterations=1)

    # Write image after removed noise
    cv2.imwrite(src_path + "removed_noise.jpg", img)

    #  Apply threshold to get image with only black and white
    img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 31, 2)

    # Write the image after apply opencv to do some ...
    cv2.imwrite(src_path + 'thres.jpg', img)

    # Recognize text with tesseract for python
    result = pytesseract.image_to_string(Image.open(src_path + "thres.jpg"), lang='eng')



    return result


print('--- Start recognize text from image ---')
print(get_string(src_path + 'abcdefg195.jpg'))

print("------ Done -------")

365

0 个答案:

没有答案