首页 > 解决方案 > 图像的动态内核

问题描述

在文本提取上,如何为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 库,但从图像中提取单词(文本)需要有限(分钟)的时间。

标签: pythonimageopencvimage-processingtext-segmentation

解决方案


推荐阅读