28

次で作成されたポリラインを含むバイナリ イメージがあります。

cv2.polylines(binaryImage,contours,1, (255,255,255))

私が今必要としているのは、すべてのポリラインを埋める効果的な方法です。opencv でそのようなメソッドは見つかりませんでしたが、存在する可能性があります。あるいは、アルゴリズムを実装して仕事をすることもできます(ただし、高速なもの-HD対応の写真があります)。あなたの考えを共有してください..

4

4 に答える 4

57

あなたが探しているのはcv2.fillPoly、1 つ以上のポリゴンで囲まれた領域を埋める だと思います。これは単純なスニペットです。正方形の頂点を表す 4 つの点の輪郭を生成し、次に多角形を白色で塗りつぶします。

import numpy as np
import cv2

contours = np.array( [ [50,50], [50,150], [150, 150], [150,50] ] )
img = np.zeros( (200,200) ) # create a single channel 200x200 pixel black image 
cv2.fillPoly(img, pts =[contours], color=(255,255,255))
cv2.imshow(" ", img)
cv2.waitKey()

ここに画像の説明を入力

于 2013-10-07T10:37:30.017 に答える
49

cv2.drawContours()で使用thickness=cv2.FILLED:

cv2.drawContours(img, contours, -1, color=(255, 255, 255), thickness=cv2.FILLED)
于 2016-02-27T14:26:27.277 に答える
3

輪郭が閉じている場合は、fillPolyまたはdrawContoursを使用できます。@jabaldonedo と @ash-ketchum の回答をまとめると、次のようになります。

import cv2
import matplotlib.pyplot as plt
import numpy as np

# Lets first create a contour to use in example
cir = np.zeros((255,255))
cv2.circle(cir,(128,128),10,1)
res = cv2.findContours(cir.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = res[-2] # for cv2 v3 and v4+ compatibility

# An open circle; the points are in contours[0]
plt.figure()
plt.imshow(cir)

# Option 1: Using fillPoly
img_pl = np.zeros((255,255))
cv2.fillPoly(img_pl,pts=contours,color=(255,255,255))
plt.figure()
plt.imshow(img_pl)

# Option 2: Using drawContours
img_c = np.zeros((255,255))
cv2.drawContours(img_c, contours, contourIdx=-1, color=(255,255,255),thickness=-1)
plt.figure()
plt.imshow(img_c)

plt.show()

img_pl と img_c の両方に、contour[0] の点からの塗りつぶされた円が含まれています

コンテキストとして、これは python 3.6.2、OpenCV (cv2. version ) 3.2.0、numpy 1.13.1、および matplotlib 2.0.2 でテストされました。cv2 3+ および python 3.5+ なら何でも動作すると思います。@elyas-karimi (および OpenCV ドキュメント) によると、findContours は 3.* で 3 つの値を返し、4.* で 2 つの値を返し、画像の戻り値をドロップします (3.2 以降は変更されていません)。

于 2020-05-11T13:30:39.747 に答える