自学内容网 自学内容网

可视化系列:绘制n*n的宫格图

可视化系列:绘制n*n的宫格图

示例代码

import cv2
import numpy as np

def draw_grid(
    grid_num, 
    fill_color_dict,
    grid_size=100, 
    line_width=1,
    line_color=(0, 0, 0),
):
    """
    绘制一个n*n的宫格图,可以指定某些宫格填充的颜色。
    
    :param grid_num: 宫格的数量,grid_num*grid_num
    :param fill_color_dict: 一个字典,键为宫格的位置(例如(0, 0)表示左上角),值为填充的颜色(BGR格式)
    :param grid_size: 宫格的大小,默认为100
    :param line_width: 宫格线的宽度,默认为1
    :param line_color: 宫格线的颜色,默认为黑色
    :return: 一个n*n的宫格图,某些宫格被填充了指定的颜色
    """
    height = width = grid_num * grid_size + (grid_num+1)*line_width
    image = np.ones((height, width, 3), dtype=np.uint8)*255
    interval_len = grid_size+1
    # # 绘制宫格线
    for i in range(grid_num+1):
        # 0, 101, 202, 303
        y = i * interval_len
        # print(f"==>> y: {y}")
        cv2.line(
            image, 
            (0, y), 
            (width, y), 
            line_color, 
            line_width
        )  # 水平线
        cv2.line(
            image, 
            (y, 0), 
            (y, height), 
            line_color, 
            line_width
        )  # 垂直线
    


    # # 填充指定的宫格
    if fill_color_dict:
        for (x, y), color in fill_color_dict.items():
            y_min = y * interval_len + line_width
            y_max = y_min + grid_size
            x_min = x * interval_len + line_width
            x_max = x_min + grid_size

            # print(f"==>> y_min: {y_min}")
            # print(f"==>> y_max: {y_max}")
            # print(f"==>> x_min: {x_min}")
            # print(f"==>> x_max: {x_max}")
            # print("\n")

            image[
                y_min: y_max, 
                x_min: x_max,
            ] = color


    return image

