• 灰度标签图显示查看,加颜色显示


    example_1

    import numpy as np
    import cv2
    import os
    color_segmentation=np.asarray([
        [0,0,0],            #[0]背景
        [180,120,120],
        [6,230,230],
        [80,50,50],
        [4,200,3],
        [120,120,80],
        [140,140,140],
        [204,5,255],
        [230,230,230],
        [4,250,7],
        [40,150,255],
        [235,255,7],
        [150,5,61],
        [120,120,70],
        [8,255,51],
        [255,6,82],
        [143,255,140],
        [204,255,4],
        [255,51,7],
        [204,70,3],
        [0,102,200],
        [61,230,250],
        [255,6,51],
        [11,102,255],
        [255,7,71],
        [255,9,224],
        [9,7,230],
        [220,220,220],
        [255, 9, 92]
    ],dtype=np.uint8)
    
    
    def decode_segmap(label_mask,n_classes = 21):
        r = label_mask.copy()
        g = label_mask.copy()
        b = label_mask.copy()
        for ll in range(0, n_classes):
            position = label_mask == ll
            b[label_mask == ll] = color_segmentation[ll, 0]
            g[label_mask == ll] = color_segmentation[ll, 1]
            r[label_mask == ll] = color_segmentation[ll, 2]
        rgb = np.zeros((label_mask.shape[0], label_mask.shape[1], 3))
    
        rgb[:, :, 0] = b
        rgb[:, :, 1] = g
        rgb[:, :, 2] = r
        rgb = rgb.astype(np.uint8)  ##重要! 要不然opencv显示有问题
        return rgb
    
    
    root_dir = "./voc/VOCdevkit/VOC2012/SegmentationClassAug/"
    list_gray_img = os.listdir(root_dir)
    for img_name in list_gray_img:
        path_gray = root_dir + img_name
        laber_mask = cv2.imread(path_gray,0) ##灰度 单通道读取
        # print(laber_mask.shape)
        color_img = decode_segmap(laber_mask)
    
        cv2.imshow("laber_mask", laber_mask*20)
        cv2.imshow("color",color_img)
        cv2.waitKey()
    

    example_2

    import numpy as np
    import cv2
    
    def generate_colors(_color_num):
        """
        生成一定数量的颜色
        Args:
            _color_num: 颜色的数量
        Returns:    所有生成的颜色
        """
        to_return_palette = []
        import colorsys
    
        # 最多50种颜色
        for i in range(_color_num):
            hue_value = (i % 50) * 0.02
            (r, g, b) = colorsys.hsv_to_rgb(hue_value, 1, 1)
            to_return_palette.append([int(b * 255), int(g * 255), int(r * 255)])
        return to_return_palette
    
    
    
    def annotate_segmentation(
            _to_draw_image,
            _segmentation_result,
            _background_index=0,
    ):
        """
        标注分割区域
        Args:
            _to_draw_image:     需要标注的图像
            _segmentation_result:   分割的结果
            _background_index:  背景部分的下标
        Returns:    标注完成的图
        """
        h, w = _to_draw_image.shape[:2]
        if _to_draw_image.shape[:2] != _segmentation_result.shape[:2]:
            _segmentation_result = cv2.resize(_segmentation_result, (w, h), cv2.INTER_NEAREST)
        distinct_index = np.sort(np.unique(_segmentation_result), axis=None)
        candidate_colors = generate_colors(len(distinct_index))
        mask_result_image = _to_draw_image.copy()
        for m_index, m_candidate_color in zip(distinct_index.tolist(), candidate_colors):
            if m_index == _background_index:
                continue
            m_index_segment_result = _segmentation_result == m_index
            np.putmask(mask_result_image, np.repeat(m_index_segment_result[..., None], 3, 
    axis=-1), m_candidate_color)
        add_weighted_result_image = cv2.addWeighted(_to_draw_image, 0.5, mask_result_image, 0.5, 0)
        return add_weighted_result_image
    
    
    path_img_src = "/src_jj.png"
    img_gray_src = cv2.imread(path_img_src)
    
    path_img = "./gray_jj.png"
    img_gray = cv2.imread(path_img)
    img_gray2 = cv2.cvtColor(img_gray,cv2.COLOR_BGR2GRAY)
    h,w = img_gray2.shape
    img_color = np.zeros([h, w, 3], np.uint8)
    img_color_new = annotate_segmentation(img_gray_src,img_gray2)
    
    cv2.imshow("img_color_new",img_color_new)
    cv2.waitKey()
    

    example2是把标注颜色打在原图上面查看的,自动生成颜色表。

    好记性不如烂键盘---点滴、积累、进步!
  • 相关阅读:
    Bootstrap表单验证插件bootstrapValidator使用方法整理
    bootStrap树形目录组件
    根据对象的某一属性进行排序的js代码(如:name,age)
    Ajax跨域:jsonp还是CORS
    用border-image实现波浪边框
    Ajax加载菊花loding效果
    写一个js向左滑动删除 交互特效的插件——Html5 touchmove
    js抛物线动画——加入购物车动效
    zepto弹出层组件
    原生JS实现购物车结算功能代码+zepto版
  • 原文地址:https://www.cnblogs.com/yanghailin/p/14763707.html
Copyright © 2020-2023  润新知