雖然您的總體任務目標的更多,你的實際問題是關於你的第2步,如何實現對數據集洪水填充算法它已經檢測到氣泡中的文字。因爲你不提供源代碼,所以我必須從頭開始創建一些東西,希望能夠與步驟1中的輸出很好地接合。爲此,我只需要2個固定的座標,就可以將白點靠近創建的blob中心您在步驟1中提取的文本。只要您提供了正確的代碼,就可以調整該界面。
我冒昧地填補了您找到的字母所產生的所有內部漏洞,如果您不想要,可以跳過第36行的代碼。
對於這個解決方案,我實際上是從兩段代碼中獲得了一些想法,我在下面的剪輯中引用了這些代碼。您可以在那裏找到更多有用的信息。
讓我們發佈您的進度!
import cv2
import numpy as np
# with ideas from:
# http://www.learnopencv.com/filling-holes-in-an-image-using-opencv-python-c/
# http://stackoverflow.com/questions/10316057/filling-holes-inside-a-binary-object
print cv2.__file__
# Read image
im_in = cv2.imread("gIEXY.png", cv2.IMREAD_GRAYSCALE);
# Threshold.
# Set values equal to or above 200 to 0.
# Set values below 200 to 255.
th, im_th = cv2.threshold(im_in, 200, 255, cv2.THRESH_BINARY_INV);
# Copy the thresholded image.
im_floodfill = im_th.copy()
# Mask used to flood filling.
# Notice the size needs to be 2 pixels than the image.
h, w = im_th.shape[:2]
mask = np.zeros((h+2, w+2), np.uint8)
# Floodfill from points inside baloons
cv2.floodFill(im_floodfill, mask, (80,400), 128);
cv2.floodFill(im_floodfill, mask, (610,90), 128);
# Invert floodfilled image
im_floodfill_inv = cv2.bitwise_not(im_floodfill)
# Combine the two images to get the foreground
im_out = im_th | im_floodfill_inv
# Create binary image from segments with holes
th, im_th2 = cv2.threshold(im_out, 130, 255, cv2.THRESH_BINARY)
# Create contours to fill holes
im_th3 = cv2.bitwise_not(im_th2)
contour,hier = cv2.findContours(im_th3,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contour:
cv2.drawContours(im_th3,[cnt],0,255,-1)
segm = cv2.bitwise_not(im_th3)
# Display image
cv2.imshow("Original", im_in)
cv2.imshow("Segmented", segm)
cv2.waitKey(0)
來源
2016-02-03 20:01:26
tfv
由於這些白色背景(在文字氣泡內)是連續的,您是否嘗試過連接的組件? –
連接組件標籤是我以後喜歡使用的*,即在生成的掩碼中枚舉特定的氣泡。我沒有看到在原始圖像上使用它的很多觀點。 – GreyCat
洪水填充和連接組件標籤與這些圖像密切相關。如果氣泡周圍的邊緣被關閉,或者可以關閉,這應該給你一個相當不錯的初步估計。特別是因爲你可以測量這些區域的屬性,例如它們的正方形等。 –