diff --git a/search.py b/search.py index 9fcffdb..4df5ea2 100644 --- a/search.py +++ b/search.py @@ -150,11 +150,8 @@ def gen_im_from_params(params, type="lines"): max_y = param["center"] + param["h"] / 2 max_y_idx = i - padding_value = 1000 # 内边距,避免整个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_width = int(max_x) + bim_height = int(max_y) bim_channels = 3 im = np.zeros((bim_height, bim_width, bim_channels), dtype=np.uint8) @@ -336,14 +333,16 @@ def _findContours(image): contours, _ = cv2.findContours(image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) return sorted(contours, key=cv2.contourArea, reverse=True) -def bim_compare_to_hd_roi(hd_roi_list,hd_img_width,hd_img_height,hd_cam_w_bim,hd_cam_h_bim): + +def bim_compare_to_hd_roi(hd_roi_list, hd_img_width, hd_img_height, bim_im_height, + bim_sub_area, bim_rect_list): """ 预埋件号码匹配 将高清摄像头照片的ROI和实际BIM图中的号码匹配上 参数: - hd_roi_list : 高清摄像头的ROI列表,每个ROI表示一个可能的预埋件区域 + hd_roi_list : 高清摄像头的ROI数据列表,每个ROI表示一个可能的预埋件区域 1.每个第二层子数组中的坐标顺序为左上、右上、右下、左下(顺时针) 2.是以左上角为原点的常规的图片坐标系。 格式如下。 @@ -353,20 +352,95 @@ def bim_compare_to_hd_roi(hd_roi_list,hd_img_width,hd_img_height,hd_cam_w_bim,hd ] hd_img_width (int): 高清相机拍摄图片原始宽度 hd_img_height (int): 高清相机拍摄图片原始高度 - hd_cam_w_bim (int): 高清相机矩形示意框在bim图中的宽度 - hd_cam_h_bim (int): 高清相机矩形示意框在bim图中的高度 + bim_im_height(int): bim图的高度 + bim_sub_area : 包含高清,广角,等坐标矩形区域数据 + bim_rect_list : bim的原始数据,坐标系上以左下角为原点的数学坐标系。 + 返回: 数组,数组内包含多个字典,每个字典有如下元素。 + [ + { + "bim_rect": bim_rect_item, + "hd_roi": hd_roi + } + ] + """ + # 高清相机坐标转换为bim图坐标,宽高肯定都是等比的直接按照bim上面高清矩形的宽度,算下比例。 + scale_w = bim_sub_area["hd_cam_w_bim"] / hd_img_width + scale_h = bim_sub_area["hd_cam_h_bim"] / hd_img_height + hd_roi_out_of_range_index = [] # 超出边界的ROI的索引 + for hd_roi_index, hd_roi in enumerate(hd_roi_list): + if hd_roi_index == 5: + print("hd_roi_index === 5") + for i in range(4): + # 所有高清照片的ROI先按照这个比例转换 ,并且加上坐标偏移,转换为bim上实际坐标 + hd_roi[i][0] = int(hd_roi[i][0] * scale_w) + bim_sub_area["hd_cam_x_bim"] + hd_roi[i][1] = int(hd_roi[i][1] * scale_h) + bim_sub_area["hd_cam_y_bim"] + # 如果ROI坐标形成的矩形,横跨bim图上高清矩形范围边界框,就剔除这个矩形 + is_x_cross_left_border_line = hd_roi[0][0] < bim_sub_area["hd_cam_x_bim"] <= hd_roi[1][0] + is_x_cross_right_border_line = hd_roi[0][0] < bim_sub_area["hd_cam_x_bim"] + bim_sub_area["hd_cam_w_bim"] <= \ + hd_roi[1][0] + is_y_cross_top_border_line = hd_roi[0][1] < bim_sub_area["hd_cam_y_bim"] <= hd_roi[2][1] + is_y_cross_bottom_border_line = hd_roi[0][1] < bim_sub_area["hd_cam_y_bim"] + bim_sub_area["hd_cam_h_bim"] <= \ + hd_roi[2][1] + if is_x_cross_left_border_line or is_x_cross_right_border_line or is_y_cross_top_border_line or is_y_cross_bottom_border_line: + hd_roi_out_of_range_index.append(hd_roi_index) + # 画出来试试 + # bim_im = cv2.rectangle(bim_im, (hd_roi[0][0], hd_roi[0][1]), (hd_roi[2][0], hd_roi[2][1]), (0, 0, 255), 30) + # 写上i编号 + # cv2.putText(bim_im, str(hd_roi_index), (hd_roi[0][0], hd_roi[0][1]), cv2.FONT_HERSHEY_SIMPLEX, 5, (0, 0, 255),20) + # print(f"超出边界的ROI的索引是:{hd_roi_out_of_range_index}") - 返回: + # bim所有的矩形,注意bim原数据是以左下角为原点的坐标系 !!! + # bim所有的矩形,注意bim原数据是以左下角为原点的坐标系 !!! + # bim所有的矩形,注意bim原数据是以左下角为原点的坐标系 !!! + # 遍历bim所有的矩形,找到与高清相机ROI匹配的矩形 + bim_rect_hit_list = [] + for bim_rect_index, bim_rect_item in enumerate(bim_rect_list): + # x不需要转换 + bim_rect_item_center_x = bim_rect_item["x"] + # 把y坐标系转换成左上角坐标系 + bim_rect_item_center_y = bim_im_height - bim_rect_item["center"] + # 逐一和高清相机ROI坐标进行匹配 + for hd_roi_index, hd_roi in enumerate(hd_roi_list): + if(hd_roi_index in hd_roi_out_of_range_index): + continue + # 如果中心点的坐标在某个高清相机ROI内,就认为匹配上了。 + bim_rect_item_center_x_inside = hd_roi[0][0] < bim_rect_item_center_x < hd_roi[1][0] + bim_rect_item_center_y_inside = hd_roi[0][1] < bim_rect_item_center_y < hd_roi[2][1] + if bim_rect_item_center_x_inside and bim_rect_item_center_y_inside: + bim_rect_hit_list.append({ + "bim_rect": bim_rect_item, + "hd_roi": hd_roi + }) + return bim_rect_hit_list + # 画出bim_rect_hit_list + # for bim_rect_hit_item in bim_rect_hit_list: + # bim_rect = bim_rect_hit_item["bim_rect"] + # hd_roi = bim_rect_hit_item["hd_roi"] + # bim_im = cv2.rectangle(bim_im, (hd_roi[0][0], hd_roi[0][1]), (hd_roi[2][0], hd_roi[2][1]), (0, 0, 255), 30) + # # 写上i编号 + # cv2.putText(bim_im, str(bim_rect["code"]), (hd_roi[0][0]+100, hd_roi[0][1]+200), cv2.FONT_HERSHEY_SIMPLEX, 5, (0, 0, 255), + # 20) + +def search(data_bim_json, data_sub_json, wide_cam_img_width, wide_cam_img_height): + """ + 根据广角相机的照片定位到bim图上面对应的位置 + + 参数: + data_bim_json :bim的json数据,左下角为原点的数学坐标系 + data_sub_json: 广角识别之后的ROI数据,左上角为原点的图片坐标系 + wide_cam_img_width:原始广角图片的宽度 + wide_cam_img_height:原始广角图片的高度 + + 返回: + 找到了:返回包含一堆坐标数据的字典 + 找不到:返回None """ - - -if __name__ == "__main__": # 读取并处理数据 data_bim = {} data_bim["type"] = 0 - data_bim["params"] = read_from_json("data_bim.json") + data_bim["params"] = data_bim_json data_bim["point"] = [] data_bim["params"] = parmas_to_num(data_bim["params"]) @@ -394,13 +468,13 @@ 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_1/data_sub.json") # 过滤矩形 # cnts 过滤之后的矩形 # sub_im 裁剪之后的图像 - cnts, sub_im = filter_rectangle("data_sub/test_1/wide_image.png", original_rectangle, wide_cam_left_cut_rate, - wide_cam_right_cut_rate) - sub_zero = np.zeros_like(sub_im) + cnts, wide_cam_img_width_after_cut = filter_rectangle(wide_cam_img_width, wide_cam_img_height, data_sub_json, + wide_cam_left_cut_rate, + wide_cam_right_cut_rate) + # sub_zero = np.zeros_like(sub_im) for contour in cnts: # x, y, w, h = cv2.boundingRect(contour) x = contour["x"] @@ -429,15 +503,15 @@ if __name__ == "__main__": param["w"] = param["x2"] - param["x1"] 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) + param['center'] = wide_cam_img_height - int((param["y1"] + param["y3"]) / 2) data_sub["params"].append(param) - cv2.rectangle(sub_zero, (x, y), (x + w, y + h), (0, 255, 0), 1) + # cv2.rectangle(sub_zero, (x, y), (x + w, y + h), (0, 255, 0), 1) # bim_im = gen_im_from_params(data_bim["params"]) # cv2.namedWindow("bim", cv2.WINDOW_NORMAL) # cv2.imshow("bim", bim_im) # cv2.imwrite("bim_im.png", bim_im) # cv2.waitKey(0) - cv2.imshow("sub_zero", sub_zero) + # cv2.imshow("sub_zero", sub_zero) # cv2.waitKey(0) # data_sub = {} @@ -457,8 +531,8 @@ if __name__ == "__main__": # sub_roi_height = int(max_y) #???? # sub_roi_width = int(max_x) #???? - sub_roi_height = sub_im.shape[0] - sub_roi_width = sub_im.shape[1] + sub_roi_height = wide_cam_img_height # 广角图片的高度没有裁剪 + sub_roi_width = wide_cam_img_width_after_cut # 广角图片的宽度裁剪过后变化了 sub_roi_params_select_id = -1 sub_roi_w_base_len = 0 # 选中预埋件的宽度作为基础长度 sub_roi_divide_w_h = 1 @@ -497,13 +571,13 @@ if __name__ == "__main__": # cv2.waitKey(0) pts = gen_points_from_params(_sub_sort_params, sub_roi_width, sub_roi_height) # # 测试,画出所有的pts - for i, p in enumerate(pts): - # 画点 - cv2.circle(sub_im, (p[0], p[1]), 2, (0, 255, 255), -1) - # 写编号 - cv2.putText(sub_im, str(i), (p[0], p[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) - cv2.rectangle(sub_im, (hd_cam_x, hd_cam_y), (hd_cam_x + hd_cam_w, hd_cam_y + hd_cam_h), (0, 255, 0), 4) - cv2.imshow("sub_im_points", sub_im) + # for i, p in enumerate(pts): + # # 画点 + # cv2.circle(sub_im, (p[0], p[1]), 2, (0, 255, 255), -1) + # # 写编号 + # cv2.putText(sub_im, str(i), (p[0], p[1]), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) + # cv2.rectangle(sub_im, (hd_cam_x, hd_cam_y), (hd_cam_x + hd_cam_w, hd_cam_y + hd_cam_h), (0, 255, 0), 4) + # cv2.imshow("sub_im_points", sub_im) # cv2.waitKey(0) polar_list = [] for i, pt in enumerate(pts): @@ -514,9 +588,6 @@ if __name__ == "__main__": polar_list.append([r, theta]) sum_r /= count * sub_roi_w_base_len sum_theta /= count - print(f"[所有点到该预埋件左上点的个数] ====== [{count}]") - print(f"[所有点到该预埋件左上点的平均极半径] ====== [{sum_r}]") - print(f"[所有点到该预埋件左上点的平均极角] ====== [{sum_theta}]") # 初始化候选预埋件 candi_params = [] @@ -528,7 +599,7 @@ if __name__ == "__main__": rst_params = [] bim_all_pts = gen_points_from_params(data_bim["params"]) - bim_im = gen_im_from_params(data_bim["params"]) + # bim_im = gen_im_from_params(data_bim["params"]) # sub_im = gen_im_from_params(data_sub["params"])# 需要读取 min_match_score = 999999 for i, param in enumerate(candi_params): @@ -536,7 +607,7 @@ if __name__ == "__main__": scale = tmp_roi_w_base_len / sub_roi_w_base_len tmp_roi_width = int(scale * sub_roi_width) tmp_roi_height = int(scale * sub_roi_height) - # 相对于bim图的坐标 + # 计算广角矩形相对于bim图的坐标 tmp_roi_start_x = int(param['x1'] - scale * polar_origin_x) tmp_roi_end_x = tmp_roi_start_x + tmp_roi_width @@ -544,8 +615,8 @@ if __name__ == "__main__": tmp_roi_end_y = tmp_roi_start_y + tmp_roi_height # 计算高清矩形,在bim上面的坐标。相当于被scale一起放大了 - hd_cam_x_bim = int(scale * hd_cam_x + tmp_roi_start_x) - hd_cam_y_bim = int(scale * hd_cam_y + tmp_roi_start_y) + hd_cam_x_bim = int(scale * hd_cam_x + tmp_roi_start_x) # 左上角x + hd_cam_y_bim = int(scale * hd_cam_y + tmp_roi_start_y) # 左上角y hd_cam_w_bim = int(scale * hd_cam_w) hd_cam_h_bim = int(scale * hd_cam_h) @@ -589,7 +660,7 @@ if __name__ == "__main__": score += (1 - abs(tmp_sum_theta - sum_theta) / 3.14) * 0.35 print("score=======", str(score)) - if score > 0.6: #???? + if score > 0.6: # ???? 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) @@ -597,11 +668,7 @@ if __name__ == "__main__": match_score = match_shapes(sc1, sc2) print("score>0.6") print(f"[score] ====== [{score}]") - print(f"[match_score] ====== [{match_score}]") - print(f"[tmp_count] ====== [{tmp_count}]") - print(f"[tmp_sum_r] ====== [{tmp_sum_r}]") - print(f"[tmp_sum_theta] ====== [{tmp_sum_theta}]") - if match_score < 5.0: #???? + if match_score < 5.0: # ???? param["start_point"] = (tmp_roi_start_x, tmp_roi_start_y) param["end_point"] = (tmp_roi_end_x, tmp_roi_end_y) param["score"] = (score, tmp_count, tmp_sum_r * tmp_roi_w_base_len, tmp_sum_theta) @@ -613,8 +680,6 @@ if __name__ == "__main__": param['hd_cam_h_bim'] = hd_cam_h_bim 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) # 找到得分最大的 @@ -626,71 +691,18 @@ if __name__ == "__main__": if abs(score[0] / match_score) > max_score: max_score = abs(score[0] / match_score) max_index = i - rst_p = rst_params[max_index] - bim_im = cv2.rectangle(bim_im, rst_p["start_point"], rst_p["end_point"], 100 * (i + 1), 50) + elapsed_time = time.time() - start_time + print(f"Execution time: {elapsed_time:.4f} seconds") + if max_index == -1: + return None + else: + return rst_params[max_index] + + # 画出广角相机矩形框 + # bim_im = cv2.rectangle(bim_im, rst_p["start_point"], rst_p["end_point"], 100 * (i + 1), 50) # 画出高清相机矩形框 - bim_im = cv2.rectangle(bim_im, (rst_p["hd_cam_x_bim"], rst_p["hd_cam_y_bim"]), ( - rst_p["hd_cam_x_bim"] + rst_p["hd_cam_w_bim"], rst_p["hd_cam_y_bim"] + rst_p["hd_cam_h_bim"],), (0, 0, 255), 40) - - # ====================== 预埋件号码匹配 开始 ========================= - hd_roi_list = get_hd_roi_from_txt("data_sub/test_1/roi_conners.txt") # 高清相机识别的ROI坐标 - # 高清相机坐标转换为bim图坐标 - hd_img_width = 9144 # 高清相机的图片宽度 - hd_img_height = 7000 # 高清相机的图片高度 - # 宽高肯定都是等比的直接按照bim上面高清矩形的宽度,算下比例。 - scale_w = rst_p["hd_cam_w_bim"] / hd_img_width - scale_h = rst_p["hd_cam_h_bim"] / hd_img_height - hd_roi_out_of_range_index = [] # 超出边界的ROI的索引 - for hd_roi_index, hd_roi in enumerate(hd_roi_list): - for i in range(4): - # 所有高清照片的ROI先按照这个比例转换 ,并且加上坐标偏移,转换为bim上实际坐标 - hd_roi[i][0] = int(hd_roi[i][0] * scale_w) + rst_p["hd_cam_x_bim"] - hd_roi[i][1] = int(hd_roi[i][1] * scale_h) + rst_p["hd_cam_y_bim"] - # 如果ROI坐标形成的矩形,横跨bim图上高清矩形范围边界框,就剔除这个矩形 - is_x_cross_left_border_line = hd_roi[0][0] < rst_p["hd_cam_x_bim"] <= hd_roi[1][0] - is_x_cross_right_border_line = hd_roi[0][0] < rst_p["hd_cam_x_bim"] + rst_p["hd_cam_w_bim"] <= hd_roi[1][0] - is_y_cross_top_border_line = hd_roi[0][1] < rst_p["hd_cam_y_bim"] <= hd_roi[2][1] - is_y_cross_bottom_border_line = hd_roi[0][1] < rst_p["hd_cam_y_bim"] + rst_p["hd_cam_h_bim"] <= hd_roi[2][1] - if is_x_cross_left_border_line or is_x_cross_right_border_line or is_y_cross_top_border_line or is_y_cross_bottom_border_line: - hd_roi_out_of_range_index.append(hd_roi_index) - # 画出来试试 - # bim_im = cv2.rectangle(bim_im, (hd_roi[0][0], hd_roi[0][1]), (hd_roi[2][0], hd_roi[2][1]), (0, 0, 255), 30) - # 写上i编号 - # cv2.putText(bim_im, str(hd_roi_index), (hd_roi[0][0], hd_roi[0][1]), cv2.FONT_HERSHEY_SIMPLEX, 5, (0, 0, 255),20) - print(f"超出边界的ROI的索引是:{hd_roi_out_of_range_index}") - - # bim所有的矩形,注意bim原数据是以左下角为原点的坐标系 !!! - # bim所有的矩形,注意bim原数据是以左下角为原点的坐标系 !!! - # bim所有的矩形,注意bim原数据是以左下角为原点的坐标系 !!! - bim_rect_list = data_bim["params"] - # 遍历bim所有的矩形,找到与高清相机ROI匹配的矩形 - bim_rect_hit_list = [] - for bim_rect_index, bim_rect_item in enumerate(bim_rect_list): - # x不需要转换 - bim_rect_item_center_x = bim_rect_item["x"] - # 把y坐标系转换成左上角坐标系 - bim_im_height = bim_im.shape[0] - bim_rect_item_center_y = bim_im_height - bim_rect_item["center"] - # 逐一和高清相机ROI坐标进行匹配 - for hd_roi_index, hd_roi in enumerate(hd_roi_list): - # 如果中心点的坐标在某个高清相机ROI内,就认为匹配上了。 - bim_rect_item_center_x_inside = hd_roi[0][0] < bim_rect_item_center_x < hd_roi[1][0] - bim_rect_item_center_y_inside = hd_roi[0][1] < bim_rect_item_center_y < hd_roi[2][1] - if bim_rect_item_center_x_inside and bim_rect_item_center_y_inside: - bim_rect_hit_list.append({ - "bim_rect": bim_rect_item, - "hd_roi": hd_roi - }) - # 画出bim_rect_hit_list - for bim_rect_hit_item in bim_rect_hit_list: - bim_rect = bim_rect_hit_item["bim_rect"] - hd_roi = bim_rect_hit_item["hd_roi"] - bim_im = cv2.rectangle(bim_im, (hd_roi[0][0], hd_roi[0][1]), (hd_roi[2][0], hd_roi[2][1]), (0, 0, 255), 30) - # 写上i编号 - cv2.putText(bim_im, str(bim_rect["code"]), (hd_roi[0][0]+100, hd_roi[0][1]+200), cv2.FONT_HERSHEY_SIMPLEX, 5, (0, 0, 255), - 20) - - # ===================== 预埋件号码匹配 结束 =========================== + # bim_im = cv2.rectangle(bim_im, (rst_p["hd_cam_x_bim"], rst_p["hd_cam_y_bim"]), ( + # rst_p["hd_cam_x_bim"] + rst_p["hd_cam_w_bim"], rst_p["hd_cam_y_bim"] + rst_p["hd_cam_h_bim"],), (0, 0, 255), 40) # # 预埋件匹配 # for i, param in enumerate(rst_params): @@ -716,11 +728,9 @@ if __name__ == "__main__": # # 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") - img_matches = cv2.resize(bim_im, (int(bim_im.shape[1] / 6), int(bim_im.shape[0] / 6))) + # img_matches = cv2.resize(bim_im, (int(bim_im.shape[1] / 6), int(bim_im.shape[0] / 6))) # sub_im = cv2.resize(sub_im, (int(sub_im.shape[1]/10), int(sub_im.shape[0]/10))) - cv2.imshow("2", img_matches) + # cv2.imshow("2", img_matches) # cnts的矩形画在sub_im 上 # for i in range(len(cnts)): # p = cnts[i] @@ -728,4 +738,51 @@ if __name__ == "__main__": # # 写编号 # cv2.putText(sub_im, str(i), (p['x'], p['y']), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # cv2.imshow("sub_im_after_filter", sub_im) + # cv2.waitKey(0) + + +if __name__ == "__main__": + + # ====================== 广角定位 开始 ========================= + data_bim_json = read_from_json("data_bim.json") # bim数据 + data_sub_json = read_from_json("data_sub/test_1/data_sub.json") # 广角识别之后的ROI数据 + wide_cam_img_width = 640 + wide_cam_img_height = 480 + bim_sub_area = search(data_bim_json, data_sub_json, wide_cam_img_width, wide_cam_img_height) + if bim_sub_area == None: + print("未找到匹配区域") + exit(0) + else: + print("bingo!!!!!!!") + # ====================== 广角定位 结束 ========================= + + # ====================== 预埋件号码匹配 开始 ========================= + bim_im = gen_im_from_params(data_bim_json) # bim图 + hd_roi_list = get_hd_roi_from_txt("data_sub/test_1/roi_conners.txt") # 高清摄像头的ROI数据 + hd_img_width = 9344 + hd_img_height = 7000 + bim_im_height = bim_im.shape[0] + bim_rect_hit_list = bim_compare_to_hd_roi(hd_roi_list, hd_img_width, hd_img_height, bim_im_height, bim_sub_area,data_bim_json) + # ===================== 预埋件号码匹配 结束 =========================== + + + # ========== 下面仅仅是测试画效果图 开始 ========== + # 画出广角相机矩形框 + cv2.rectangle(bim_im, bim_sub_area["start_point"], bim_sub_area["end_point"], 100, 50) + # 画出高清相机矩形框 + cv2.rectangle(bim_im, (bim_sub_area["hd_cam_x_bim"], bim_sub_area["hd_cam_y_bim"]), ( + bim_sub_area["hd_cam_x_bim"] + bim_sub_area["hd_cam_w_bim"], bim_sub_area["hd_cam_y_bim"] + bim_sub_area["hd_cam_h_bim"],), (0, 0, 255), 40) + + + # 在bim上画出高清识别对应的件号 + for bim_rect_hit_item in bim_rect_hit_list: + bim_rect = bim_rect_hit_item["bim_rect"] + hd_roi = bim_rect_hit_item["hd_roi"] + cv2.rectangle(bim_im, (hd_roi[0][0], hd_roi[0][1]), (hd_roi[2][0], hd_roi[2][1]), (0, 0, 255), 30) + # 写上i编号 + cv2.putText(bim_im, str(bim_rect["code"]), (hd_roi[0][0]+100, hd_roi[0][1]+200), cv2.FONT_HERSHEY_SIMPLEX, 5, (0, 0, 255), + 20) + bim_im_resize = cv2.resize(bim_im, (int(bim_im.shape[1] / 6), int(bim_im.shape[0] / 6))) + cv2.imshow("bim_im_resize", bim_im_resize) cv2.waitKey(0) + # ========== 下面仅仅是测试画效果图 结束 ========== diff --git a/utils.py b/utils.py index 4260829..cb80a44 100644 --- a/utils.py +++ b/utils.py @@ -13,27 +13,25 @@ def re_cal_point(point, offset): -def filter_rectangle(image_path, points, wide_cam_left_cut_rate, wide_cam_right_cut_rate): +def filter_rectangle(image_width,image_height, points, wide_cam_left_cut_rate, wide_cam_right_cut_rate): """ 根据左右裁剪之后的图像,过滤矩形。在裁剪之后,整个矩形已经不在图片里面的,就去掉。 1 高度过大的不要 2 整个矩形全部身体都在裁剪区域之外的不要 - image_path:图片路径 + image_width:原始广角图片的宽度 + image_height:原始广角图片的高度 points:要过滤的点 wide_cam_left_cut_rate:# 左边界的裁剪比例,从左边开始裁剪百分之多少 wide_cam_right_cut_rate # 右边界的裁剪比例,从右边开始裁剪百分之多少 返回值: - 1 过滤之后的矩形 - 2 裁剪之后的图片 + filtered_points:过滤之后的全部都矩形坐标 + wide_cam_img_width_after_cut:裁剪之后的图片的宽度 """ # 高度过大矩形过滤参数 max_height_rate = 0.5 # 矩形高度占整个画面高度的最大比例,如果超过该比例,则认为是无效矩形 - image = cv2.imread(image_path) - image_height = image.shape[0] # 获取图片高度 - image_width = image.shape[1] # 获取图片宽度 image_x_min = int(image_width * wide_cam_left_cut_rate) # 左边界的裁剪点 image_x_max = int(image_width * (1 - wide_cam_right_cut_rate)) # 右边界的裁剪点 @@ -73,7 +71,7 @@ def filter_rectangle(image_path, points, wide_cam_left_cut_rate, wide_cam_right_ # 图片裁剪 # 裁剪图片 (height方向不变,宽度方向裁剪) - cropped_image = image[:, image_x_min:image_x_max] + # cropped_image = image[:, image_x_min:image_x_max] # 展示 # cv2.imshow("cropped_image", cropped_image) # cv2.imshow("image", image) @@ -84,7 +82,9 @@ def filter_rectangle(image_path, points, wide_cam_left_cut_rate, wide_cam_right_ # cv2.putText(cropped_image, str(i), (p['x'], p['y']), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # cv2.imshow("cropped_image_draw", cropped_image) # cv2.waitKey(0) - return filtered_points, cropped_image + wide_cam_img_width_after_cut = image_x_max - image_x_min + + return filtered_points, wide_cam_img_width_after_cut