小言_互联网的博客

LabelImg标注的YOLO格式txt标签中心坐标和物体边界框长宽的转换

413人阅读  评论(0)

目录

1.LabelImg标注的YOLO格式的TXT标签


Opencv+YOLO-V3实现目标跟踪

YOLO-V3实时检测实现(opencv+python实现)——改进——>更加的易懂

YOLO-V3实时检测实现(opencv+python实现)

1.LabelImg标注的YOLO格式的TXT标签

关于LabelImg下载及使用:标注工具 labelImg 的下载安装及使用

首先标注一张图片:

查看标签.txt文件:

 

提示:如果我们要进行训练的话,那么需要上面的坐标进行变换一下,得到框的左上角的坐标和右下角的坐标。

  

现在我们要根据中心坐标和边框得到左上角(xmin,ymin)和右下角(xmax,ymax)坐标:

变换公式如下:

提示:现在已经知道坐标之间的关系了,那么可以使用python进行求解:

  • 第一步从标注的.txt文件中读取数据;
  • 第二步将读取的数据利用上面的公式进行转换;

  
  1. def Xmin_Xmax_Ymin_Ymax():
  2. img_path = "data/train/000_0.png"
  3. txt_path = "data/XML/000_0.txt"
  4. img = cv2. imread(img_path)
  5. # 获取图片的高宽
  6. h, w, _ = img.shape
  7. txt_path= "data/XML/000_0.txt"
  8. #读取TXT文件 中的中心坐标和框大小
  9. with open(txt_path, "r") as fp:
  10. #以空格划分
  11. contline=fp. readline(). split( ' ')
  12. #contline : class x_center y_center width height
  13. print(contline)
  14. #计算框的左上角坐标和右下角坐标,使用strip将首尾空格去掉
  15. xmin= float((contline[ 1]). strip()) -float(contline[ 3]. strip())/ 2
  16. xmax= float(contline[ 1]. strip())+ float(contline[ 3]. strip())/ 2
  17. ymin = float(contline[ 2]. strip()) - float(contline[ 4]. strip()) / 2
  18. ymax = float(contline[ 2]. strip()) + float(contline[ 4]. strip()) / 2
  19. #将坐标( 0- 1之间的值)还原回在图片中实际的坐标位置
  20. xmin,xmax=w*xmin,w*xmax
  21. ymin,ymax=h*ymin,h*ymax
  22. return (xmin,xmax,ymin,ymax)

将返回的坐标利用opencv将框绘制出来:


  
  1. def draw(tupelist):
  2. img_path = "data/train/000_0.png"
  3. img = cv2. imread(img_path)
  4. xmin=tupelist[ 0]
  5. xmax=tupelist[ 1]
  6. ymin=tupelist[ 2]
  7. ymax=tupelist[ 3]
  8. cv2. rectangle(img,( int(xmin), int(ymin)),( int(xmax), int(ymax)),( 255, 0, 255), 2)
  9. cv2. imshow( 'img',img)
  10. cv2. waitKey( 0)
  11. cv2. destroyAllWindows()

  
  1. import cv2
  2. import os
  3. import numpy as np
  4. if __name__ == '__main__':
  5. tuplelist= Xmin_Xmax_Ymin_Ymax()
  6. draw(tuplelist)

 


转载:https://blog.csdn.net/Keep_Trying_Go/article/details/128224748
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场