gpt4 book ai didi

python - 增强OCR的图像

转载 作者:行者123 更新时间:2023-12-02 16:54:36 25 4
gpt4 key购买 nike

[这是示例图像]

我想为OCR这样一些其他类似的彩色图像裁剪标题Text。什么是最有效的步骤来预处理图像,以便仅对标题文本进行更好的识别。

最佳答案

res

注意

对于所有想要复制代码并想在其他项目中使用的人:您将不得不对其进行调整和调整(尤其是阈值/内核/迭代值)。
此版本最好在用户提供的图像上运行。

import cv2

image = cv2.imread("image.jpg")
image_c = image.copy()

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # grayscale
cv2.imshow('gray', gray)
cv2.waitKey(0)

_, thresh = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) # threshold
cv2.imshow('thresh', thresh)
cv2.waitKey(0)

kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))

dilated = cv2.dilate(thresh, kernel, iterations=13) # dilate
cv2.imshow('dilated', dilated)
cv2.waitKey(0)

image, 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 i, contour in enumerate(contours):
# get rectangle bounding contour
x, y, w, h = cv2.boundingRect(contour)

roi = image_c[y:y + h, x:x + w]

if 50 < h < 100 or 200 < w < 420: # these values are specific for this example

# draw rectangle around contour on original image
rect = cv2.rectangle(image_c, (x, y), (x + w, y + h), (255, 255, 255), 1)
cv2.imshow('rectangles', rect)
cv2.waitKey(0)

cv2.imwrite('extracted{}.png'.format(i), roi)


# write original image with added contours to disk - change values above to (255,0,255) to see clearly the contours
cv2.imwrite("contoured.jpg", image_c)

关于python - 增强OCR的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49316237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com