首页 > 其他分享 >yolov8实例分割每个物体不同的灰度图

yolov8实例分割每个物体不同的灰度图

时间:2024-05-16 10:08:18浏览次数:21  
标签:Particle mask Primary yolov8 colors 实例 灰度 np size

预测代码

点击查看代码
import os
from ultralytics import YOLO
import numpy as np
import cv2
def generate_colors_by_classes_size(classes_size=100):
    # colors = np.zeros((classes_size, 3), dtype=np.uint8)
    colors = np.zeros((classes_size, 3), dtype=np.uint16)
    for i in range(classes_size):
        # color=((i+1)* (255 / classes_size)) % 256
        color=((i+1)* (19999 / classes_size)) % 20000
        colors[i] = color
    return colors
def gen_gray_mask(results,colors,outdir='./out_mask'):
     for result in results:
            input_image = cv2.imread(result.path)
            h,w,c=input_image.shape
            # mask= np.zeros((h, w), dtype=np.uint8) 
            mask= np.zeros((h, w), dtype=np.uint16)   
            labels=result.boxes.cls.tolist()
            # 每张图的mask
            i=0
            for mask_xy in result.masks.xy:
                pixel_xy0 = result.masks.xy[0]
                pixel_xy = mask_xy
                points = np.array(pixel_xy, np.int32)
                if(points.size==0):
                    continue
                # print(points)
                try:
                    # cv2.drawContours(input_image, [points], -1, (0, 255, 0), 2) tuple(arr.tolist())
                    label_index=int(labels[i])
                    clolor=tuple(colors[i].tolist()) #每个实例不同颜色
                    # clolor=tuple(colors[label_index].tolist())#按照类别着色
                    # cv2.drawContours(input_image, [points], -1, clolor, 1)
                    cv2.fillPoly(mask, [points], clolor)
                    i=i+1
                except Exception as e:
                    print(e)
                    break
            basename=os.path.basename(result.path)
            outname=os.path.join(outdir,basename)
            cv2.imwrite(outname, mask)
         
if __name__=='__main__':

        # names=["Primary_Particle_Z","Primary_Particle_X","Primary_Particle_B","Primary_Particle_K","Primary_Particle_C","Primary_Particle5",
        #        "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"
        #        ]
        names=["Micropowder","SQ","Primary_Particle_B","Primary_Particle_K","Primary_Particle_C","Primary_Particle5",
               "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"
               ]

        # colors=generate_random_colors()
        num_class=9999
        colors=generate_colors_by_classes_size(num_class)
        clolor=tuple(colors[0].tolist())
        model = YOLO(r'D:\gzj\PycharmWorkSpace\ultralytics-main\ultralytics-main\yolov8s_wf_big_small4pic.pt')
        results=model.predict(r'D:\gzj\PycharmWorkSpace\ultralytics-main\ultralytics-main\Test\img\001_1.jpg', save=True,save_txt=True, imgsz=640,line_width=2)
        outpath='./out_mask0-1'
        os.makedirs(outpath,exist_ok=True)
        gen_gray_mask(results=results,colors=colors,outdir=outpath) 
        print("done")
原图

灰度图

标签:Particle,mask,Primary,yolov8,colors,实例,灰度,np,size
From: https://www.cnblogs.com/SunshineWeather/p/18195416

相关文章

  • vue监听事件实例
    <!DOCTYPEhtml><html><head><metacharset="utf-8"><title>Vue测试实例-菜鸟教程(runoob.com)</title><scriptsrc="https://cdn.staticfile.net/vue/2.4.2/vue.min.js"></script></he......
  • Ubuntu 实例桌面版
    基于ubuntu18.04安装的桌面版本,桌面使用xfce4,集成了Pytorch2.3.0、cuda11.8、Python3.10、VNC、noVNC、VSCode-Server。立即免费体验:https://gpumall.com/login?type=register&source=cnblogs在镜像市场选择xfce4-desktop镜像,然后进行创建实例#autodl#恒源云#矩池云#算......
  • 实例网盘(强推)
    实例网盘为实例的/gm-fs目录,该目录为实例同一个数据中心的分布式存储,对于较大的文件或者压缩文件有着出色的读写性能,实例网盘不受实例删除/释放影响,采用分布式冗余存储,数据安全性较高,强烈建议使用网盘备份数据。立即免费体验:https://gpumall.com/login?type=register&source=cn......
  • 【图像处理】使用matplotlib库显示灰度图像为自定义颜色(2)
    在下面的代码中,facies_img的值只有[0,1,2]表明图像是灰度图像。通过下面的代码可以让图像显示为彩色图像importmatplotlib.pyplotaspltimportmatplotlib.colorsasmcolorsfromPILimportImageimportosimportrandomimportnumpyasnp#路径设置data_dir='data......
  • 实例内使用百度网盘
    在GpuMall平台的实例中,可以通过使用baidupcs命令工具来实现对个人百度网盘账号中的数据上传及下载操作,具体操作方法如下:立即免费体验:https://gpumall.com/login?type=register&source=cnblogs备注下载baidupcs命令工具到实例中获取BDUSS和STOKEN通过BDUSS和STOKE......
  • 实例内使用阿里云盘
    在GpuMall平台的实例中,可以通过使用aliyunpan命令工具来实现对个人阿里云盘账号中的数据上传及下载操作,具体操作方法如下:立即免费体验:https://gpumall.com/login?type=register&source=cnblogs备注下载aliyunpan命令工具到实例中在实例中登录aliyunpan在实例中下载阿......
  • 公网访问实例内服务
    在GpuMall平台使用该功能可以帮您把实例内的服务暴露到公网进行访问,该服务可以是Web项目、API接口、StableDiffusionwebUI、以及任何监听了HTTP、TCP端口类型的进程。立即免费体验:https://gpumall.com/login?type=register&source=cnblogs使用该功能注意事项严禁通过该......
  • 实例启动时拉起任务
    在GpuMall平台使用该功能可以在实例启动的同时自动帮您拉起进程或者任务,该功能主要适用于以下场景:立即免费体验:https://gpumall.com/login?type=register&source=cnblogs实例开机即启动您需要的进程。推理任务,比如您需要扩容某个功能节点,只需要创建实例即可自动拉起您的推理......
  • 本地SSH方式连接实例
    通过SSH登录GPUMALL实例介绍通过SSH方式连接到Linux服务器的方法有多种,这里介绍几种常用的SSH远程登录工具,只需要使用其中一种可以登录到GpuMall实例即可。立即免费体验:https://gpumall.com/login?type=register&source=cnblogsWindows系统可以使用:XShell、Mobaxterm、......
  • 实例后台运行训练或任务
    后台运行进程在正常情况下,使用命令pythontrain.py运行机器学习的训练或推理任务时,该进程会挂载到系统的前台,这意味着如果您通过SSH连接到远程实例进行操作,一旦SSH连接因网络延迟或波动等原因中断,与SSH会话关联的前台进程(包括您的训练任务)也将被终止,这会导致您失去所有未保存的......