Пример #1
0
 def find_template(self, im_source, im_search, threshold=None):
     """
     模板匹配, 返回匹配度最高的坐标
     :param im_source: 待匹配图像
     :param im_search: 待匹配模板
     :param threshold: 匹配度
     :return:  None or Rect
     """
     start = time.time()
     im_source, im_search = self.check_detection_input(im_source, im_search)
     # 模板匹配取得res矩阵
     res = self._get_template_result_matrix(im_source, im_search)
     # 找到最佳匹配项
     min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
     h, w = im_search.shape[:2]
     # 求可信度
     img_crop = im_search.adjustROI(max_loc[1], max_loc[1] + h, max_loc[0],
                                    max_loc[0] + w)
     confidence = self.cal_rgb_confidence(img_crop, im_search)
     # confidence = self.test_cal_rgb_confidence(img_crop, im_search)
     # 如果可信度小于threshold,则返回None
     if confidence < (threshold or self.threshold):
         return None
     # 求取位置
     x, y = max_loc
     rect = Rect(x=x, y=y, width=w, height=h)
     print('[tpl]{Rect}, confidence={confidence}, time={time:.2f}'.format(
         confidence=confidence,
         Rect=rect,
         time=(time.time() - start) * 1000))
     return generate_result(rect, confidence)
Пример #2
0
def find_template(im_source,
                  im_search,
                  threshold: float = 0.85,
                  mode=cv2.TM_CCOEFF_NORMED):
    """
    模板匹配
    :param im_source: 待匹配图像
    :param im_search: 待匹配模板
    :param threshold: 匹配度
    :param mode: 识别模式
    :return: None or Rect
    """
    start = time.time()
    im_source, im_search = check_detection_input(im_source, im_search)
    # 模板匹配取得res矩阵
    res = _get_template_result_matrix(im_source, im_search)
    # 找到最佳匹配项
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    h, w = im_search.shape[:2]
    # 求可信度
    img_crop = im_source[max_loc[1]:max_loc[1] + h, max_loc[0]:max_loc[0] + w]
    confidence = cal_rgb_confidence(img_crop, im_search)
    # 如果可信度小于threshold,则返回None
    if confidence < threshold:
        return None
    # 求取位置
    x, y = max_loc
    rect = Rect(x=x, y=y, width=w, height=h)
    print('[tpl]{Rect}, confidence={confidence}, time={time:.2f}'.format(
        confidence=confidence, Rect=rect, time=(time.time() - start) * 1000))
    return generate_result(rect, confidence)
Пример #3
0
def find_templates(im_source,
                   im_search,
                   threshold: float = 0.85,
                   max_count=10):
    """
    模板匹配
    :param im_source: 待匹配图像
    :param im_search: 待匹配模板
    :param threshold: 匹配度
    :param mode: 识别模式
    :param max_count: 最多匹配数量
    :return: None or Rect
    """
    start = time.time()
    im_source, im_search = check_detection_input(im_source, im_search)
    # 模板匹配取得res矩阵
    res = _get_template_result_matrix(im_source, im_search)
    result = []
    h, w = im_search.shape[:2]

    while True:
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        img_crop = im_source[max_loc[1]:max_loc[1] + h,
                             max_loc[0]:max_loc[0] + w]
        confidence = cal_rgb_confidence(img_crop, im_search)
        if confidence < threshold or len(result) > max_count:
            break
        x, y = max_loc
        rect = Rect(x=x, y=y, width=w, height=h)
        result.append(generate_result(rect, confidence))
        # 屏蔽最优结
        cv2.rectangle(res, (int(max_loc[0] - w / 2), int(max_loc[1] - h / 2)),
                      (int(max_loc[0] + w / 2), int(max_loc[1] + h / 2)),
                      (0, 0, 0), -1)
    if result:
        print('[tpls] find counts:{counts}, time={time:.2f}ms{result}'.format(
            counts=len(result),
            time=(time.time() - start) * 1000,
            result=''.join([
                '\n\t{}, confidence={}'.format(x['rect'], x['confidence'])
                for x in result
            ])))
    return result if result else None
Пример #4
0
 def find_sift(self, im_source, im_search, threshold: int = 0.8):
     """基于FlannBasedMatcher的SIFT实现"""
     start_time = time.time()
     im_source, im_search = check_detection_input(im_source, im_search)
     # 第一步: 获取特征点集并匹配出特征点对 (最耗时的一段)
     kp_sch, kp_src, good = self.get_key_points(im_search=im_search,
                                                im_source=im_source)
     # 第二步:根据匹配点对(good),提取出来识别区域:
     rect = self.extract_good_points(im_source, im_search, kp_sch, kp_src,
                                     good, threshold)
     if not rect:
         return None
     # 第三步,通过识别矩阵周围+-1像素的矩阵,求出结果可信度,并且返回最大精准度的范围
     confidences = []
     similar_rect = create_similar_rect(rect.x, rect.y, rect.width,
                                        rect.height)
     pprint(similar_rect)
     h, w = im_search.shape[:2]
     for i in similar_rect:
         # cv2.matchTemplate 目标和模板长宽不能一大一小
         target_img = im_source[i.tl.y:i.br.y, i.tl.x:i.br.x]
         print(i)
         target_img = cv2.resize(target_img, (w, h))
         confidences.append(
             self._cal_sift_confidence(resize_img=target_img,
                                       im_search=im_search))
     # 提取最大值
     if confidences:
         confidence = max(confidences)
         rect = similar_rect[confidences.index(confidence)]
     else:
         confidence = 0
     best_match = generate_result(rect=rect, confi=confidence)
     print(
         '[sift]:{Rect}, confidence=(max={max_confidence:.5f},min={min_confidence:.5f}), time={time:.1f}ms'
         .format(max_confidence=max(confidences),
                 min_confidence=min(confidences),
                 Rect=rect,
                 time=(time.time() - start_time) * 1000))
     return best_match if confidence > threshold else None