我正在尝试构建OCR以从Image中提取文本,我正在使用轮廓来形成文本字符的边界,
经过几次改变cv2.threshold的试验后,我在形成文本字符边界时得到了最佳轮廓。
#files = os.listdir(r'letters/harry.jpeg',0)
file = r'/home/naga/Documents/Naga/Machine Learning/Data_extract/letters/Harry/Harry Potter and the Sorcerer s Stone-page-006.jpg'
im1 = cv2.imread(file,0)
im = cv2.imread(file)
# ret,thresh1 = cv2.threshold(im1,180,278,cv2.THRESH_BINARY)
# _,contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
ret,thresh1 = cv2.threshold(im1,180,278,cv2.THRESH_BINARY)
kernel = np.ones((5,5),np.uint8)
dilated = cv2.dilate(im1,kernel,iterations = 1)
_,contours, hierarchy = cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
#bound the images
cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),1)
cv2.namedWindow('BindingBox', cv2.WINDOW_NORMAL)
cv2.imwrite('output2/BindingBox4.jpg',im)
现在我想创建单词的轮廓。我需要每个单词的父轮廓。 Open cv中要更改的属性是什么?
我是opencv的新手,我跟着cv2 threshold但是无法理解应用它。请提供您在单词上形成轮廓的输入。
一个简单的解决方案是在运行findcontour函数之前扩展阈值图像的结果。
ret,thresh1 = cv2.threshold(im1,180,255,cv2.THRESH_BINARY_INV)
kernel = np.ones((5,5),np.uint8)
dilated = cv2.dilate(thresh1,kernel,iterations = 2)
contours, hierarchy = cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
扩张是形态函数,其增加二进制斑点的面积。它倾向于将所有附近的斑点组合在一起形成一个单一的斑点,这正是您将文本组合成单词所需要的。
如果不是所有文本都组合成一个单词,则可以增加迭代次数。如果您不确定此处使用的值,则需要进行一些反复试验。
阅读形态学过程,以便更好地理解该主题。它是用于基本图像处理的有用工具。
作为额外的提示,尝试在openCV中搜索函数adaptivethreshold。在对文本图像进行二值化时,它将使您的生活更轻松。