我是靠谱客的博主 包容便当,这篇文章主要介绍(一)根据yolo格式txt标签文件,在原图中截取目标图片并保存,现在分享给大家,希望可以做个参考。

处理一张图片

1)构建代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import cv2 import os def cut_one_image(img_path, txt_path): # 读取txt文件信息 def read_list(txt_path): pos = [] with open(txt_path, 'r') as file_to_read: while True: lines = file_to_read.readline() # 整行读取数据 if not lines: break pass # 将整行数据分割处理,如果分割符是空格,括号里就不用传入参数,如果是逗号, 则传入‘,'字符。 p_tmp = [float(i) for i in lines.split(' ')] pos.append(p_tmp) # 添加新读取的数据 # Efield.append(E_tmp) pass return pos # txt转换为box def convert(size, box): xmin = (box[1]-box[3]/2.)*size[1] xmax = (box[1]+box[3]/2.)*size[1] ymin = (box[2]-box[4]/2.)*size[0] ymax = (box[2]+box[4]/2.)*size[0] box = (int(xmin), int(ymin), int(xmax), int(ymax)) return box image = cv2.imread(img_path) pos = read_list(txt_path) for i in range(len(pos)): box = convert(image.shape, pos[i]) image = cv2.rectangle(image, (box[0], box[1]), (box[2],box[3]), (0,255,0), 1) save_path = '1_'+ str(i)+'.jpg' print(save_path) img = image[box[1]:box[3], box[0]:box[2]] cv2.imwrite(save_path, img)

2)执行代码

复制代码
1
2
cut_one_image(img_path = "1.jpg", txt_path = "1.txt")

3)或者执行代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
def choose_one(img_folder, label_folder,i): img_list = os.listdir(img_folder) img_list.sort() label_list = os.listdir(label_folder) label_list.sort() img_path = img_folder + "/" + img_list[i] txt_path = label_folder + "/" + label_list[i] cut_one_image(img_path = img_path, txt_path = txt_path) choose_one(img_folder="./images", label_folder = "./yolo_label", i=0)

更改i数值即可以

改进版本

1)构建代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import cv2 import os def cut_one_image(img_path, txt_path, path): # 读取txt文件信息 def read_list(txt_path): pos = [] with open(txt_path, 'r') as file_to_read: while True: lines = file_to_read.readline() # 整行读取数据 if not lines: break pass # 将整行数据分割处理,如果分割符是空格,括号里就不用传入参数,如果是逗号, 则传入‘,'字符。 p_tmp = [float(i) for i in lines.split(' ')] pos.append(p_tmp) # 添加新读取的数据 # Efield.append(E_tmp) pass return pos # txt转换为box def convert(size, box): xmin = (box[1]-box[3]/2.)*size[1] xmax = (box[1]+box[3]/2.)*size[1] ymin = (box[2]-box[4]/2.)*size[0] ymax = (box[2]+box[4]/2.)*size[0] box = (int(xmin), int(ymin), int(xmax), int(ymax)) return box image = cv2.imread(img_path) pos = read_list(txt_path) for i in range(len(pos)): box = convert(image.shape, pos[i]) image = cv2.rectangle(image, (box[0], box[1]), (box[2],box[3]), (0,255,0), 1) save_path = path + '_' + str(i)+'.jpg' print(save_path) img = image[box[1]:box[3], box[0]:box[2]] cv2.imwrite(save_path, img) def choose_one(img_folder, label_folder,i): img_list = os.listdir(img_folder) img_list.sort() label_list = os.listdir(label_folder) label_list.sort() img_path = img_folder + "/" + img_list[i] path1 = img_path.split(".")[0].split("/")[1] print(path1) txt_path = label_folder + "/" + label_list[i] cut_one_image(img_path = img_path, txt_path = txt_path, path = path1)

2)执行代码

复制代码
1
2
3
choose_one(img_folder="images", label_folder = "yolo_label", i=0)

更改i数值即可以

最后

以上就是包容便当最近收集整理的关于(一)根据yolo格式txt标签文件,在原图中截取目标图片并保存的全部内容,更多相关(一)根据yolo格式txt标签文件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部