我試圖從下圖中提取文本行,并嘗試了下面的代碼,但我只得到一個沒有任何資料的影像。在屏蔽影像時,所有線條都被完美地屏蔽了。下面我附上了蒙版影像、最終輸出影像和所需的輸出影像。
img = cv.imread('Handwritten_data/Ostu_images/H_1.jpg')
lower = (0, 0, 0)
upper = (0, 120, 150)
# threshold on border color
mask = cv.inRange(img, lower, upper)
# dilate threshold
kernel = cv.getStructuringElement(cv.MORPH_RECT, (250,10))
mask = cv.morphologyEx(mask, cv.MORPH_DILATE, kernel)
# recolor border to white
img[mask==255] = (255,255,255)
# Inverting the mask by
# performing bitwise-not operation
mask_black = cv.bitwise_not(mask)
Mask = cv.bitwise_and(img, img, mask = mask_black)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# otsu threshold
thresh = cv.threshold(gray, 0, 255, cv.THRESH_OTSU )[1]
# apply morphology open
kernel = cv.getStructuringElement(cv.MORPH_RECT, (250,10))
morph = cv.morphologyEx(thresh, cv.MORPH_OPEN, kernel)
# creating a folder
try:
# creating a folder named data
if not os.path.exists('Image_0'):
os.makedirs('Image_0')
# if not created then raise error
except OSError:
print ('Error: Creating directory of data')
# find contours and bounding boxes
bboxes = []
bboxes_img = img.copy()
contours = cv.findContours(morph, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
for cntr in contours:
x,y,w,h = cv.boundingRect(cntr)
cv.rectangle(bboxes_img, (x, y), (x w, y h), (0,0,255), 1)
bboxes.append((x,y,w,h))
for j in range(len(bboxes)):
(x,y,w,h) = bboxes[j]
crop = img[y-10:y h 10, x-10:x w 10]
cv.imwrite(f'Image_0/S_{j}.jpg', crop)
任何解決此問題的建議或幫助。
下面是遮罩圖

最終影像輸出

所需的影像輸出就像

提前致謝
uj5u.com熱心網友回復:
您為文本行分割提出的想法是正確的。但這種方法需要一些調整。
筆記:
每當您想遮蓋影像的一部分時,請確保遮蓋區域為白色。因為在尋找輪廓時,演算法會尋找白色區域。由于您正在尋找黑色的輪廓,因此演算法錯過了它。
下面根據上述思路進行修改。
解決方案:
img = cv2.imread(image_file)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh2 = cv2.threshold(img_gray, 150, 255, cv2.THRESH_BINARY_INV)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (150,2))
mask = cv2.morphologyEx(thresh2, cv2.MORPH_DILATE, kernel)
下面是蒙版圖片:

bboxes = []
bboxes_img = img.copy()
contours = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
for cntr in contours:
x,y,w,h = cv2.boundingRect(cntr)
cv2.rectangle(bboxes_img, (x, y), (x w, y h), (0,0,255), 1)
bboxes.append((x,y,w,h))
以下是帶有邊界框的最終結果:

您現在可以添加您的部分代碼以將每個區域保存為單獨的影像檔案。您可以嘗試修改內核引數并探索不同的形態學操作以獲得更好的遮罩區域。
Hope this puts you on the right track!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/457150.html
上一篇:在網路攝像頭視頻上創建多個ROI
