试图在激光指针,OpenCV上绘制圆圈

时间:2016-03-14 10:30:10

标签: python-2.7 opencv3.0 opencv-contour

所以我从@bradmontgomer的Github那里获取了代码并尝试修改它。代码首先将帧转换为HSV颜色空间,将视频帧拆分为颜色通道,然后在HSV组件上执行AND以识别激光。我无法找到检测到的激光点的轮廓。继承我的代码;

    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

如果len(cnts)>我在行中获得大于0的cnts" 0:",但无法看到激光指示器中绘制的圆圈。 this is the still frame of video, pointing the laser spot.

1 个答案:

答案 0 :(得分:0)

还有另一个显示激光帧的功能(display())(self.channel ['laser']),

    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