from PIL import Image
import pytesseract
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
import cv2
import numpy as np
from matplotlib import pyplot as plt
import pytesseract
# 如果Tesseract OCR不在默认路径下,需要设置tesseract_cmd的路径
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' # 示例路径,根据你的安装位置修改
def displayimage(image):
image= binary_image
# 定义目标大小(800x600像素)
target_width = 800
target_height = 600
# 计算保持宽高比的缩放因子
original_height, original_width = binary_image.shape
scale_x = target_width / original_width
scale_y = target_height / original_height
scale = min(scale_x, scale_y) # 使用较小的比例来确保图像不会被拉伸
# 计算调整大小后的图像尺寸
new_width = int(original_width * scale)
new_height = int(original_height * scale)
# 计算需要填充的黑边大小(如果需要的话)
pad_x = (target_width - new_width) // 2
pad_y = (target_height - new_height) // 2
# 调整图像大小
resized_image = cv2.resize(binary_image, (new_width, new_height))
# 显示二值化后的图像
cv2.imshow('Binary Image', resized_image)
# 等待用户按键事件,参数0表示无限期等待
cv2.waitKey(0)
# 关闭所有OpenCV创建的窗口
cv2.destroyAllWindows()
try:
# 读取图像
# image_path = "Picture/Pic_826.bmp" # 替换为你的图像路径 thresh 10 白车
image_path ="Picture/Pic_93.bmp" # 替换为你的图像路径 thresh 60 黑车
# image_path ="Pic_826_1.bmp" # 替换为你的图像路径 thresh 10
# image_path ="Picture/Pic_200001.bmp" # 替换为你的图像路径 thresh 10
# image_path ="001.jpg" # 替换为你的图像路径 thresh 10
image = cv2.imread(image_path)
if image is None:
raise FileNotFoundError("指定的图片文件不存在。")
# 转换为灰度图像
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 应用二值化
_, binary_image = cv2.threshold(gray_image, 60, 255, cv2.THRESH_BINARY) # thresh 60 灰度值60以下为黑色, 以上为白
displayimage(binary_image)
# 定义腐蚀操作的结构元素(核)
erode_kernel = np.ones((5, 5), np.uint8) # 通常使用3x3或5x5的核进行腐蚀
# 应用腐蚀操作
eroded_image = cv2.erode(binary_image, erode_kernel, iterations=2)
# 定义膨胀操作的结构元素(核),这里可以使用与腐蚀相同的核,也可以不同
dilate_kernel = np.ones((3, 3), np.uint8) # 或者使用不同的尺寸,如5x5
# 应用膨胀操作
dilated_image = cv2.dilate(eroded_image, dilate_kernel, iterations=3) # 注意这里是对腐蚀后的图像进行膨胀
plt.imshow(dilated_image, cmap='gray')
plt.title('Preprocessed Image')
plt.axis('off')
plt.show()
# 提取文本
# text = pytesseract.image_to_string(binary_image, config='--psm 8') # psm 8 表示将图像视为单个文本行
# 识别字符
text = pytesseract.image_to_string(dilated_image) # psm 6 表示将图像视为单个单词
# 打印提取的文本内容
print("提取的文本内容:")
print(text)
except FileNotFoundError:
print("错误:指定的图片文件不存在。")
except Exception as e:
print(f"发生错误:{e}")
# def displayimage(image):
标签:image,cv2,height,width,腐蚀,pytesseract,图像,tesseract,二值化
From: https://blog.csdn.net/2301_77798341/article/details/144333141