来自 PyTesseract 的裁剪边界框
Crop bounding box from PyTesseract
我正在使用 pytesseract 来确定图像上的单个字符。我正在使用 image_to_boxes
函数,它为我提供了字符和坐标。但是,我需要将单个字符裁剪成单个图像文件。目前,下面的代码将创建边界框,但我需要它来裁剪字符并将其保存到每个字符的单独图像文件中。
filename = 'image.png'
# read the image and get the dimensions
img = cv2.imread(filename)
h, w, _ = img.shape # assumes color image
# run tesseract, returning the bounding boxes
boxes = pytesseract.image_to_boxes(img)
# draw the bounding boxes on the image
for b in boxes.splitlines():
b = b.split(' ')
img = cv2.rectangle(img, (int(b[1]), h - int(b[2])), (int(b[3]), h - int(b[4])), (0, 255, 0), 2)
您可以使用pillow's crop
函数:
from PIL import Image
img = Image.open("samp.png")
text = pytesseract.image_to_boxes(img).split("\n")
for i in text:
if i:
(left, upper, right, lower) = list(map(int, i.split(" ")[1:-1]))
im_crop = img.crop((left, upper, right, lower))
plt.imshow(im_crop)
plt.show()
或
roi = img[lower:upper, left:right]
我正在使用 pytesseract 来确定图像上的单个字符。我正在使用 image_to_boxes
函数,它为我提供了字符和坐标。但是,我需要将单个字符裁剪成单个图像文件。目前,下面的代码将创建边界框,但我需要它来裁剪字符并将其保存到每个字符的单独图像文件中。
filename = 'image.png'
# read the image and get the dimensions
img = cv2.imread(filename)
h, w, _ = img.shape # assumes color image
# run tesseract, returning the bounding boxes
boxes = pytesseract.image_to_boxes(img)
# draw the bounding boxes on the image
for b in boxes.splitlines():
b = b.split(' ')
img = cv2.rectangle(img, (int(b[1]), h - int(b[2])), (int(b[3]), h - int(b[4])), (0, 255, 0), 2)
您可以使用pillow's crop
函数:
from PIL import Image
img = Image.open("samp.png")
text = pytesseract.image_to_boxes(img).split("\n")
for i in text:
if i:
(left, upper, right, lower) = list(map(int, i.split(" ")[1:-1]))
im_crop = img.crop((left, upper, right, lower))
plt.imshow(im_crop)
plt.show()
或
roi = img[lower:upper, left:right]