应用轮廓后如何从图像中提取文本

时间:2018-12-22 07:41:06

标签: python opencv computer-vision

我正在尝试从车牌图像中获取信件,因为我拍摄了一张图像并将其更改为灰色,然后对其应用了阈值。然后使用轮廓裁剪仅具有牌照的图像。为此,我使用python

代码:

import numpy as np
import sys
import cv2
import imutils

img = "d1.jpg"
# load the image and convert it to grayscale
image = cv2.imread(img)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

(T, threshInv) = cv2.threshold(gray, 0, 255,
cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
cv2.imshow("Threshold", threshInv)

#masking
mask = np.zeros(image.shape[:2], dtype="uint8")

# find all contours in the image and draw ALL contours on the image
cnts=cv2.findContours(threshInv.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
clone = image.copy()
cv2.drawContours(clone, cnts, -1, (0, 255, 0), 2)
print("Found {} contours".format(len(cnts)))

for cnt in cnts:
    x,y,w,h = cv2.boundingRect(cnt)
    crop = image[y:(y+h),x:(x+w)]
    if(w>300 and h>100 and w<700 and h<500):
        print("detected")
        cv2.imshow('plate',crop)
        cv2.rectangle(mask, (x, y), (x+w, y+h), 255, -1)
        break
cv2.imshow("Mask", mask)
masked = cv2.bitwise_and(clone, clone, mask=mask)
cv2.imshow("Mask Applied to Image", masked)
cv2.waitKey(0)
cv2.destroyAllWindows()

contouring applied image 谁能帮助我实现这一目标。 预先感谢。

2 个答案:

答案 0 :(得分:0)

我不确定您要做什么:光学字符识别?如果是,则可以尝试Tesseract:https://www.pyimagesearch.com/2017/07/10/using-tesseract-ocr-python/

答案 1 :(得分:0)

以下GitHub链接可能会帮助您了解从图像中提取文本的情况

https://github.com/anuj-badhwar/Indian-Number-Plate-Recognition-System

它基本上使用pytesseract库。