Python 和 OpenCv 实现对图像本身的文本进行编码

标签 python opencv tesseract

我在 python 中有一个小代码,可以检测图像中的文本:

import cv2


image = cv2.imread("sample.jpg")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) # grayscale
_,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY_INV) # threshold
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))  
dilated = cv2.dilate(thresh,kernel,iterations = 13) # dilate
_, contours, hierarchy =  cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) # get contours

# for each contour found, draw a rectangle around it on original image
for contour in contours:
    # get rectangle bounding contour
    [x,y,w,h] = cv2.boundingRect(contour)

    # discard areas that are too large
    if h>300 and w>300:
        continue

    # discard areas that are too small
    if h<40 or w<40:
        continue

    # draw rectangle around contour on original image
    cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)

    # write original image with added contours to disk  
    cv2.imwrite("contoured.jpg", image) 

所以输出是一个新图像,在检测到的文本上有矩形。 我还有一个函数,可以对静态图像中的文本进行编码,并将编码结果显示在控制台上,该函数如下所示:

from pytesseract import image_to_string


val_1 = sys.argv[1]
text =  image_to_string(Image.open(''+val_1+''))

def encode(key, string):
    encode = []
    for i in xrange(len(string)):
        key_c = key[i % len(key)]
        encoded_c = chr(ord(string[i]) + ord(key_c) % 256)
        encode.append(encoded_c)
    encoded_string = "".join(encode)
    return base64.urlsafe_b64encode(encoded_string)

encry =  encode(key,text)
#print encry

例如,如果我给它一个包含文本的图像,它会提取文本,对其进行编码(如果我们给它一个 key )并将编码后的字符串打印到控制台上。然而,是否可以在顶部对文本进行编码图像本身,而不是将其打印在控制台上。

最佳答案

是的,这是可能的。

您需要包含文本的图像和包含文本的区域的坐标。然后你可以使用 OpenCV 函数 putText()

为此,您必须对您的实现进行一些更改。您有两种不同的选择:

  • 在包含文本的每个矩形中执行 OCR,因此您应该这样做:

    import cv2
    from pytesseract import image_to_string
    
    
    # ..various image operations..
    
    # for each contour found, draw a rectangle, perform OCR in that rectangle and write the result on the image
    for contour in contours:
        # get rectangle bounding contour
        [x,y,w,h] = cv2.boundingRect(contour)
    
        # discard areas that are too large
        if h>300 and w>300:
            continue
    
        # discard areas that are too small
        if h<40 or w<40:
            continue
    
        # draw rectangle around contour on original image
        cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,255),2)
    
        # get the image area with the text
        text_image = image[y:y+h, x:x+w]
        # perform OCR
        text = image_to_string(text_image)
        # encode the text with your function
        encry = encode(key, text)
        # write the encoded text on the image
        cv2.putText(image, encry, (x,y), cv2.FONT_HERSHEY_SIMPLEX, 4, (255,255,255), 2, cv2.LINE_AA)
    
    • 或者,您可以对整个图像执行一次 OCR 操作,然后分析识别出的文本的坐标。您应该使用 pytesseract.image_to_boxespytesseract.image_to_data 之类的东西。

我想澄清一下,我没有测试代码,因此可能存在一些不准确之处。

关于Python 和 OpenCv 实现对图像本身的文本进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49510618/

相关文章:

python - Unicode解码错误: 'utf8' codec can't decode byte

python - 通过在__init__.py 中导入子包来跳过导入路径中的目录名

python - OpenCV Python : How to save name of the recognized face from face recognition program after the face is recognised?

python - 如何用彩色分割和 alpha channel 绘制图片?

android - 如何使用由带有额外模块的主干构建的opencv for android

python - 使用 OpenCV 清理文本图像以进行 OCR 阅读

Python打印函数多次输出

python - PyQt 连接到 KeyPressEvent

java - 将输入流转换为文件

在 Google colaboratory 中安装 Tesseract