python - 图像的动态内核
问题描述
在文本提取上,如何为cv2中的morphologyEx操作动态设置内核大小?基本上,我想根据各种类型的字体、大小从图像中提取单词。我的代码仅适用于特定图像。如何找出应该为图像内容提供什么合适的内核大小?我的代码片段如下。
def text_ROI_word(thresh,output):
kernel = np.ones((2,1), np.uint8)
kernel2 = np.ones((1,4), np.uint8)
temp_img = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel,iterations=2)
word_img = cv2.dilate(temp_img,kernel2,iterations=1)
(image,contours,hierarchy ) = cv2.findContours(word_img.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(output,(x-1,y-5),(x+w,y+h),(255,0,0),1)
return output
image = cv2.imread("local path")
output_image_word=image.copy()
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret,th =
cv2.threshold(gray_image,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
output_image_word = text_ROI_word_2(th,output_image_word)
cv2.imwrite("local path", output_image_word)
使用tesseract 库,但从图像中提取单词(文本)需要有限(分钟)的时间。
解决方案
推荐阅读
- google-sheets - 基于列的自动填充数字
- sql - 以开玩笑的方式并行运行创建扩展的 sql 迁移会触发约束错误
- javascript - 如果包含 html 输入的 Div 被隐藏,则删除必需的属性
- docker - 让 `pipenv run` 运行多个命令
- html - 如果键盘显示,全屏背景自动调整大小
- python - 如何在 SymPy 中获取表达式的系数列表?
- gitlab-ci - 为什么 Gitlab CI docker build 标记问题
- python - SQLAlchemy 删除对象列表(与 bulk_save_objects 相对)
- python - Cython 能否进一步减少此函数的 Python 方法调用开销?
- excel - 在用户窗体中截断三个几乎相同的子?