语义分割,将RGB三通道的lable转为单通道

由于模型的label为单通道的图,需要进行颜色转换,参考了

csdn_label2color2label: 语义分割,灰度图与彩色图的相互转换

进行修改

import numpy as np
import os
import cv2
import time


def color2gray(img_path, color_map):
    # 读取图片
    color_img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
    # 计算时间
    t0 = time.time()
    gray_img = np.zeros(shape=(color_img.shape[0], color_img.shape[1]), dtype=np.uint8)
    for i in range(color_map.shape[0]):
        index = np.where(np.all(color_img == color_map[i], axis=-1))  # np.all true false
        gray_img[index] = i
    t1 = time.time()
    time_cost = round(t1 - t0, 3)
    print(f"color2label  cost time {time_cost}")
    # 保存图片
    dir, name = os.path.split(img_path)
    save_dir='./gray_label'
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)
    save_path = os.path.join(save_dir, name)
    cv2.imwrite(save_path, gray_img)

if __name__ == '__main__':
    # 你的colormap  注意:这个是BGR的!!!!!!很重要
    cmap = np.array(
        [
            (0, 0, 0),
            (108, 64, 20),
            (255, 229, 204),
            (0, 102, 0),
            (0, 255, 0),
            (0, 153, 153),
            (0, 128, 255),
            (0, 0, 255),
            (255, 255, 0),
            (255, 0, 127),
            (64, 64, 64),
            (255, 128, 0),
            (255, 0, 0),
            (153, 76, 0),
            (102, 102, 0),
            (102, 0, 0),
            (0, 255, 128),
            (204, 153, 255),
            (102, 0, 204),
            (255, 153, 204),
            (0, 102, 102),
            (153, 204, 255),
            (102, 255, 255),
            (101, 101, 11),
            (114, 85, 47)
        ]
    )
    # 文件路径
    img_dir = './label/'
    if not os.path.exists(img_dir):
        os.mkdir(img_dir)
    for img in os.listdir(img_dir):
        if not img.endswith((".png", ".jpg")):
            continue
        img_path = os.path.join(img_dir, img)
        color2gray(img_path, color_map=cmap)

color_map为你的颜色映射,输入文件夹路径就可以使用了

效果为

  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要将 JLabel 改为圆角,可以通过设置其 Border 来实现。以下是一个示例代码: ``` import javax.swing.*; import java.awt.*; public class RoundedLabel extends JLabel { private int radius; private Color backgroundColor; private Color foregroundColor; public RoundedLabel(String text, int radius, Color backgroundColor, Color foregroundColor) { super(text); this.radius = radius; this.backgroundColor = backgroundColor; this.foregroundColor = foregroundColor; } @Override protected void paintComponent(Graphics g) { int width = getWidth(); int height = getHeight(); Graphics2D graphics = (Graphics2D) g; graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setColor(backgroundColor); graphics.fillRoundRect(0, 0, width, height, radius, radius); graphics.setColor(foregroundColor); super.paintComponent(graphics); } @Override public Dimension getPreferredSize() { Dimension preferredSize = super.getPreferredSize(); int width = preferredSize.width + radius; int height = preferredSize.height + radius; return new Dimension(width, height); } public static void main(String[] args) { JFrame frame = new JFrame(); RoundedLabel label = new RoundedLabel("Hello, World!", 10, Color.BLUE, Color.WHITE); label.setHorizontalAlignment(SwingConstants.CENTER); frame.add(label); frame.setSize(200, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } ``` 在上面的代码中,我们创建了一个继承自 JLabel 的 RoundedLabel 类,它具有圆角边框和背景色。我们重写了 paintComponent 和 getPreferredSize 方法来绘制圆角边框和设置首选大小。在 main 方法中,我们创建了一个 JFrame 并添加了一个 RoundedLabel 实例。 你可以根据需要调整 radius、backgroundColor 和 foregroundColor 参数来自定义圆角标签的外观。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值