使用 Tesseract 和 OpenCV 获取图像中'NRA'字符的像素坐标

本教程将指导您使用 Python 的 Tesseract 和 OpenCV 库从图像中提取'NRA'字符的像素坐标。

步骤:

  1. 导入必要的库:
import cv2
import pytesseract
  1. 读取图像并转换为灰度图像:
image = cv2.imread('your_image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  1. 使用 Tesseract 进行 OCR 识别:
text = pytesseract.image_to_string(gray)
  1. 找到包含'NRA'字符的位置:
coords = []
for i in range(len(text)):
    if text[i:i+3] == 'NRA':
        coords.append((i, i+3))
  1. 输出坐标:
print(coords)

完整代码:

import cv2
import pytesseract

image = cv2.imread('your_image.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

text = pytesseract.image_to_string(gray)

coords = []
for i in range(len(text)):
    if text[i:i+3] == 'NRA':
        coords.append((i, i+3))

print(coords)

请确保将代码中的'your_image.jpg'替换为您的图像文件路径。这样,您就可以获取图像中包含'NRA'字符的像素坐标。


原文地址: https://www.cveoy.top/t/topic/o1zN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录