ymj_bim_test/utils.py

61 lines
2.2 KiB
Python
Raw Normal View History

2025-02-27 17:31:43 +08:00
import json
import logging
import cv2
def filter_points(image_path,points):
# 高度过大点过滤参数
max_height_rate = 0.5 # 矩形高度占整个画面高度的最大比例,如果超过该比例,则认为是无效矩形
# x范围过滤只保留中间部分的点只保留矩形完整出现在在画面中间的那部分矩形。
left_x_cut_rate=0.2 # 左边界的裁剪比例,从左边开始裁剪百分之多少
right_x_cut_rate=0.2 # 右边界的裁剪比例,从右边开始裁剪百分之多少
image = cv2.imread(image_path)
image_height = image.shape[0]
image_width = image.shape[1]
image_x_min = image_width * left_x_cut_rate
image_x_max = image_width * (1 - right_x_cut_rate)
#开始过滤点
bad_point_index = []
print(f'开始过滤点,原有点数为{len(points)}')
for index in range(len(points)):
point = points[index]
# 高度过大过滤
if point['height'] > image_height * max_height_rate:
bad_point_index.append(index)
continue
# x坐标范围过滤
# 原point中的x和y,是矩形左上角的坐标
x_min = point['x'] # 矩形四个点坐标中x的最小值
x_max = point['x'] + point['width'] # 矩形四个点坐标中x的最大值
# 如果矩形x的最小值小于左边界去除这个矩形
if x_min < image_x_min:
bad_point_index.append(index)
continue
# 如果矩形x的最大值大于右边界去除这个矩形
if x_max > image_x_max:
bad_point_index.append(index)
continue
# 删除bad_point_index
filtered_points = []
for i, point in enumerate(points):
if i not in bad_point_index:
filtered_points.append(point)
2025-02-27 17:35:39 +08:00
print(f'过滤点结束,过滤之后的点数为{len(filtered_points)}')
2025-02-27 17:31:43 +08:00
return filtered_points
# def read_from_json(file_path):
# with open(file_path, 'r') as f:
# loaded_array = json.load(f)
# return loaded_array
# cnts = read_from_json("data_sub/test_1/data_sub.json")
# filter_points("data_sub/test_1/wide_image.png",cnts)