最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用Python實現(xiàn)生成顏色表(color chart)

 更新時間:2023年05月10日 09:14:16   作者:拜陽  
在做色彩相關(guān)的算法分析時候,經(jīng)常需要使用規(guī)則的顏色表來進行輔助,本文就來利用numpy和opencv生成顏色表并保存為圖片,需要的可以參考一下

前言

在做色彩相關(guān)的算法分析時候,經(jīng)常需要使用規(guī)則的顏色表來進行輔助。下面用python(numpy和opencv)來生成顏色表并保存為圖片。

有兩種類型:

  • 格子形狀的顏色表
  • 漸變色帶

長的樣子分別如下:

格子顏色表

這里需要注意,當劃分的顏色數(shù)量比較少時,最好把一個顏色像素擴展成為一個格子,不然的話整個圖看起來就太小了。

# -*- coding: utf-8 -*-
import cv2
import numpy as np


def generate_color_chart(block_num=18,
                         block_columns=6,
                         grid_width=32,
                         grid_height=None):
    """
    Generate color chart by uniformly distributed color indexes, only support
    8 bit (uint8).

    Parameters
    ----------
    block_num: Block number of color chart, also the number of color indexes.
    block_columns: Column number of color chart. Row number is computed by
        block_num / block_columns
    grid_width: Width of color grid
    grid_height: Height of color grid. If not set, it will equal to grid_width.
    """
    color_index = np.linspace(0, 255, block_num)
    color_index = np.uint8(np.round(color_index))

    if grid_height is None:
        grid_height = grid_width

    # compute sizes
    block_rows = np.int_(np.ceil(block_num / block_columns))
    block_width = grid_width * block_num
    block_height = grid_height * block_num
    width = block_width * block_columns
    height = block_height * block_rows
    result = np.zeros((height, width, 3), dtype=np.uint8)

    # compute red-green block, (blue will be combined afterward)
    red_block, green_block = np.meshgrid(color_index, color_index)
    red_block = expand_pixel_to_grid(red_block, grid_width, grid_height)
    green_block = expand_pixel_to_grid(green_block, grid_width, grid_height)
    rg_block = np.concatenate([red_block, green_block], axis=2)

    # combine blue channel
    for i in range(block_num):
        blue = np.ones_like(rg_block[..., 0], dtype=np.uint8) * color_index[i]
        color_block = np.concatenate([rg_block, blue[..., np.newaxis]], axis=2)
        # compute block index
        block_row = i // block_columns
        block_column = i % block_columns
        xmin = block_column * block_width
        ymin = block_row * block_height
        xmax = xmin + block_width
        ymax = ymin + block_height
        result[ymin:ymax, xmin:xmax, :] = color_block

    result = result[..., ::-1]  # convert from rgb to bgr
    return result


def expand_pixel_to_grid(matrix, grid_width, grid_height):
    """
    Expand a pixel to a grid. Inside the grid, every pixel have the same value
    as the source pixel.

    Parameters
    ----------
    matrix: 2D numpy array
    grid_width: width of grid
    grid_height: height of grid
    """
    height, width = matrix.shape[:2]
    new_heigt = height * grid_height
    new_width = width * grid_width
    repeat_num = grid_width * grid_height

    matrix = np.expand_dims(matrix, axis=2).repeat(repeat_num, axis=2)
    matrix = np.reshape(matrix, (height, width, grid_height, grid_width))
    # put `height` and `grid_height` axes together;
    # put `width` and `grid_width` axes together.
    matrix = np.transpose(matrix, (0, 2, 1, 3))
    matrix = np.reshape(matrix, (new_heigt, new_width, 1))
    return matrix


if __name__ == '__main__':
    color_chart16 = generate_color_chart(block_num=16,
                                         grid_width=32,
                                         block_columns=4)
    color_chart18 = generate_color_chart(block_num=18,
                                         grid_width=32,
                                         block_columns=6)
    color_chart36 = generate_color_chart(block_num=36,
                                         grid_width=16,
                                         block_columns=6)
    color_chart52 = generate_color_chart(block_num=52,
                                         grid_width=8,
                                         block_columns=13)
    color_chart256 = generate_color_chart(block_num=256,
                                          grid_width=1,
                                          block_columns=16)

    cv2.imwrite('color_chart16.png', color_chart16)
    cv2.imwrite('color_chart18.png', color_chart18)
    cv2.imwrite('color_chart36.png', color_chart36)
    cv2.imwrite('color_chart52.png', color_chart52)
    cv2.imwrite('color_chart256.png', color_chart256)

漸變色帶

# -*- coding: utf-8 -*-
import cv2
import numpy as np


