我需要提取文本的邊界框并將其保存為主影像的子影像。我沒有為此任務獲得正確的代碼檔案。
請任何人都可以向我提供代碼檔案或幫助鏈接或任何可以幫助從掃描影像中裁剪文本的 python 模塊。
下面我附上了掃描的影像和預期的輸出。
下面的影像掃描副本需要從影像中裁剪文本。
import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd ='C:\\Program Files (x86)\\Tesseract-OCR\\tesseract'
img = cv2.imread("test.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 18))
dilation = cv2.dilate(thresh1, rect_kernel, iterations = 1)
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
im2 = img.copy()
file = open("recognized.txt", "w ")
file.write("")
file.close()
for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt)
rect = cv2.rectangle(im2, (x, y), (x w, y h), (0, 255, 0), 2)
cropped = im2[y:y h, x:x w]
file = open("recognized.txt", "a")
text = pytesseract.image_to_string(cropped)
file.write(text)
file.write("\n")
crop_img = img[y:y h, x:x w] # just the region you are interested
file.close

第二張影像預期裁剪影像:

uj5u.com熱心網友回復:
這是 Python/OpenCV 中的一種方法。
- 讀取輸入
- 獲得 Canny 優勢
- 獲取邊緣的外輪廓
- 過濾輪廓以去除小的無關點
- 獲取主邊簇的凸包
- 將凸包繪制為黑色背景上填充的白色作為蒙版
- 蒙版將輸入的外部區域變黑
- 從凸包中獲取旋轉的矩形
- 從旋轉矩形的負角和中心使用透視變形校正方向
- 保存結果
輸入:

import cv2
import numpy as np
# Read image
img = cv2.imread('receipt.jpg')
hh, ww = img.shape[:2]
# get edges
canny = cv2.Canny(img, 50, 200)
# get contours
contours = cv2.findContours(canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
# filter out small regions
cimg = np.zeros_like(canny)
for cntr in contours:
area = cv2.contourArea(cntr)
if area > 20:
cv2.drawContours(cimg, [cntr], 0, 255, 1)
# get convex hull and draw on input
points = np.column_stack(np.where(cimg.transpose() > 0))
hull = cv2.convexHull(points)
himg = img.copy()
cv2.polylines(himg, [hull], True, (0,0,255), 1)
# draw convex hull as filled mask
mask = np.zeros_like(cimg, dtype=np.uint8)
cv2.fillPoly(mask, [hull], 255)
# blacken out input using mask
mimg = img.copy()
mimg = cv2.bitwise_and(mimg, mimg, mask=mask)
# get rotate rectangle
rotrect = cv2.minAreaRect(hull)
(center), (width,height), angle = rotrect
box = cv2.boxPoints(rotrect)
boxpts = np.int0(box)
# draw rotated rectangle on copy of input
rimg = img.copy()
cv2.drawContours(rimg, [boxpts], 0, (0,0,255), 1)
# from https://www.pyimagesearch.com/2017/02/20/text-skew-correction-opencv-python/
# the `cv2.minAreaRect` function returns values in the
# range [-90, 0); as the rectangle rotates clockwise the
# returned angle tends to 0 -- in this special case we
# need to add 90 degrees to the angle
if angle < -45:
angle = -(90 angle)
# otherwise, check width vs height
else:
if width > height:
angle = -(90 angle)
else:
angle = -angle
# negate the angle to unrotate
neg_angle = -angle
print('unrotation angle:', neg_angle)
print('')
# Get rotation matrix
# center = (width // 2, height // 2)
M = cv2.getRotationMatrix2D(center, neg_angle, scale=1.0)
# unrotate to rectify
result = cv2.warpAffine(mimg, M, (ww, hh), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_CONSTANT, borderValue=(0,0,0))
# save results
cv2.imwrite('receipt_mask.jpg', mask)
cv2.imwrite('receipt_edges.jpg', canny)
cv2.imwrite('receipt_filtered_edges.jpg', cimg)
cv2.imwrite('receipt_hull.jpg', himg)
cv2.imwrite('receipt_rotrect.jpg', rimg)
cv2.imwrite('receipt_masked_result.jpg', result)
cv2.imshow('canny', canny)
cv2.imshow('cimg', cimg)
cv2.imshow('himg', himg)
cv2.imshow('mask', mask)
cv2.imshow('rimg', rimg)
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
精明的邊緣:

從輪廓過濾邊緣:

凸殼:

面具:

旋轉矩形:

整改結果:

uj5u.com熱心網友回復:
在 OpenCV 中,您可以使用它cv2.findContours來繪制邊界框。請參閱這篇文章,其中解釋了如何做到這一點:https ://www.geeksforgeeks.org/text-detection-and-extraction-using-opencv-and-ocr/
然后,在您獲得邊界框位置(文本所在的感興趣區域,并且您想要裁剪)之后,您可以使用 useslicing來裁剪影像:
import cv2
img = cv2.imread("lenna.png")
crop_img = img[y:y h, x:x w] # just the region you are interested
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)
如果你想直接提取文本,我認為你可以使用tesseract ocrpython 包(如何開始:https ://pypi.org/project/pytesseract/ )。您還可以使用 OpenCV 內置的 OCR 功能。閱讀更多:https ://nanonets.com/blog/ocr-with-tesseract/
uj5u.com熱心網友回復:
from PIL import image
original_image = Image.open(".nameofimage.jpg")
rotate_image = Original_image.rotate(330)
rotate_image.show()
x = 100
y = 80
h = 200
w = 200
cropped_image = rotate_image[y:y h, x:x w]
cropped_image.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/483314.html
