在python中将轮廓点转换为边界框

时间:2021-02-11 22:40:03

标签: python opencv computer-vision bounding-box

我正在使用包含感兴趣区域的轮廓坐标的 xml 文件处理医学图像。我能够提取这些点,但无法将其转换为可用于为医学图像创建遮罩的边界框。

以下是一个感兴趣区域的“point_px”。 'point_mm' 是一个类似的元组列表,全部为零。

[
(2462.929932, 995.062988), (2452.830078, 986.978027), (2442.719971, 982.935974), (2430.914551, 983.919678), 
(2420.48999, 988.999023), (2409.370117, 997.083984), (2402.300049, 1008.200012), (2397.25, 1019.320007),
(2393.172363, 1030.600708), (2392.179199, 1041.526123), (2392.179199, 1052.451416), (2394.165527, 1065.363159), 
(2399.131592, 1079.268066), (2410.056885, 1089.200195), (2421.975586, 1094.16626), (2432.900879, 1097.145996), 
(2443.659912, 1095.849976), (2453.399902, 1094.329956), (2465.969971, 1090.060059), (2477.080078, 1081.969971), 
(2485.169922, 1071.869995), (2491.22998, 1060.75), (2491.22998, 1049.630005), (2490.219971, 1038.52002), 
(2484.159912, 1025.380005), (2478.090088, 1015.27002), (2472.030029, 1004.159973)
]

图像上没有轮廓,因此我无法使用 findcontour 方法。

1 个答案:

答案 0 :(得分:0)

您可以通过查找最低的 x,y 值和最高的 x,y 值(分别为左上角和右下角)来获得边界框。

import numpy as np

# given points
points = [
(2462.929932, 995.062988), (2452.830078, 986.978027), (2442.719971, 982.935974), (2430.914551, 983.919678), 
(2420.48999, 988.999023), (2409.370117, 997.083984), (2402.300049, 1008.200012), (2397.25, 1019.320007),
(2393.172363, 1030.600708), (2392.179199, 1041.526123), (2392.179199, 1052.451416), (2394.165527, 1065.363159), 
(2399.131592, 1079.268066), (2410.056885, 1089.200195), (2421.975586, 1094.16626), (2432.900879, 1097.145996), 
(2443.659912, 1095.849976), (2453.399902, 1094.329956), (2465.969971, 1090.060059), (2477.080078, 1081.969971), 
(2485.169922, 1071.869995), (2491.22998, 1060.75), (2491.22998, 1049.630005), (2490.219971, 1038.52002), 
(2484.159912, 1025.380005), (2478.090088, 1015.27002), (2472.030029, 1004.159973)]

# to numpy array
numped = np.array(points);

# get corners (top-left, bottom-right)
tl = [min(numped[:,0]), min(numped[:,1])];
br = [max(numped[:,0]), max(numped[:,1])];

# print
print(tl);
print(br);

numpy slice numped[:,0] 将所有 x 值作为单个 1D numpy 数组获取

另一个切片 numped[:,1] 获取所有 y 值。

相关问题