問題描述
擁有如上圖所示的圖像,我可以將其裁剪成四個方形框,使用 OpenCV 形態(tài)學(xué)操作(基本膨脹、腐蝕)去除邊框并得到如下結(jié)果:
Having an image such as one above, I am able to crop it into four square boxes, remove the borders using OpenCV morphological operations (basic dilation, erosion) and get a result such as:
這在大多數(shù)情況下效果很好,但如果有人越界寫,這可能會被預(yù)測為 7 而不是 2.
Which works great in most cases, but if someone writes over the line, this may get predicted as 7 instead of 2.
我無法找到一種解決方案,該解決方案可以在刪除邊框的同時恢復(fù)寫在線條上的字符部分.我擁有的圖像已經(jīng)轉(zhuǎn)換為灰度,因此我無法根據(jù)顏色區(qū)分書寫數(shù)字.解決這個問題的最佳方法是什么?
I am having trouble finding a solution that would recover the parts of the character written over the line while removing the borders. Images I have are already converted to grayscale so I can't distinguish written digits based on the color. What would be the best way to approach this problem?
推薦答案
這是一個管道
- 將圖像轉(zhuǎn)換為灰度
- Otsu 獲取二值圖像的閾值
- 去除豎線
- 去除水平線
- 構(gòu)建修復(fù)內(nèi)核和修復(fù)鏡像
- 反轉(zhuǎn)圖像
轉(zhuǎn)為灰度后,我們大津的閾值
After converting to grayscale, we Otsu's threshold
從這里我們刪除垂直線
然后去掉水平線
這給我們留下了字符間隙,為了解決這個問題,我們創(chuàng)建了一個修復(fù)內(nèi)核來擴(kuò)大圖像
This leaves us with a gap in the characters, to fix this, we create a repair kernel to dilate the image
接下來我們使用閾值圖像來保持我們的角色細(xì)節(jié)
Next we bitwise-and with the thresholded image to maintain our character detail
差距仍然存在,但要好一些.我們執(zhí)行 morph close 以縮小差距
The gap is still there but a little better. We perform morph close to close the gap
它現(xiàn)在已經(jīng)關(guān)閉,但我們丟失了角色細(xì)節(jié).我們使用閾值圖像執(zhí)行最終的逐位與運算以恢復(fù)我們的細(xì)節(jié)
It's now closed but we lost character detail. We perform a final bitwise-and with the thresholded image to recover our detail
為了得到想要的結(jié)果,我們反轉(zhuǎn)圖像
To get the desired result, we invert the image
import cv2
image = cv2.imread('1.png')
removed = image.copy()
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Remove vertical lines
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,40))
remove_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(remove_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(removed, [c], -1, (255,255,255), 15)
# Remove horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40,1))
remove_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(remove_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(removed, [c], -1, (255,255,255), 5)
# Repair kernel
repair_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
removed = 255 - removed
dilate = cv2.dilate(removed, repair_kernel, iterations=5)
dilate = cv2.cvtColor(dilate, cv2.COLOR_BGR2GRAY)
pre_result = cv2.bitwise_and(dilate, thresh)
result = cv2.morphologyEx(pre_result, cv2.MORPH_CLOSE, repair_kernel, iterations=5)
final = cv2.bitwise_and(result, thresh)
invert_final = 255 - final
cv2.imshow('thresh', thresh)
cv2.imshow('removed', removed)
cv2.imshow('dilate', dilate)
cv2.imshow('pre_result', pre_result)
cv2.imshow('result', result)
cv2.imshow('final', final)
cv2.imshow('invert_final', invert_final)
cv2.waitKey()
這篇關(guān)于從圖像中刪除邊框,但將文本保留在邊框上(OCR 之前的預(yù)處理)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!