fix 中心点计算

This commit is contained in:
leon 2025-03-06 15:47:32 +08:00
parent 457a33bad8
commit bbd68bee55
4 changed files with 139 additions and 25 deletions

BIN
bim_im.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 563 KiB

BIN
result.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 456 KiB

102
search.py
View File

@ -380,6 +380,7 @@ def bim_compare_to_hd_roi(hd_roi_list, hd_img_width, hd_img_height, bim_im_heigh
}
]
"""
# bim_im = gen_im_from_params(bim_rect_list) # bim图
# 高清相机坐标转换为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
@ -399,25 +400,58 @@ def bim_compare_to_hd_roi(hd_roi_list, hd_img_width, hd_img_height, bim_im_heigh
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)
# 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)
# cv2.imwrite("bim_im.jpg", bim_im)
# print(f"超出边界的ROI的索引是:{hd_roi_out_of_range_index}")
# ====================== 遍历bim所有的矩形找到与高清相机ROI匹配的矩形 开始 =========================
# bim所有的矩形注意bim原数据是以左下角为原点的坐标系
# bim所有的矩形注意bim原数据是以左下角为原点的坐标系
# bim所有的矩形注意bim原数据是以左下角为原点的坐标系
# 遍历bim所有的矩形找到与高清相机ROI匹配的矩形
bim_rect_hit_list = []
bim_rect_in_hd_roi_list =[]
# 1. 从bim中筛选出在高清相机ROI范围内的矩形不完整的矩形也算上因为转换到bim图上面的高清的ROI范围不是很准确的。
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
# 把bim矩形的坐标系转换成左上角的坐标系,
# 矩形左上角
x1 = int(bim_rect_item['x'] - bim_rect_item['w'] / 2)
y1 = bim_im_height - int(bim_rect_item['center'] + bim_rect_item['h'] / 2)
# 矩形右下角
x3 = int(bim_rect_item['x'] + bim_rect_item['w'] / 2)
y3 = bim_im_height - int(bim_rect_item['center'] - bim_rect_item['h'] / 2)
# 高清的roi范围
hd_roi_x_start = bim_sub_area["hd_cam_x_bim"]
hd_roi_x_end = bim_sub_area["hd_cam_x_bim"] + bim_sub_area["hd_cam_w_bim"]
hd_roi_y_start = bim_sub_area["hd_cam_y_bim"]
hd_roi_y_end = bim_sub_area["hd_cam_y_bim"] + bim_sub_area["hd_cam_h_bim"]
# 开始判断
# 如矩形左上角在在高清ROI范围内 或着右下角在高清ROI范围之内 就算矩形在高清ROI之内
is_1_inside = (x1 >= hd_roi_x_start and x1 <= hd_roi_x_end) and (y1 >= hd_roi_y_start and y1 <= hd_roi_y_end)
is_3_inside = (x3 >= hd_roi_x_start and x3 <= hd_roi_x_end) and (y3 >= hd_roi_y_start and y3 <= hd_roi_y_end)
if(is_1_inside or is_3_inside):
bim_rect_in_hd_roi_list.append(bim_rect_item)
print(f"[bim上在高清相机矩形范围内预埋件数量为] ====== [{len(bim_rect_in_hd_roi_list)}]")
# 2. 先根据中心点是否落在高清ROI内第一次匹配。
un_hit_hd_roi_list = [] #中心点匹配没匹配上的高清ROI用来进行第三步的中心点距离匹配
bim_rect_hit_index_list = [] # bim上已经被匹配上的索引
for hd_roi_index, hd_roi in enumerate(hd_roi_list):
if (hd_roi_index in hd_roi_out_of_range_index):
continue
is_hd_roi_hit = False
for bim_rect_index, bim_rect_item in enumerate(bim_rect_in_hd_roi_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坐标进行匹配,判断哪一个高清ROi的中心点距离bim上面这块
center_distance_min = 0
# 如果中心点的坐标在某个高清相机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]
@ -426,7 +460,44 @@ def bim_compare_to_hd_roi(hd_roi_list, hd_img_width, hd_img_height, bim_im_heigh
"bim_rect": bim_rect_item,
"hd_roi": hd_roi
})
bim_rect_hit_index_list.append(bim_rect_index)
is_hd_roi_hit = True
break
# 没匹配上的,存起来第三步再匹配
if(is_hd_roi_hit == False):
un_hit_hd_roi_list.append(hd_roi)
# 3. 如过中心点匹配有的高清ROI没匹配上去再使用中心点距离去匹配
for hd_roi_index, hd_roi in enumerate(un_hit_hd_roi_list):
if (hd_roi_index in hd_roi_out_of_range_index):
continue
hd_roi_center_x = int(hd_roi[0][0] + (hd_roi[2][0] - hd_roi[0][0]) / 2)
hd_roi_center_y = int(hd_roi[0][1] + (hd_roi[2][1] - hd_roi[0][1]) / 2)
center_distance_min = 99999 # 距离最小的值
center_distance_min_bim_rect_index = -1 # 距离最小的值这个bim的索引
# 逐一和bim进行匹配,找到bim中心点距离这个高清相机ROI的中心点最近的
for bim_rect_index, bim_rect_item in enumerate(bim_rect_in_hd_roi_list):
# 如果中心点已经匹配过的,跳过
if(bim_rect_index in bim_rect_hit_index_list):
continue
# x不需要转换
bim_rect_item_center_x = bim_rect_item["x"]
# 把y坐标系转换成左上角坐标系
bim_rect_item_center_y = bim_im_height - bim_rect_item["center"]
# 计算中心点距离
center_distance = math.sqrt((hd_roi_center_x - bim_rect_item_center_x) ** 2 + (hd_roi_center_y - bim_rect_item_center_y) ** 2)
if(center_distance < center_distance_min):
center_distance_min = center_distance
center_distance_min_bim_rect_index = bim_rect_index
bim_rect_hit_list.append({
"bim_rect": bim_rect_in_hd_roi_list[center_distance_min_bim_rect_index],
"hd_roi": hd_roi
})
return bim_rect_hit_list
# ====================== 遍历bim所有的矩形找到与高清相机ROI匹配的矩形 开始 =========================
# 画出bim_rect_hit_list
# for bim_rect_hit_item in bim_rect_hit_list:
# bim_rect = bim_rect_hit_item["bim_rect"]
@ -807,13 +878,14 @@ if __name__ == "__main__":
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)
image = cv2.imread("data_sub/test_5/wide_image.png")
image_x_min = int(640 * 0.1) # 左边界的裁剪点
image_x_max = int(640 * (1 - 0.1)) # 右边界的裁剪点
cropped_image = image[:, image_x_min:image_x_max]
# image = cv2.imread("data_sub/test_5/wide_image.png")
# image_x_min = int(640 * 0.1) # 左边界的裁剪点
# image_x_max = int(640 * (1 - 0.1)) # 右边界的裁剪点
# cropped_image = image[:, image_x_min:image_x_max]
# cv2.imshow("image", image)
# 展示
cv2.imshow("cropped_image", cropped_image)
cv2.imshow("image", image)

62
test.py
View File

@ -1,18 +1,60 @@
import numpy as np
import cv2
from utils import get_hd_cam_rect
from search import gen_im_from_params, get_bim_height, bim_compare_to_hd_roi, read_from_json
from utils import get_hd_roi_from_txt
roi_list_after_cal = [
np.array([[338, 164],
[462, 164],
[462, 289],
[338, 289]], dtype=np.int32),
np.array([[296, 86],
[360, 86],
[360, 151],
[296, 151]], dtype=np.int32),
np.array([[113, 1],
[207, 1],
[207, 88],
[113, 88]], dtype=np.int32),
np.array([[206, 259],
[281, 259],
[281, 335],
[206, 335]], dtype=np.int32),
np.array([[384, 35],
[448, 35],
[448, 102],
[384, 102]], dtype=np.int32),
np.array([[127, 180],
[275, 180],
[275, 241],
[127, 241]], dtype=np.int32)
]
def _darw_rect(im):
x, y, w, h = get_hd_cam_rect(0)
print(w)
print(h)
cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 5)
image = cv2.imread("./result.png")
for index in range(len(roi_list_after_cal)):
item = roi_list_after_cal[index]
cv2.polylines(image, [item], True, (0, 0, 255), 2)
# index
cv2.putText(image, str(index), (item[0][0], item[0][1]+5), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255),
2)
sub_im = cv2.imread("data_sub/test_1/wide_image.png")
_darw_rect(sub_im)
cv2.imshow("sub_zero", sub_im)
cv2.imshow("image", image)
cv2.waitKey(0)
# ====================== 预埋件号码匹配 开始 =========================
data_bim_json = read_from_json("data_bim.json") # bim数据
bim_sub_area = {'code': 1, 'type': '250x1450', 'x': 945, 'y': 0, 'center': 351, 'w': 1450, 'h': 250, 'angle': '0', 'x1': 220, 'y1': 1833, 'x2': 1670, 'y2': 1833, 'x3': 1670, 'y3': 2083, 'x4': 220, 'y4': 2083, 'x_center': 945, 'y_center': 1958, 'effective_points': [[220, 1833, 0], [1670, 1833, 0], [1670, 2083, 0], [220, 2083, 0], [945, 1958, 0], [773, 1405, 1], [1073, 1405, 1], [1073, 1705, 1], [773, 1705, 1], [923, 1555, 1], [1388, 1005, 2], [1888, 1005, 2], [1888, 1506, 2], [1388, 1506, 2], [1638, 1255, 2], [393, 1035, 3], [1041, 1035, 3], [1041, 1285, 3], [393, 1285, 3], [717, 1160, 3], [1212, 641, 4], [1462, 641, 4], [1462, 891, 4], [1212, 891, 4], [1337, 766, 4], [1655, 435, 5], [1904, 435, 5], [1904, 684, 5], [1655, 684, 5], [1779, 559, 5], [349, 154, 6], [748, 154, 6], [748, 554, 6], [349, 554, 6], [548, 354, 6], [992, 0, 7], [1294, 0, 7], [1294, 300, 7], [992, 300, 7], [1143, 150, 7], [1594, 2, 8], [1896, 2, 8], [1896, 302, 8], [1594, 302, 8], [1745, 152, 8], [2215, 1840, 9], [2215, 2088, 9], [2938, 1964, 9], [2798, 1413, 10], [3095, 1413, 10], [3095, 1711, 10], [2798, 1711, 10], [2946, 1562, 10], [3390, 1020, 11], [3390, 1519, 11], [2419, 1041, 12], [3067, 1041, 12], [3067, 1286, 12], [2419, 1286, 12], [2743, 1163, 12], [3214, 636, 13], [3214, 883, 13], [3337, 759, 13], [2352, 155, 15], [2744, 155, 15], [2744, 553, 15], [2352, 553, 15], [2548, 354, 15], [2996, 12, 16], [3289, 12, 16], [3289, 312, 16], [2996, 312, 16], [3142, 162, 16]], 'start_point': (-643, -805), 'end_point': (3413, 2998), 'score': (0.9973816190955833, 73, 2007.9927917215468, -0.5308235609555432), 'match_score': 0.49774032187522993, 'hd_cam_x_bim': -135, 'hd_cam_y_bim': 304, 'hd_cam_w_bim': 2472, 'hd_cam_h_bim': 1854}
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 = int(9344/16)
hd_img_height = int(7000/16)
bim_im_height = get_bim_height(data_bim_json)
bim_rect_hit_list = bim_compare_to_hd_roi(roi_list_after_cal, hd_img_width, hd_img_height, bim_im_height, bim_sub_area,
data_bim_json)
print(f"[bim_rect_hit_list] ====== [{len(bim_rect_hit_list)}]")
# ===================== 预埋件号码匹配 结束 ===========================