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))
# combine blue channel
for i in range(block_num):
blue = np.ones_like(rg_block[..., 0], dtype=np.uint8) * color_index
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.
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
right_color = right_colors
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]