将mask的图片标签转换为yolo的txt标签

将mask的图片标签转换为yolo的txt标签

在这里插入图片描述

import copy
import cv2
import os
import shutil
import numpy as np


path = "你的mask路径  /Dataset/mask"
files = os.listdir(path)
for file in files:
    name = file.split('.')[0]
    file_path = os.path.join(path,name+'.png')
    img = cv2.imread(file_path)
    # img = cv2.imread(path)
    H,W=img.shape[0:2]
    print(H,W)

    #img1 = cv2.imread("F:/Deep_Learning/Model/YOLOv8_Seg/Dataset/images/20160222_080933_361_1.jpg")

    gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret,bin_img = cv2.threshold(gray_img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    cnt,hit = cv2.findContours(bin_img,cv2.RETR_TREE,cv2.CHAIN_APPROX_TC89_KCOS)

    #cv2.drawContours(img1,cnt,-1,(0,255,0),5)

    cnt = list(cnt)
    f = open("标签保存路径 Dataset/labels/{}.txt".format(file.split(".")[0]), "a+")
    for j in cnt:
        result = []
        pre = j[0]
        for i in j:
            if abs(i[0][0] - pre[0][0]) > 1 or abs(i[0][1] - pre[0][1]) > 1:# 在这里可以调整间隔点,我设置为1
                pre = i
                temp = list(i[0])
                temp[0] /= W
                temp[1] /= H
                result.append(temp)

                #cv2.circle(img1,i[0],1,(0,0,255),2)

        print(result)
        print(len(result))

        # if len(result) != 0:

        if len(result) != 0:
            f.write("0 ")
            for line in result:
                line = str(line)[1:-2].replace(",","")
                # print(line)
                f.write(line+" ")
            f.write("\n")
    f.close()

    #cv2.imshow("test",img1)
    # while True:
    #     key = cv2.waitKey(1)  # 等待 1 毫秒,返回键盘按键的 ASCII 值
    #     if key == ord('q'):  # 如果按下 'q' 键,退出循环
    #         break
    #
    # cv2.destroyAllWindows()  # 关闭窗口

转换后得到所有的轮廓信息

获取外轮廓

在这里插入图片描述

cv2.RETR_EXTERNAL

替换一下,得到如下txt信息

在这里插入图片描述
经过YOLOv8训练后
在这里插入图片描述

  • 9
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是一个 Python 程序,可以将图片旋转指定角度后,相应地调整 YOLO 标签中的坐标。 ```python import cv2 import math # 旋转图片并调整 YOLO 标签 def rotate_image_and_labels(img_path, label_path, angle): # 读取图片 img = cv2.imread(img_path) # 计算旋转后的图片大小和中心点 height, width = img.shape[:2] center_x, center_y = width / 2, height / 2 new_width = int(abs(width * math.cos(angle)) + abs(height * math.sin(angle))) new_height = int(abs(width * math.sin(angle)) + abs(height * math.cos(angle))) # 旋转图片 M = cv2.getRotationMatrix2D((center_x, center_y), angle, 1.0) rotated_img = cv2.warpAffine(img, M, (new_width, new_height)) # 保存旋转后的图片 cv2.imwrite('rotated.jpg', rotated_img) # 读取标签文件并调整坐标 with open(label_path, 'r') as f: labels = f.readlines() new_labels = [] for label in labels: label = label.strip().split(' ') x, y, w, h = float(label[1]), float(label[2]), float(label[3]), float(label[4]) # 计算坐标变换后的新坐标 new_x = (x - center_x) * math.cos(angle) + (y - center_y) * math.sin(angle) + new_width / 2 new_y = -(x - center_x) * math.sin(angle) + (y - center_y) * math.cos(angle) + new_height / 2 new_w = w * math.cos(angle) + h * math.sin(angle) new_h = h * math.cos(angle) + w * math.sin(angle) # 将新坐标保存到新标签列表中 new_label = label[0] + ' ' + str(new_x) + ' ' + str(new_y) + ' ' + str(new_w) + ' ' + str(new_h) + '\n' new_labels.append(new_label) # 保存新标签文件 with open('rotated.txt', 'w') as f: f.writelines(new_labels) ``` 该程序接受三个参数:原始图片路径、YOLO 标签文件路径和旋转角度。程序会先将图片旋转指定角度,然后读取标签文件中的坐标,并根据图片旋转后的变换公式计算出新的坐标,最后保存旋转后的图片和新的标签文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值