import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd="D:\Tesseract\tesseract.exe"
i = cv2.imread('1.png')
himg,wimg,_ = i.shape
k= [b.split(' ') for b in pytesseract.image_to_boxes(i).splitlines()]
for x,y,w,h in [int(x) for x in [a[1],a[2],a[3],a[4]] for a in k] :
cv2.rectangle(i, (x, himg-y), (w, himg + h))
我的最终目标是使用cv2在图像中的每个字母周围绘制框。矩形(i, (x, him -y), (w, him + h))。请帮忙处理最后两行。我想让它越小越好
要在图像中的每个字母周围绘制框,可以使用循环遍历边框列表,并为每个框绘制一个矩形:
for box in k:
x, y, w, h = box[1], box[2], box[3], box[4]
cv2.rectangle(i, (x, y), (x+w, y+h), (0, 255, 0), 2)
完整代码:
import cv2
import pytesseract
# Set the path to the Tesseract executable
pytesseract.pytesseract.tesseract_cmd = "D:\Tesseract\tesseract.exe"
# Read the image
i = cv2.imread('1.png')
# Extract the bounding boxes for each letter in the image
k = [b.split(' ') for b in pytesseract.image_to_boxes(i).splitlines()]
# Iterate over the bounding boxes and draw a rectangle around each letter
for box in k:
x, y, w, h = box[1], box[2], box[3], box[4]
cv2.rectangle(i, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Show the image
cv2.imshow('image', i)
cv2.waitKey(0)
cv2.destroyAllWindows()