things.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:srcsim2017 作者: ZarjRobotics 项目源码 文件源码
def _find_array_button_thing(self):
        """ Find the array button on the solar array box """

        """ This uses color to determine if we have a choke """
        lower = np.array([0, 0, 60], dtype = "uint8")
        upper = np.array([20, 20, 255], dtype = "uint8")
        mask = cv2.inRange(self.img, lower, upper)

        blurred = cv2.GaussianBlur(mask, (5, 5), 0)
        thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]

        contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        contours = contours[0] if is_cv2() else contours[1]

        debug_img = None
        if self.debug:
            debug_img = self.img.copy()

        button_box = None
        for c in contours:
            box = cv2.boundingRect(c)

            if button_box is None:
                button_box = box
            else:
                button_box = self._union_box(deepcopy(button_box), box)

        if button_box is None:
            return

        top,bottom,left,right,center = self.find_dimensions(np.int0(np.array(self._bound_to_boxpoints(button_box))))
        if top is None or left is None or center is None:
            return None

        height = self.find_distance(top, bottom)
        width = self.find_distance(left, right)

        if self.debug:
            for c in contours:
                cv2.drawContours(debug_img, [c], -1, (0, 255, 0), 2)

            cv2.circle(debug_img, top, 5, (255, 255, 0))
            cv2.circle(debug_img, bottom, 5, (255, 255, 0))
            cv2.circle(debug_img, left, 5, (255, 255, 0))
            cv2.circle(debug_img, right, 5, (255, 255, 0))
            cv2.rectangle(debug_img, (button_box[0], button_box[1]),
                    (button_box[0] + button_box[2], button_box[1] + button_box[3]), (128, 0, 128), 2)
            #cv2.circle(debug_img, center, 5, (255, 255, 0))

            cv2.imshow("button picture", debug_img)
            cv2.setMouseCallback("button picture", self.handle_mouse)
            cv2.waitKey(0)
            cv2.destroyAllWindows()

        self.array_button = Thing(height, width, center, None)
        self.array_button.set_array_button()
        self.array_button.computed_center = self.compute_center(left, right, top, bottom)
        self.things.append(self.array_button)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号