if __name__ == "__main__":
    # 使用示例
    grid_num = 3  # 宫格尺寸
    # 4领域法示例
    # fill_color_dict = {
    #     # (w, h)
    #     (1, 1): (0, 0, 0),  # 黑色
    #     (0, 1): (128, 128, 128),  # 灰色
    #     (2, 1): (128, 128, 128),  # 灰色
    #     (1, 0): (128, 128, 128),  # 灰色
    #     (1, 2): (128, 128, 128),  # 灰色
    # }
    # image_name_prefix = "grid_4_domain"

    # 8领域法示例
    # fill_color_dict = {
    #     # (w, h)
    #     (1, 1): (0, 0, 0),  # 黑色
    #     (0, 0): (128, 128, 128),  # 灰色
    #     (1, 0): (128, 128, 128),  # 灰色
    #     (0, 1): (128, 128, 128),  # 灰色
    #     (2, 0): (128, 128, 128),  # 灰色
    #     (0, 2): (128, 128, 128),  # 灰色
    #     (2, 2): (128, 128, 128),  # 灰色
    #     (2, 1): (128, 128, 128),  # 灰色
    #     (1, 2): (128, 128, 128),  # 灰色
    # }
    # image_name_prefix = "grid_8_domain"
    # image_visual_path = f"./image_patent/{image_name_prefix}.png"

    # T形示例
    # fill_color_dict = {
    #     # (w, h)
    #     (1, 1): (0, 0, 0),  # 黑色
    #     (0, 1): (128, 128, 128),  # 灰色
    #     (2, 1): (128, 128, 128),  # 灰色
    #     (1, 2): (128, 128, 128),  # 灰色
    # }
    # image_name_prefix = "grid_8_domain_T"
    # image_visual_path = f"./image_patent/{image_name_prefix}.png"

    # 45度的T形示例
    # fill_color_dict = {
    #     # (w, h)
    #     (1, 1): (0, 0, 0),  # 黑色
    #     (2, 0): (128, 128, 128),  # 灰色
    #     (0, 2): (128, 128, 128),  # 灰色
    #     (2, 2): (128, 128, 128),  # 灰色
    # }
    # image_name_prefix = "grid_8_domain_T_45_degree"
    # image_visual_path = f"./image_patent/{image_name_prefix}.png"

    # # Y1形示例
    # fill_color_dict = {
    #     # (w, h)
    #     (1, 1): (0, 0, 0),  # 黑色
    #     (0, 1): (128, 128, 128),  # 灰色
    #     (1, 0): (128, 128, 128),  # 灰色
    #     (2, 2): (128, 128, 128),  # 灰色
    # }
    # image_name_prefix = "grid_8_domain_Y1"
    # image_visual_path = f"./image_patent/{image_name_prefix}.png"

    # # Y2形示例
    # fill_color_dict = {
    #     # (w, h)
    #     (1, 1): (0, 0, 0),  # 黑色
    #     (0, 0): (128, 128, 128),  # 灰色
    #     (2, 0): (128, 128, 128),  # 灰色
    #     (1, 2): (128, 128, 128),  # 灰色
    # }
    # image_name_prefix = "grid_8_domain_Y2"
    # image_visual_path = f"./image_patent/{image_name_prefix}.png"
    
    # L1形示例
    # fill_color_dict = {
    #     # (w, h)
    #     (1, 1): (0, 0, 0),  # 黑色
    #     (0, 1): (128, 128, 128),  # 灰色
    #     (2, 0): (128, 128, 128),  # 灰色
    #     (2, 1): (128, 128, 128),  # 灰色
    # }
    # image_name_prefix = "grid_8_domain_L1"
    # image_visual_path = f"./image_patent/{image_name_prefix}.png"

    # L2形示例
    # fill_color_dict = {
    #     # (w, h)
    #     (1, 1): (0, 0, 0),  # 黑色
    #     (0, 0): (128, 128, 128),  # 灰色
    #     (2, 2): (128, 128, 128),  # 灰色
    #     (2, 1): (128, 128, 128),  # 灰色
    # }
    # image_name_prefix = "grid_8_domain_L2"
    # image_visual_path = f"./image_patent/{image_name_prefix}.png"



    # # 十字形1
    # fill_color_dict = {
    #     # (w, h)
    #     (1, 1): (0, 0, 0),  # 黑色
    #     (0, 1): (128, 128, 128),  # 灰色
    #     (1, 0): (128, 128, 128),  # 灰色
    #     (2, 1): (128, 128, 128),  # 灰色
    #     (1, 2): (128, 128, 128),  # 灰色
    # }
    # image_name_prefix = "grid_8_domain_+1"
    # image_visual_path = f"./image_patent/{image_name_prefix}.png"
    
    # # X字形1
    # fill_color_dict = {
    #     # (w, h)
    #     (1, 1): (0, 0, 0),  # 黑色
    #     (0, 0): (128, 128, 128),  # 灰色
    #     (2, 0): (128, 128, 128),  # 灰色
    #     (2, 2): (128, 128, 128),  # 灰色
    #     (0, 2): (128, 128, 128),  # 灰色
    # }
    # image_name_prefix = "grid_8_domain_x1"
    # image_visual_path = f"./image_patent/{image_name_prefix}.png"
    
    # X字形2
    fill_color_dict = {
        # (w, h)
        (1, 1): (0, 0, 0),  # 黑色
        (1, 0): (128, 128, 128),  # 灰色
        (1, 2): (128, 128, 128),  # 灰色
        (2, 0): (128, 128, 128),  # 灰色
        (0, 2): (128, 128, 128),  # 灰色
    }
    image_name_prefix = "grid_8_domain_x2"
    image_visual_path = f"./image_patent/{image_name_prefix}.png"


    grid_image = draw_grid(
        grid_num=grid_num, 
        fill_color_dict=fill_color_dict,
        grid_size=100, 
    )
    cv2.imwrite(image_visual_path, grid_image)

示例图

4领域图像

请添加图片描述

八领域图像

请添加图片描述


原文地址:https://blog.csdn.net/familytaijun/article/details/144069326

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!