2016-03-14 5 views
0

Так что я взял код из Github @bradmontgomer и попытался его изменить. Код сначала преобразует кадр в цветовое пространство HSV, разбивает видеокадр на цветовые каналы и затем выполняет И-компонент HSV для идентификации лазера. У меня возникают проблемы с поиском контуров обнаруженной лазерной точки. мой код;Пытается нарисовать круг на лазерной указке, OpenCV

def threshold_image(self, channel): 
    if channel == "hue": 
     minimum = self.hue_min 
     maximum = self.hue_max 
    elif channel == "saturation": 
     minimum = self.sat_min 
     maximum = self.sat_max 
    elif channel == "value": 
     minimum = self.val_min 
     maximum = self.val_max 

    (t, tmp) = cv2.threshold(
     self.channels[channel], # src 
     maximum, # threshold value 
     0, # we dont care because of the selected type 
     cv2.THRESH_TOZERO_INV #t type 
    ) 

    (t, self.channels[channel]) = cv2.threshold(
     tmp, # src 
     minimum, # threshold value 
     255, # maxvalue 
     cv2.THRESH_BINARY # type 
    ) 

    if channel == 'hue': 
     # only works for filtering red color because the range for the hue is split 
     self.channels['hue'] = cv2.bitwise_not(self.channels['hue']) 


def detect(self, frame): 
    # resize the frame, blur it, and convert it to the HSV 
    # color space 
    frame = imutils.resize(frame, width=600) 

    hsv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 


    # split the video frame into color channels 
    h, s, v = cv2.split(hsv_img) 
    self.channels['hue'] = h 
    self.channels['saturation'] = s 
    self.channels['value'] = v 

    # Threshold ranges of HSV components; storing the results in place 
    self.threshold_image("hue") 
    self.threshold_image("saturation") 
    self.threshold_image("value") 

    # Perform an AND on HSV components to identify the laser! 
    self.channels['laser'] = cv2.bitwise_and(
     self.channels['hue'], 
     self.channels['value'] 
    ) 
    self.channels['laser'] = cv2.bitwise_and(
     self.channels['saturation'], 
     self.channels['laser'] 
    ) 

    # Merge the HSV components back together. 
    hsv_image = cv2.merge([ 
     self.channels['hue'], 
     self.channels['saturation'], 
     self.channels['value'], 
    ]) 

    thresh = cv2.threshold(self.channels['laser'], 25, 255, cv2.THRESH_BINARY)[1] 

     #find contours in the mask and initialize the current 
     #(x, y) center of the ball 
    #cnts = cv2.findContours(self.channels['laser'].copy(), cv2.RETR_EXTERNAL, 
    #cv2.CHAIN_APPROX_SIMPLE) 
    (_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, 
    cv2.CHAIN_APPROX_SIMPLE) 

    center = None   

     # only proceed if at least one contour was found 
    if len(cnts) > 0: 
      # find the largest contour in the mask, then use 
      # it to compute the minimum enclosing circle and 
      # centroid 
      c = max(cnts, key=cv2.contourArea) 
      ((x, y), radius) = cv2.minEnclosingCircle(c) 
      M = cv2.moments(c) 
      center = (int(M["m10"]/M["m00"]), int(M["m01"]/M["m00"])) 

      # only proceed if the radius meets a minimum size 
      if radius > 10: 
       # draw the circle and centroid on the frame, 
       # then update the list of tracked points 
       cv2.circle(frame, (int(x), int(y)), int(radius), 
        (0, 255, 255), 2) 
       cv2.circle(frame, center, 5, (0, 0, 255), -1)    
    cv2.imshow('LaserPointer', self.channels['laser']) 
    ################################################ 
    return hsv_image 

Я получаю УНТ больше чем 0 в строке «если LEN (УНТ)> 0:», но не может видеть круг, нарисованный в лазерной указкой. this is the still frame of video, pointing the laser spot.

ответ

0

Была другую функция (дисплей()), который был отображением лазерного кадра (self.channel [ 'лазерного']),

def display(self, img, frame): 
     """Display the combined image and (optionally) all other image channels 
     NOTE: default color space in OpenCV is BGR. 
     """ 
     cv2.imshow('RGB_VideoFrame', frame) 
     cv2.imshow('LaserPointer', self.channels['laser']) 

Я закомментирована эти линии cv2.iamshow из этой функции, а затем Я смог увидеть круг вокруг лазерной указки. Это потому, что теперь кадр из строки cv2.iamshow внутри функции «detect (self, frame):» был выполнен. Затем я применил дальнейшие кодировки указателя, чтобы обнаружить его местоположение. enter image description here

Смежные вопросы