2017-09-12 7 views
-2

私はpythonとopencvライブラリで作業しています。ラベル付きの固有の輪郭(または独立した輪郭を描く)

私は、カメラのキャプチャをスレッシュホールドしたり、輪郭(複数)を見つけて描画することができます。 しかし、私は問題があります。私は、一意のIDまたはタグでこれらの輪郭を識別しようとします。 (たとえば:Id:1、Id:2)を使用して追跡します。 私はこの輪郭が永続的なIDを使用する必要があります。

目標は1本の線を描き、1つ以上の輪郭を数え、時には1つ以上の輪郭を大きな輪郭に変換することです。

注:私は深度カメラと私の画像の深さの配列を扱っています。

コードを追加します。

ありがとうございます。

countours = cv2.findContours(mask, cv2.RETR_EXTERNAL, 
           cv2.CHAIN_APPROX_SIMPLE)[1] 

    # only proceed if at least one contour was found 
    if len(countours) > 0: 
     # find the largest contour in the mask, then use 
     # it to compute the minimum enclosing circle and 
     # centroid 

     for (i,c) in enumerate(countours): 
      ((x, y), radius) = cv2.minEnclosingCircle(c) 

      M = cv2.moments(c) 
      if M["m00"] > 0: 
       center = (int(M["m10"]/M["m00"]), int(M["m01"]/M["m00"])) 
       centerString = str(center) 
       x = (int(M["m10"]/M["m00"])) 
       y = (int(M["m01"]/M["m00"])) 
      else: 
       center = int(x), int(y) 




      if radius > 10: 
       # draw the circle and centroid on the frame, 
       cv2.circle(frame, (int(x), int(y)), int(radius), 
          (0, 255, 255), 2) 
       cv2.circle(frame, center, 5, (0, 0, 255), -1) 
       # then update the ponter trail 
       if self.previous_position: 
        cv2.line(self.trail, self.previous_position, center, 
          (255, 255, 255), 2) 
        cv2.add(self.trail, frame, frame) 
        print center 
    self.previous_position = center 
    if len(countours) < 1: 
     center = 0 
     self.trail = numpy.zeros((self.cam_height, self.cam_width, 3), 
          numpy.uint8) 
     self.previous_position = None 

答えて

0

2つのオプションがあります。まず最初に、等高線は既にPythonリストに入っているので、そのリストのインデックスを使ってそれらを列挙することができます。実際には、(i,c) in enumerate(countours)を使用して、ある意味ですでにそれを行っています。インデックスiを使用して、それぞれの輪郭に値「i」を空白の画像として「色付け」すると、画像を調べるだけで輪郭がわかります。もう1つ、おそらくもっと良いオプションのIMOは、バイナリイメージの輪郭ではなくバイナリイメージにラベルを付けるのにcv2.connectedComponents()を使用することです。また、事前ラベル付けすると、ブロブを閉じるためにmorphological operationsを試してみるかもしれません。

+0

それはあなたの質問に答えた場合に認められたとして、あなたは答えをマークすることができます。しかし、これらのアプローチのいずれかを試してみて、それらを実装するのに問題があるなら、私はそれを拡張することができます! –

+0

あなたの最初の答えが輪郭を見つけてラベル付けするために解決されましたが、ラベルは幅と変化に依存します(0は幅の最高値、上の画像、1は低い値です)。ありがとう。 –

0

編集:私はあなたの勧告に従うと、私は新たな問題を持っている:)

ラベルは永続的ではありません。両方の輪郭を動かすと、ラベルの数が変わります。 写真と自分のコードを追加してください。 A.Garrido @

Hand label 0 and circle 1

Hand label 1 and circle 0

while True: 

    cnt += 1 
    if (cnt % 10) == 0: 
     now = time.time() 
     dt = now - last 
     fps = 10/dt 
     fps_smooth = (fps_smooth * smoothing) + (fps * (1.0-smoothing)) 
     last = now 

    c = dev.color 

    cad = dev.cad 

    dev.wait_for_frames() 

    c = cv2.cvtColor(c, cv2.COLOR_RGB2GRAY) 
    depth = dev.depth * dev.depth_scale * 1000 

    print depth 

    #d = cv2.applyColorMap(depth.astype(np.uint8), cv2.COLORMAP_SUMMER) 



    print depth 


    res = np.float32(dev.depth) 

    depth = 255 * np.logical_and(depth >= 100, depth <= 500) 
    res2 = depth.astype(np.uint8) 

    kernel = np.ones((3, 3), np.uint8) 
    #res2 = cv2.blur(res2,(15,15)) 
    res2 = cv2.erode(res2, kernel, iterations=4) 
    res2 = cv2.dilate(res2, kernel, iterations=8) 

    im_floodfill = res2.copy() 

    h, w = res2.shape[:2] 
    mask2 = np.zeros((h + 2, w + 2), np.uint8) 

    cv2.floodFill(im_floodfill, mask2, (0, 0), 255) 

    im_floodfill_inv = cv2.bitwise_not(im_floodfill) 

    im_out = res2 | im_floodfill_inv 

    im_out = cv2.blur(im_out,(5,5)) 

    label = cv2.connectedComponentsWithStats(im_out) 

    n = label[0] - 1 
    cog = np.delete(label[3], 0, 0) 

    for i in xrange(n): 
     # im = cv2.circle(im,(int(cog[i][0]),int(cog[i][1])), 10, (0,0,255), -1) 
     cv2.putText(im_out, str(i), (int(cog[i][0]), int(cog[i][1])), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) 

    cv2.imshow("Depth", res) 

    cv2.imshow("OUT", im_out) 

    cv2.imshow("C", res2)