2015-10-06 20 views
1

去除最大的輪廓我有一個形象,像這樣從圖像

enter image description here

我想檢測和讓我結束了,僅僅有文本的圖像從該圖像中刪除箭頭。

我嘗試下面的方法,但它不工作

image_src = cv2.imread("roi.png") 
gray = cv2.cvtColor(image_src, cv2.COLOR_BGR2GRAY) 
canny=cv2.Canny(gray,50,200,3) 
ret, gray = cv2.threshold(canny, 10, 255, 0) 
contours, hierarchy = cv2.findContours(gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) 
largest_area = sorted(contours, key=cv2.contourArea)[-1] 
mask = np.ones(image_src.shape[:2], dtype="uint8") * 255 
cv2.drawContours(mask, [largest_area], -1, 0, -1) 
image = cv2.bitwise_and(image_src, image_src, mask=mask) 

上面的代碼似乎給我回用箭頭相同的圖像。

我該如何移除箭頭?

+0

我在關注這篇文章,所以我使用了'bitwse_and' http://www.pyimagesearch.com/2015/02/09/removing-contours-image-using-python-opencv/。我會檢查我的代碼 – Anthony

回答

4

下面將刪除最大的輪廓:

import numpy as np 
import cv2 

image_src = cv2.imread("roi.png") 

gray = cv2.cvtColor(image_src, cv2.COLOR_BGR2GRAY) 
ret, gray = cv2.threshold(gray, 250, 255,0) 

image, contours, hierarchy = cv2.findContours(gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) 
mask = np.zeros(image_src.shape, np.uint8) 
largest_areas = sorted(contours, key=cv2.contourArea) 
cv2.drawContours(mask, [largest_areas[-2]], 0, (255,255,255,255), -1) 
removed = cv2.add(image_src, mask) 

cv2.imwrite("removed.png", removed) 

注意,在這種情況下,最大的輪廓將是整個圖像,因此它實際上是中國第二大的輪廓。

+0

嗯使用'cv2.add'獲取被刪除的圖像的方法可能無法正常工作,如果箭頭用黑色填充,但是對不對?如果刪除的圖像是通過繪製所有剩餘的輪廓繪製的,您是否認爲它適用於大量圖像? – Anthony

+0

由於面膜是白色的,我希望'黑色+白色=白色'。繪製所有剩餘的輪廓絕對是一種替代方法。 –