def generate_color_band(left_colors, right_colors, grade=256, height=32):
    """
    Generate color bands by uniformly changing from left colors to right
    colors. Note that there might be multiple bands.

    Parameters
    ----------
    left_colors: Left colors of the color bands.
    right_colors: Right colors of the color bands.
    grade: how many colors are contained in one color band.
    height: height of one color band.
    """
    # check and process color parameters, which should be 2D list
    # after processing
    if not isinstance(left_colors, (tuple, list)):
        left_colors = [left_colors]
    if not isinstance(right_colors, (tuple, list)):
        right_colors = [right_colors]

    if not isinstance(left_colors[0], (tuple, list)):
        left_colors = [left_colors]
    if not isinstance(right_colors[0], (tuple, list)):
        right_colors = [right_colors]

    # initialize channel, and all other colors should have the same channel
    channel = len(left_colors[0])

    band_num = len(left_colors)
    result = []
    for i in range(band_num):
        left_color = left_colors[i]
        right_color = right_colors[i]
        if len(left_color) != channel or len(right_color) != channel:
            raise ValueError("All colors should have same channel number")

        color_band = np.linspace(left_color, right_color, grade)
        color_band = np.expand_dims(color_band, axis=0)
        color_band = np.repeat(color_band, repeats=height, axis=0)
        color_band = np.clip(np.round(color_band), 0, 255).astype(np.uint8)
        result.append(color_band)
    result = np.concatenate(result, axis=0)
    result = np.squeeze(result)
    return result


if __name__ == '__main__':
    black = [0, 0, 0]
    white = [255, 255, 255]
    red = [0, 0, 255]
    green = [0, 255, 0]
    blue = [255, 0, 0]

    gray_band = generate_color_band([[0], [255]], [[255], [0]])
    color_band8 = generate_color_band(
        [black, white, red, green, blue, black, black, black],
        [white, black, white, white, white, red, green, blue]
    )

    cv2.imwrite('gray_band.png', gray_band)
    cv2.imwrite('color_band8.png', color_band8)

到此這篇關(guān)于利用Python實現(xiàn)生成顏色表(color chart)的文章就介紹到這了,更多相關(guān)Python顏色表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于Python編寫個語法解析器

    基于Python編寫個語法解析器

    這篇文章主要為大家詳細介紹了如何基于Python編寫個語法解析器,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解一下
    2023-07-07
  • python實現(xiàn)RSA加密(解密)算法

    python實現(xiàn)RSA加密(解密)算法

    RSA是目前最有影響力的公鑰加密算法,它能夠抵抗到目前為止已知的絕大多數(shù)密碼攻擊,已被ISO推薦為公鑰數(shù)據(jù)加密標準,下面通過本文給大家介紹python實現(xiàn)RSA加密(解密)算法,需要的朋友參考下
    2016-02-02
  • Python?Pandas讀取csv/tsv文件(read_csv,read_table)的區(qū)別

    Python?Pandas讀取csv/tsv文件(read_csv,read_table)的區(qū)別

    這篇文章主要給大家介紹了關(guān)于Python?Pandas讀取csv/tsv文件(read_csv,read_table)區(qū)別的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家學習或者使用Pandas具有一定的參考學習價值,需要的朋友可以參考下
    2022-01-01
  • Python數(shù)據(jù)結(jié)構(gòu)與算法之列表(鏈表,linked list)簡單實現(xiàn)

    Python數(shù)據(jù)結(jié)構(gòu)與算法之列表(鏈表,linked list)簡單實現(xiàn)

    這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法之列表(鏈表,linked list)簡單實現(xiàn),具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • 在Python的while循環(huán)中使用else以及循環(huán)嵌套的用法

    在Python的while循環(huán)中使用else以及循環(huán)嵌套的用法

    這篇文章主要介紹了在Python的while循環(huán)中使用else以及循環(huán)嵌套的用法,是Python入門學習中的基礎知識,需要的朋友可以參考下
    2015-10-10
  • Python面向?qū)ο笕筇卣?封裝、繼承、多態(tài)

    Python面向?qū)ο笕筇卣?封裝、繼承、多態(tài)

    這篇文章主要介紹了Python面向?qū)ο笕筇卣?封裝、繼承、多態(tài),下面文章圍繞Python面向?qū)ο笕筇卣鞯南嚓P(guān)資料展開具體內(nèi)容,需要的朋友可以參考一下,希望對大家有所幫助
    2021-11-11
  • PyTorch和Keras計算模型參數(shù)的例子

    PyTorch和Keras計算模型參數(shù)的例子

    今天小編就為大家分享一篇PyTorch和Keras計算模型參數(shù)的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • python數(shù)據(jù)解析之XPath詳解

    python數(shù)據(jù)解析之XPath詳解

    本篇文章主要介紹了python數(shù)據(jù)解析之xpath的基本使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-09-09
  • Python解析CDD文件的代碼詳解

    Python解析CDD文件的代碼詳解

    這篇文章主要介紹了Python解析CDD文件的方法,使用Python 腳本解析CDD文件,統(tǒng)一定義,一鍵生成,十分快捷,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • Python生成隨機驗證碼代碼實例解析

    Python生成隨機驗證碼代碼實例解析

    這篇文章主要介紹了Python生成隨機驗證碼代碼實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06

最新評論

陵水| 正宁县| 台安县| 屯留县| 丰顺县| 沙坪坝区| 界首市| 南宫市| 邵阳县| 天气| 启东市| 黄冈市| 庆城县| 镇安县| 股票| 青龙| 鄂伦春自治旗| 峨眉山市| 昌图县| 平湖市| 延吉市| 周口市| 临邑县| 建始县| 沅江市| 太仆寺旗| 化隆| 汾西县| 建水县| 崇左市| 杨浦区| 贵南县| 黎平县| 勃利县| 吴旗县| 安远县| 广河县| 麻城市| 乌兰县| 调兵山市| 济源市|