markerdetector.py 文件源码

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

项目:BAR4Py 作者: bxtkezhan 项目源码 文件源码
def recognize(self, points, frame, dictionary=None, limit=0.80, side_length=28, batch_size=3):
        '''
        Inputs:
        points is marker.points param
        frame is image frame
        dictionary is Dictionary object
        ...

        Outputs:
        marker_id, rotations

        >>> marker_i, rotations = detector.recognize(points, frame, dictionary=dictionary)
        '''
        dictionary = dictionary or self.dictionary
        if dictionary is None: raise TypeError('recognize nead dictionary')

        # To Gray
        gray = frame
        if len(gray.shape) == 3: gray = bgr2gray(frame)

        # Convert the points, gray to local_points, local_gray
        rect = self.localRect(points)
        gray = self.localFrame(rect, gray)
        points = self.localCorners(rect, points)

        # Define src_points and dst_points, src: 0,1,2,3 -> dst: 1,0,3,2
        points_src = np.float32(points)
        points_dst = np.float32([[0,side_length],[0,0], 
                                 [side_length,0],[side_length,side_length]])


        # Calc transform matrix and perspective dst map
        M = cv2.getPerspectiveTransform(points_src, points_dst)
        dst = cv2.warpPerspective(gray, M, (side_length, side_length))

        # Begin recognize
        _, dst = cv2.threshold(dst, dst.mean(), 1, cv2.THRESH_OTSU)
        # Probables
        probables = []
        marker_dict = dictionary.getDict()
        # for marker_id, hash_map in dictionary.getDict():
        for marker_id in marker_dict:
            hash_map = marker_dict[marker_id]
            if dst.shape != hash_map.shape: hash_map = cv2.resize(hash_map, dst.shape[::-1])
            deviation = rotations = 0
            for i in range(4):
                now_deviation = np.sum((dst == hash_map).astype(int)) / (side_length**2)
                if now_deviation > deviation: deviation, rotations = now_deviation, i
                hash_map = np.rot90(hash_map)
            if deviation > limit:
                probables.append((deviation, marker_id, rotations))
                if len(probables) > batch_size: break
        # Best of marker_id and rotations
        if len(probables) > 0:
            return max(probables, key=lambda item:item[0])[1:]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号