diff --git a/data_sub/test_4/data_sub.json b/data_sub/test_4/data_sub.json new file mode 100644 index 0000000..ce79eb3 --- /dev/null +++ b/data_sub/test_4/data_sub.json @@ -0,0 +1,162 @@ +[ + { + "label": 0, + "prob": 0.895011305809021, + "x": 536, + "y": 127, + "width": 49, + "height": 53 + }, + { + "label": 0, + "prob": 0.8946242332458496, + "x": 547, + "y": 224, + "width": 70, + "height": 37 + }, + { + "label": 0, + "prob": 0.8866094946861267, + "x": 198, + "y": 216, + "width": 67, + "height": 63 + }, + { + "label": 0, + "prob": 0.8786224126815796, + "x": 435, + "y": 222, + "width": 63, + "height": 66 + }, + { + "label": 0, + "prob": 0.873380720615387, + "x": 83, + "y": 115, + "width": 54, + "height": 53 + }, + { + "label": 0, + "prob": 0.8712457418441772, + "x": 597, + "y": 117, + "width": 36, + "height": 39 + }, + { + "label": 0, + "prob": 0.8656917810440063, + "x": 583, + "y": 268, + "width": 37, + "height": 41 + }, + { + "label": 0, + "prob": 0.718810498714447, + "x": 526, + "y": 316, + "width": 112, + "height": 39 + }, + { + "label": 0, + "prob": 0.6975097060203552, + "x": 621, + "y": 185, + "width": 17, + "height": 32 + }, + { + "label": 0, + "prob": 0.6963123083114624, + "x": 314, + "y": 119, + "width": 53, + "height": 55 + }, + { + "label": 0, + "prob": 0.5993771553039551, + "x": 286, + "y": 319, + "width": 194, + "height": 40 + }, + { + "label": 0, + "prob": 0.2692039906978607, + "x": 386, + "y": 101, + "width": 46, + "height": 48 + }, + { + "label": 0, + "prob": 0.21311397850513458, + "x": 321, + "y": 221, + "width": 86, + "height": 40 + }, + { + "label": 0, + "prob": 0.1731298416852951, + "x": 531, + "y": 121, + "width": 98, + "height": 47 + }, + { + "label": 0, + "prob": 0.1717808097600937, + "x": 69, + "y": 310, + "width": 172, + "height": 37 + }, + { + "label": 0, + "prob": 0.14592395722866058, + "x": 87, + "y": 217, + "width": 81, + "height": 35 + }, + { + "label": 0, + "prob": 0.14323937892913818, + "x": 179, + "y": 171, + "width": 38, + "height": 38 + }, + { + "label": 0, + "prob": 0.10391232371330261, + "x": 156, + "y": 94, + "width": 46, + "height": 47 + }, + { + "label": 0, + "prob": 0.08634057641029358, + "x": 414, + "y": 176, + "width": 39, + "height": 37 + }, + { + "label": 0, + "prob": 0.0807800367474556, + "x": 361, + "y": 269, + "width": 51, + "height": 42 + } +] \ No newline at end of file diff --git a/data_sub/test_4/wide_image.png b/data_sub/test_4/wide_image.png new file mode 100644 index 0000000..1f2d907 Binary files /dev/null and b/data_sub/test_4/wide_image.png differ diff --git a/search.py b/search.py index 9f7241a..f174b50 100644 --- a/search.py +++ b/search.py @@ -96,7 +96,8 @@ def calculate_second_point(x1, y1, r, theta_radians): def cal_c1c2c3c4(param, heigt): ''' - 按照上左、上右、下右、下左的顺时针顺序 + 按照上左、上右、下右、下左的顺时针顺序. + 返回的数据是转换成了以左上角为原点的坐标系下的坐标点。 ''' param['x1'] = int(param['x'] - param['w'] / 2) param['y1'] = heigt - int(param['center'] + param['h'] / 2) @@ -138,7 +139,9 @@ def gen_im_from_params(params, type="lines"): padding_value = 0 # 内边距,避免整个bim图片贴着边缘展示 bim_width = int(max_x) + padding_value + print(f"[bim_width] ====== [{bim_width}]") bim_height = int(max_y) + padding_value + print(f"[bim_height] ====== [{bim_height}]") bim_channels = 3 im = np.zeros((bim_height, bim_width, bim_channels), dtype=np.uint8) @@ -335,11 +338,11 @@ if __name__ == "__main__": # _im_edge_sobel = _sobel(sub_zero) # _, _im_thresh = cv2.threshold(_im_edge_sobel, 5, 255, cv2.THRESH_BINARY) # cnts = _findContours(_im_thresh) - original_rectangle = read_from_json("data_sub/test_3/data_sub.json") + original_rectangle = read_from_json("data_sub/test_1/data_sub.json") # 过滤矩形 # cnts 过滤之后的矩形 # sub_im 裁剪之后的图像 - cnts,sub_im = filter_rectangle("data_sub/test_3/wide_image.png", original_rectangle) + cnts,sub_im = filter_rectangle("data_sub/test_1/wide_image.png", original_rectangle) sub_zero = np.zeros_like(sub_im) for contour in cnts: # x, y, w, h = cv2.boundingRect(contour) @@ -359,7 +362,7 @@ if __name__ == "__main__": param["x1"] = x param["y1"] = y param["x2"] = x + w - param["y2"] = y + param["y2"] = y param["x3"] = x + w param["y3"] = y + h param["x4"] = x @@ -367,7 +370,7 @@ if __name__ == "__main__": param['x_center'] = int((param['x1'] + param['x2']) / 2) param['y_center'] = int((param['y1'] + param['y3']) / 2) param["w"] = param["x2"] - param["x1"] - param["h"] = param["y3"] - param["y1"] + param["h"] = param["y3"] - param["y1"] param["x"] = int((param["x1"] + param["x2"]) / 2) param['center'] = sub_im.shape[0] - int((param["y1"] + param["y3"]) / 2) data_sub["params"].append(param) @@ -478,8 +481,10 @@ if __name__ == "__main__": # 相对于bim图的坐标 tmp_roi_start_x = int(param['x1'] - scale * polar_origin_x) tmp_roi_end_x = tmp_roi_start_x + tmp_roi_width + tmp_roi_start_y = int(param['y1'] - scale * polar_origin_y) tmp_roi_end_y = tmp_roi_start_y + tmp_roi_height + tmp_roi_conners = [[tmp_roi_start_x, tmp_roi_start_y], [tmp_roi_end_x, tmp_roi_start_y], [tmp_roi_end_x, tmp_roi_end_y], @@ -505,19 +510,19 @@ if __name__ == "__main__": # if tmp_count / count > 1.3 or tmp_count / count < 0.77: continue if abs(tmp_count - count) == 0: score = 0.5 - elif abs(tmp_count - count) <= 10: score = 0.3 - elif abs(tmp_count - count) <= 20: score = 0.2 - elif abs(tmp_count - count) <= 30: score = 0.1 + elif abs(tmp_count - count) <= 10: score = 0.4 + elif abs(tmp_count - count) <= 20: score = 0.3 + elif abs(tmp_count - count) <= 30: score = 0.2 else: score = 0.0 # else: score = (1 / abs(tmp_count - count) ) * 0.7 score += (1 - abs(tmp_sum_r - sum_r) / sub_roi_width) * 0.25 - score += (1 - abs(tmp_sum_theta - sum_theta) / 3.14) * 0.25 + score += (1 - abs(tmp_sum_theta - sum_theta) / 3.14) * 0.35 print("score=======", str(score)) if score > 0.6: #???? - cartesian_points1 = polar_to_cartesian(np.asarray(tmp_polar_list)) - cartesian_points2 = polar_to_cartesian(np.asarray(polar_list)) + cartesian_points1 = polar_to_cartesian(np.asarray(tmp_polar_list)) # bim上的坐标 + cartesian_points2 = polar_to_cartesian(np.asarray(polar_list)) # sub上的坐标 sc1 = compute_shape_context(cartesian_points1) sc2 = compute_shape_context(cartesian_points2) match_score = match_shapes(sc1, sc2) @@ -534,28 +539,43 @@ if __name__ == "__main__": param['match_score'] = match_score if min_match_score > match_score: min_match_score = match_score + print(f"[start_point] ====== [{param["start_point"]}]") + print(f"[end_point] ====== [{param["end_point"]}]") rst_params.append(param) - # 预埋件匹配 + + max_score = -99999 + max_index = -1 for i, param in enumerate(rst_params): score = param["score"] match_score = param["match_score"] - print(f"[match_score] ====== [{match_score}]") - - id = param["code"] - if min_match_score == match_score or match_score < 0.5: #???? - # if True: - index_list = [] - for j in range(len(param['effective_points'])): - pt = param['effective_points'][j] - if pt[2] not in index_list: - index_list.append(param['effective_points'][j][2]) - # for i, param in enumerate(_sub_sort_params): - # param[] - print(f"起始预埋件ID:{id},置信度为:{score[0]},点数量:{score[1]},平均长度为:{score[2]},平均角度为:{score[3]}") - print(f"match_sscore为:{match_score}") - bim_im = cv2.rectangle(bim_im, param["start_point"], \ - param["end_point"], 100 * (i+1), 50) + if abs(score[0] / match_score) > max_score: + max_score = abs(score[0] / match_score) + max_index = i + bim_im = cv2.rectangle(bim_im,rst_params[max_index]["start_point"], rst_params[max_index]["end_point"], 100 * (i + 1), 50) + # # 预埋件匹配 + # for i, param in enumerate(rst_params): + # score = param["score"] + # match_score = param["match_score"] + # print(f"[match_score] ====== [{match_score}]") + # + # id = param["code"] + # + # if min_match_score == match_score or match_score < 5.0: #???? + # # if True: + # index_list = [] + # for j in range(len(param['effective_points'])): + # pt = param['effective_points'][j] + # if pt[2] not in index_list: + # index_list.append(param['effective_points'][j][2]) + # # for i, param in enumerate(_sub_sort_params): + # # param[] + # print(f"起始预埋件ID:{id},置信度为:{score[0]},点数量:{score[1]},平均长度为:{score[2]},平均角度为:{score[3]}") + # print(f"match_sscore为:{match_score}") + # print(f"[start_point] ====== [{param["start_point"]}]") + # print(f"[end_point] ====== [{param["end_point"]}]") + # + # bim_im = cv2.rectangle(bim_im, param["start_point"], param["end_point"], 100 * (i + 1), 50) elapsed_time = time.time() - start_time print(f"Execution time: {elapsed_time:.4f} seconds") diff --git a/utils.py b/utils.py index 9b82d3a..106ebce 100644 --- a/utils.py +++ b/utils.py @@ -27,8 +27,8 @@ def filter_rectangle(image_path, points): max_height_rate = 0.5 # 矩形高度占整个画面高度的最大比例,如果超过该比例,则认为是无效矩形 # 裁剪参数 - left_x_cut_rate=0.15 # 左边界的裁剪比例,从左边开始裁剪百分之多少 - right_x_cut_rate=0.15 # 右边界的裁剪比例,从右边开始裁剪百分之多少 + left_x_cut_rate=0.1 # 左边界的裁剪比例,从左边开始裁剪百分之多少 + right_x_cut_rate=0.1# 右边界的裁剪比例,从右边开始裁剪百分之多少 image = cv2.imread(image_path) image_height = image.shape[0] # 获取图片高度 image_width = image.shape[1] # 获取图片宽度