簡體   English   中英

如何從圖像中刪除方括號?

[英]How to remove square brackets from an image?

我有這張圖片: raw

我想從此圖像中刪除方括號。 我到目前為止:

# Import packages 
import cv2
import numpy as np

#Create MSER object
mser = cv2.MSER_create()

#Your image path i-e receipt path
img = cv2.imread('uky.JPG')

#Convert to gray scale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

vis = img.copy()

#detect regions in gray scale image
regions, _ = mser.detectRegions(gray)

hulls = [cv2.convexHull(p.reshape(-1, 1, 2)) for p in regions]

cv2.polylines(vis, hulls, 1, (0, 255, 0))

cv2_imshow(vis)

mask = np.zeros((img.shape[0], img.shape[1], 1), dtype=np.uint8)

for contour in hulls:

    cv2.drawContours(mask, [contour], -1, (255, 255, 255), -1)

#this is used to find only text regions, remaining are ignored
text_only = cv2.bitwise_and(img, img, mask=mask)

cv2_imshow(text_only)

此代碼的結果:檢測到

預計 output:預計

但我不知道如何刪除方括號。 我確定這是一個如此簡單的問題,但由於我不熟悉 OpenCV,所以我幾個小時都無法解決這個問題。

如果有人能向我解釋這一點,我會很高興。 非常感謝你提前。

這是 Python/OpenCV 中的一種方法。 獲取輪廓並按縱橫比和面積進行過濾。 在白色背景上將剩余的輪廓繪制為黑色。

輸入:

在此處輸入圖像描述

import cv2
import numpy as np

#Read input image
img = cv2.imread('brackets.jpg')

# threshold on black
lower =(0,0,0) 
upper = (50,50,50) 
thresh = cv2.inRange(img, lower, upper)

# find contours and get one with area about 180*35
contours = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]

# filter contours on aspect ratio and area
max_aspect = 3.7
min_aspect = 0.7
min_area = 15
result = np.full_like(img, (255,255,255))
for cntr in contours:
    area = cv2.contourArea(cntr)
    x,y,w,h = cv2.boundingRect(cntr)
    aspect = h/w
    if aspect > min_aspect and aspect < max_aspect and area > min_area:
            cv2.drawContours(result, [cntr], -1, (0, 0, 0), 2)

# save result
cv2.imwrite("brackets_thresh.jpg", thresh)
cv2.imwrite("brackets_removed.jpg", result)

# show images
cv2.imshow("THRESH", thresh)
cv2.imshow("RESULT", result)
cv2.waitKey(0)

閾值圖像:

在此處輸入圖像描述

結果:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM