2017-09-12 6 views
-2

임 파이썬과 opencv 라이브러리에서 작동합니다.레이블이 지정된 고유 한 윤곽 (또는 독립 윤곽을 그립니다.)

카메라 캡처를 임계 값으로 설정하고 등고선 (두 개 이상)을 찾아 그릴 수 있습니다. 하지만 문제가 있습니다. 고유 한 ID 또는 태그로 그 윤곽을 식별하려고합니다. (예 : Id : 1, Id : 2)를 사용하여 추적합니다. 윤곽선에 영구 ID를 사용해야합니다.

목표는 선을 그어 두 개 이상의 등고선을 계산하고 때로는 둘 이상의 주변선이 큰 선으로 변환합니다.

참고 : 깊이 카메라로 사진을 찍었고 이미지에는 깊이가 있습니다.

코드 조각을 추가하십시오.

미리 감사드립니다.

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

두 가지 옵션이 있습니다. 우선, 등고선은 이미 파이썬 목록에 있으므로 목록의 색인을 사용하여 해당 목록을 열거 할 수 있습니다. 실제로 당신은 이미 (i,c) in enumerate(countours)으로 어떤 의미에서 그것을하고 있습니다. 색인 i을 사용하여 각 윤곽선에 빈 칸에 값 i 그림이있는 '색'을 지정하면 그림을 검토하여 어떤 윤곽선인지 알 수 있습니다. 다른 옵션 인 IMO는 cv2.connectedComponents()을 사용하여 이진 이미지의 윤곽 대신 이진 이미지에 라벨을 지정합니다. 또한 미리 레이블을 지정하면 morphological operations에 얼룩을 닫으십시오.

+0

Hand label 0 and circle 1

. 그러나 이러한 접근법 중 하나를 시도하고 구현하는 데 문제가 있다면 확장 할 수 있습니다! –

+0

첫 번째 해답은 등고선을 찾고 라벨을 붙이기 위해 해결되었지만 이제 레이블은 너비와 변경에 따라 달라집니다 (0은 너비의 최고 값, 상단 이미지, 1은 더 낮은 값임). 위치에 따라 프레임 단위로 변경됩니다. 감사. –

0

편집 : 나는 당신의 추천을 따라 나는 새로운 문제 :

레이블이 지속되지 않습니다 있습니다. 두 윤곽선을 모두 움직이면 레이블의 수가 바뀝니다. 사진과 코드를 추가하십시오. 그것은 당신의 질문에 대답하는 경우 받아, 당신은 답을 표시 할 수 있습니다 A.Garrido @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)