2015-04-06 73 views

回答

1

openCV沒有直接的方法來做到這一點。您可以使用方法floodFill編寫函數,並將邊界像素作爲種子點循環。

floodFill(dstImg,seed,Scalar (0));

其中:

dstImg:輸出與邊框去除。

種子:[(X,Y)點的所有的邊界座標

標量(0):如果朝向種子點連接區域被發現要填充的顏色。因此(0)正如你的情況是將它填充爲黑色。

樣品:

int totalRows = srcImg.rows; 
int totalCols = srcImg.cols; 
int strt = 0, flg = 0; 
int iRows = 0, jCols = 0; 
while (iRows < srcImg.rows) 
{ 
    if (flg ==1) 
     totalRows = -1; 
    Point seed(strt,iRows);  
    iRows++; 
    floodFill(dstImg,seed,Scalar (0)); 
    if (iRows == totalRows) 
    { 
     flg++; 
     iRows = 0; 
     strt = totalCols - 1; 
    } 
}  

同樣不修改它的列。 希望它有幫助。

+0

謝謝斯利拉姆的答覆外的座標,你能告訴我用cv2替換邊界組件的簡單代碼嗎?功能以及如何使用此FloodFill – Deepak 2015-04-06 07:13:36

+0

回答更新了代碼...!您還需要爲列添加類似的代碼。嘗試自己實現... – sriram 2015-04-06 09:00:26

+0

這是否適合你? – sriram 2015-04-07 05:37:56

1

不是很優雅,但你可以在邊框包圍每個輪廓和測試是否該矩形秋天或圖像(IM)的邊界

for c in contours: 
    include = True 
    # omit this contour if it touches the edge of the image 
    x,y,w,h = cv2.boundingRect(c)  
    if x <= 1 or y <=1: 
     include = False     
    if x+w+1 >= im.shape[1] or y+h+1 >= im.shape[0]: 
     include = False 
    # draw the contour 
    if include == True: 
     cv2.drawContours(im, [c], -1, (255, 0, 255), 2)