如何将Yolo格式的边界框坐标转换成OpenCV格式
How to convert Yolo format bounding box coordinates into OpenCV format
我有 Yolo
格式的对象边界框注释保存在 .txt
文件中。现在我想加载这些坐标并使用 OpenCV
在图像上绘制它,但我不知道如何将这些 float 值转换为 OpenCV
格式坐标值
我试过了 但没有用,下面是我正在尝试做的示例
代码与输出
import matplotlib.pyplot as plt
import cv2
img = cv2.imread(<image_path>)
dh, dw, _ = img.shape
fl = open(<label_path>, 'r')
data = fl.readlines()
fl.close()
for dt in data:
_, x, y, w, h = dt.split(' ')
nx = int(float(x)*dw)
ny = int(float(y)*dh)
nw = int(float(w)*dw)
nh = int(float(h)*dh)
cv2.rectangle(img, (nx,ny), (nx+nw,ny+nh), (0,0,255), 1)
plt.imshow(img)
实际注释和图像
0 0.286972 0.647157 0.404930 0.371237
0 0.681338 0.366221 0.454225 0.418060
关于这个主题还有另一个问答,在接受的答案下面有 1 个有趣的评论。最重要的是,YOLO 坐标具有不同的中心 w.r.t。到图像。不幸的是,评论员没有提供 Python 端口,所以我在这里做了:
import cv2
import matplotlib.pyplot as plt
img = cv2.imread(<image_path>)
dh, dw, _ = img.shape
fl = open(<label_path>, 'r')
data = fl.readlines()
fl.close()
for dt in data:
# Split string to float
_, x, y, w, h = map(float, dt.split(' '))
# Taken from https://github.com/pjreddie/darknet/blob/810d7f797bdb2f021dbe65d2524c2ff6b8ab5c8b/src/image.c#L283-L291
# via
l = int((x - w / 2) * dw)
r = int((x + w / 2) * dw)
t = int((y - h / 2) * dh)
b = int((y + h / 2) * dh)
if l < 0:
l = 0
if r > dw - 1:
r = dw - 1
if t < 0:
t = 0
if b > dh - 1:
b = dh - 1
cv2.rectangle(img, (l, t), (r, b), (0, 0, 255), 1)
plt.imshow(img)
plt.show()
所以,对于一些 Lenna 图像,这就是输出,我认为它显示了正确的坐标 w.r.t。你的图片:
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
Matplotlib: 3.3.2
OpenCV: 4.4.0
----------------------------------------
1请为链接的答案和评论点赞。
有更多 straight-forward 的方法可以用 pybboxes 做这些事情。安装,
pip install pybboxes
在你的情况下,
import pybboxes as pbx
yolo_bbox1 = (0.286972, 0.647157, 0.404930, 0.371237)
yolo_bbox2 = (0.681338, 0.366221, 0.454225, 0.418060)
W, H = 300, 300 # WxH of the image
pbx.convert_bbox(yolo_bbox1, from_type="yolo", to_type="voc", image_width=W, image_height=H)
>>> (25, 138, 147, 250)
pbx.convert_bbox(yolo_bbox2, from_type="yolo", to_type="voc", image_width=W, image_height=H)
>>> (136, 47, 273, 173)
注意,转成YOLO格式需要图片宽高进行缩放。
我有 Yolo
格式的对象边界框注释保存在 .txt
文件中。现在我想加载这些坐标并使用 OpenCV
在图像上绘制它,但我不知道如何将这些 float 值转换为 OpenCV
格式坐标值
我试过了
代码与输出
import matplotlib.pyplot as plt
import cv2
img = cv2.imread(<image_path>)
dh, dw, _ = img.shape
fl = open(<label_path>, 'r')
data = fl.readlines()
fl.close()
for dt in data:
_, x, y, w, h = dt.split(' ')
nx = int(float(x)*dw)
ny = int(float(y)*dh)
nw = int(float(w)*dw)
nh = int(float(h)*dh)
cv2.rectangle(img, (nx,ny), (nx+nw,ny+nh), (0,0,255), 1)
plt.imshow(img)
实际注释和图像
0 0.286972 0.647157 0.404930 0.371237
0 0.681338 0.366221 0.454225 0.418060
关于这个主题还有另一个问答,在接受的答案下面有
import cv2
import matplotlib.pyplot as plt
img = cv2.imread(<image_path>)
dh, dw, _ = img.shape
fl = open(<label_path>, 'r')
data = fl.readlines()
fl.close()
for dt in data:
# Split string to float
_, x, y, w, h = map(float, dt.split(' '))
# Taken from https://github.com/pjreddie/darknet/blob/810d7f797bdb2f021dbe65d2524c2ff6b8ab5c8b/src/image.c#L283-L291
# via
l = int((x - w / 2) * dw)
r = int((x + w / 2) * dw)
t = int((y - h / 2) * dh)
b = int((y + h / 2) * dh)
if l < 0:
l = 0
if r > dw - 1:
r = dw - 1
if t < 0:
t = 0
if b > dh - 1:
b = dh - 1
cv2.rectangle(img, (l, t), (r, b), (0, 0, 255), 1)
plt.imshow(img)
plt.show()
所以,对于一些 Lenna 图像,这就是输出,我认为它显示了正确的坐标 w.r.t。你的图片:
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
Matplotlib: 3.3.2
OpenCV: 4.4.0
----------------------------------------
1请为链接的答案和评论点赞。
有更多 straight-forward 的方法可以用 pybboxes 做这些事情。安装,
pip install pybboxes
在你的情况下,
import pybboxes as pbx
yolo_bbox1 = (0.286972, 0.647157, 0.404930, 0.371237)
yolo_bbox2 = (0.681338, 0.366221, 0.454225, 0.418060)
W, H = 300, 300 # WxH of the image
pbx.convert_bbox(yolo_bbox1, from_type="yolo", to_type="voc", image_width=W, image_height=H)
>>> (25, 138, 147, 250)
pbx.convert_bbox(yolo_bbox2, from_type="yolo", to_type="voc", image_width=W, image_height=H)
>>> (136, 47, 273, 173)
注意,转成YOLO格式需要图片宽高进行缩放。