当前位置: 首页 > news >正文

将语义分割的标签转换为实例分割(yolo)的标签

语义分割的标签(目标处为255,其余处为0)
在这里插入图片描述
实例分割的标签(yolo.txt),描述边界的多边形顶点的归一化位置
在这里插入图片描述
绘制在原图类似蓝色的边框所示。
在这里插入图片描述

废话不多说,直接贴代码;

import os
import cv2
import numpy as np
import shutildef img2label(imgPath, labelPath, imgbjPath, seletName):# 检查labelPath文件夹是否存在if not os.path.exists(labelPath):os.makedirs(labelPath)if not os.path.exists(imgbjPath):os.makedirs(imgbjPath)imgList = os.listdir(imgPath)for imgName in imgList:# 筛选if imgName.split('_')[0] != seletName and seletName != '':continueprint(imgName)img = cv2.imread(imgPath + imgName, cv2.IMREAD_COLOR)h, w, _ = img.shape# print(h, w)GrayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #图片灰度化处理ret, binary = cv2.threshold(GrayImage,40,255,cv2.THRESH_BINARY) #图片二值化,灰度值大于40赋值255,反之0# ret, binary = cv2.threshold(binary, 80, 255, cv2.THRESH_BINARY_INV)    # (黑白二值反转)cv2.imwrite(r'denoisedfz.png', binary) #保存图片# 腐蚀# kernel = np.ones((3,3),np.uint8) # binary = cv2.erode(binary,kernel,iterations = 3)thresholdL = h/100 * w/100   #设定阈值thresholdH = h/1 * w/1   #设定阈值#cv2.fingContours寻找图片轮廓信息"""提取二值化后图片中的轮廓信息 ,返回值contours存储的即是图片中的轮廓信息,是一个向量,内每个元素保存了一组由连续的Point点构成的点的集合的向量,每一组Point点集就是一个轮廓,有多少轮廓,向量contours就有多少元素"""contours,hierarch=cv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_TC89_L1)contoursNorm = []objs= []# print(contours)for i in range(len(contours)):area = cv2.contourArea(contours[i]) #计算轮廓所占面积# print(area)if area > thresholdL and area < thresholdH:obj = ['0']for point in contours[i]:obj.append(str(point[0][0] * 1.0 / w)) # 获取xobj.append(str(point[0][1] * 1.0 / h)) # 获取ycontoursNorm.append(contours[i])objs.append(obj)# print(objs[10])# 查看效果cv2.drawContours(img, contoursNorm, -1,(255,0,0),2)cv2.imwrite(imgbjPath+imgName, img) #保存图片if len(objs) == 0:print('不保存标签,跳过!')continue# 写入txtrealName = imgName.split('-l')[0]f=open(labelPath + realName + '.txt',"w")for obj in objs:f.writelines(' '.join(obj))f.writelines('\n')f.close()# break# oridata 保存着原图像
# maskdata 保存着标签图像
# lab 保存这yolo格式的标签文件
# bj 保存着标记好边界的图像def OrganizeImages(path):imgs = os.listdir(path)for im in imgs:imPath = os.path.join(path, im)if im.split('.')[-1] == 'jpg':# 原图像# 移动到oridatasource_path = imPathdestination_path = 'data\\oridata\\' + imshutil.copy(source_path, destination_path)if im.split('.')[-1] == 'png':# mask label# 移动到maskdatasource_path = imPathdestination_path = 'data\\maskdata\\' + imshutil.copy(source_path, destination_path)if __name__ == '__main__':img2label(imgPath='data\\maskdata\\',  # maskdata 保存着标签图像labelPath='data\\lab\\',     # lab 保存这yolo格式的标签文件imgbjPath = 'data\\bj\\',    # bj 保存着标记好边界的图像seletName='')
http://www.lryc.cn/news/430885.html

相关文章:

  • QT 遍历ini配置文件
  • ecmascript和javascript的区别详细讲解
  • 【Python报错已解决】“ModuleNotFoundError: No module named ‘timm‘”
  • 「图::存储」链式邻接表|链式前向星(C++)
  • 《Cloud Native Data Center Networking》(云原生数据中心网络设计)读书笔记 -- 10数据中心中的BGP
  • unity游戏开发——标记物体 一目了然
  • vue 项目打包图片没有打包进去问题解决
  • TCP的传输速度
  • 直播间的“骆驼”比沙漠还多?刀郎演唱会惊现“骆驼”
  • Android Studio gradle下载太慢了!怎么办?(已解决)
  • 安卓版Infuse来了 打造自己的影视墙
  • 【Python时序预测系列】高创新模型:基于xlstm模型实现单变量时间序列预测(案例+源码)
  • Ubuntu 22.04 系统中 ROS2安装
  • Vue内置指令v-once、v-memo和v-pre提升性能?
  • OpenHarmony轻松玩转GIF数据渲染
  • torch.clip函数介绍
  • 西北工业大学oj题-兔子生崽
  • 【Go语言成长之路】 模糊测试
  • 异或运算的高级应用和Briankernighan算法
  • 音视频入门基础:WAV专题(9)——FFmpeg源码中计算WAV音频文件每个packet的duration和duration_time的实现
  • AI写的论文查重率高吗?分享6款实测AI论文生成免费网站
  • 【专题】2024年8月中国企业跨境、出海、国际化、全球化行业报告汇总PDF合集分享(附原数据表)
  • [算法]单调栈解法
  • 构建数据安全防线:MySQL数据备份策略的文档化实践
  • 4. GIS前端工程师岗位职责、技术要求和常见面试题
  • 软件测试-Selenium+python自动化测试
  • SpringBoot与Minio的极速之旅:解锁文件切片上传新境界
  • Java 7.3 - 分布式 id
  • 144. 腾讯云Redis数据库
  • 基于单片机的自动浇花控制写设计任务书