zoukankan      html  css  js  c++  java
  • 灰度标签图显示查看,加颜色显示

    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是把标注颜色打在原图上面查看的,自动生成颜色表。

    好记性不如烂键盘---点滴、积累、进步!
  • 相关阅读:
    Azure PowerShell (7) 使用CSV文件批量设置Virtual Machine Endpoint
    Windows Azure Cloud Service (39) 如何将现有Web应用迁移到Azure PaaS平台
    Azure China (7) 使用WebMetrix将Web Site发布至Azure China
    Microsoft Azure News(4) Azure新D系列虚拟机上线
    Windows Azure Cloud Service (38) 微软IaaS与PaaS比较
    Windows Azure Cloud Service (37) 浅谈Cloud Service
    Azure PowerShell (6) 设置单个Virtual Machine Endpoint
    Azure PowerShell (5) 使用Azure PowerShell创建简单的Azure虚拟机和Linux虚拟机
    功能代码(1)---通过Jquery来处理复选框
    案例1.用Ajax实现用户名的校验
  • 原文地址:https://www.cnblogs.com/yanghailin/p/14763707.html
Copyright © 2011-2022 走看看