Replies: 1 comment
-
在 以下是一个完整的 import cv2
import numpy as np
def has_text(image_path, threshold=100):
"""
判断图片是否包含文字
:param image_path: 图片路径
:param threshold: 用于判断是否有文本的边缘像素总数阈值
:return: 是否包含文字 (True/False)
"""
# 读取图片并转换为灰度图
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# 使用Sobel算子计算边缘
sobel_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
# 计算梯度幅值
sobel_magnitude = np.sqrt(sobel_x**2 + sobel_y**2)
# 计算边缘像素总数
edge_pixels = np.sum(sobel_magnitude > 50) # 50为经验阈值
# 判断是否有文字
return edge_pixels > threshold
# 例子
image_path = "test_image.jpg" # 替换成你的图片路径
if has_text(image_path):
print("图片包含文字")
else:
print("图片不包含文字") 代码解析:
为什么使用
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
rec模型训练时,怎样先判断图片上是否有文字,发最快numpy方法的python完整代码
Beta Was this translation helpful? Give feedback.
All reactions