asdTools-彩色分割GT为原图上色

该文章介绍了一个Python脚本,用于根据彩色分割GroundTruth(GT)为原始RGB图像上色。脚本从指定目录读取图片和对应的分割GT,然后将GT的颜色应用到原图上,产生结合了原始色彩和分割信息的新图像。用户可以找到示例代码在SampleColorImgWithGT文件夹中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章首发及后续更新:https://mwhls.top/4684.html,无图/无目录/格式错误/更多相关请至首发页查看。
新的更新内容请到mwhls.top查看。
欢迎提出任何疑问及批评,非常感谢!

Github – 开源代码及Readme
Blog – 工具介绍

摘要:使用彩色分割GT为原图上色。

场景
  • 使用彩色的分割GT,为原始RGB图上色。
使用示例
from asdTools.Classes.Image.ImageBase import ImageBase

class ColorImgWithGT(ImageBase):
    """
    使用彩色分割GT为原图上色。示例见 Sample\ColorImgWithGT 
    注:可使用asdTools\Tools\Image\ColorGT.py为GT上色
    Colorize the image with the colorful segmentation GT. Examples can be found in the "Sample\ColorImgWithGT" folder.
    Note: You can use the "asdTools\Tools\Image\ColorGT.py" script to colorize the GT. 
    """
    def __init__(self, **kwargs) -> None:
        super().__init__(multipleFiles=True, **kwargs)

    def __call__(self, imgs_dir:str, GTs_dir:str) -> str:
        self.run(imgs_dir, GTs_dir)

    def map_paths_to_fullPaths(self, files_dir:str, files_path:list):
        res = {}
        for file_path in files_path:
            fullPath = file_path
            path = fullPath[len(files_dir)+1:]
            res[path] = fullPath
        return res

    def run(self, imgs_dir:str, GTs_dir:str) -> str:
        imgs_path = self.get_paths_from_dir(imgs_dir)
        imgs_path = self.filter_ext(imgs_path, ["png", "jpg", "jpeg"])
        imgs_path_mapping = self.map_paths_to_fullPaths(imgs_dir, imgs_path)
        GTs_path = self.get_paths_from_dir(GTs_dir)
        GTs_path = self.filter_ext(GTs_path, ["png", "jpg", "jpeg"])
        GTs_path_mapping = self.map_paths_to_fullPaths(GTs_dir, GTs_path)
        for i, (img_path, img_fullPath) in enumerate(imgs_path_mapping.items()):
            try:
                GT_fullPath = GTs_path_mapping[img_path]
            except:
                self.warning(f"{i+1}: GT is not found: {img_path}")
                continue
            img_res = self.blend(img_fullPath, GT_fullPath, 0.25)
            img_dir = self.get_dir_of_file(img_path)
            img_name = self.get_name_of_file(img_path, True)
            self.save_image(img_res, output_middle_dir=img_dir, output_file=img_name)
            self.log(f"{i+1}: Img has been colored: {img_path}")
        self.done()

if __name__ == "__main__":
    imgs_dir = r"Sample\ColorImgWithGT\Before\Images"
    GTs_dir = r"Sample\ColorImgWithGT\Before\Annotations"
    Converter = ColorImgWithGT()
    Converter(imgs_dir, GTs_dir)
    ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值