[英]Enhancement of Image for OCR
[這是一個示例圖像]
我想為OCR這樣的其他幾個類似的彩色圖像裁剪標題Text。 什么是最有效的步驟來預處理圖像,以便僅對標題文本進行更好的識別。
注意
對於所有想要復制代碼並想在其他項目中使用的人:您將不得不對其進行調整和調整(尤其是閾值/內核/迭代值)。 此版本最好在用戶提供的圖像上運行。
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.