How to fill OpenCV image with one solid color?

前端 未结 8 1460
陌清茗
陌清茗 2020-12-01 11:32

How to fill OpenCV image with one solid color?

8条回答
  •  无人及你
    2020-12-01 12:02

    Use numpy.full. Here's a Python that creates a gray, blue, green and red image and shows in a 2x2 grid.

    import cv2
    import numpy as np
    
    gray_img = np.full((100, 100, 3), 127, np.uint8)
    
    blue_img = np.full((100, 100, 3), 0, np.uint8)
    green_img = np.full((100, 100, 3), 0, np.uint8)
    red_img = np.full((100, 100, 3), 0, np.uint8)
    
    full_layer = np.full((100, 100), 255, np.uint8)
    
    # OpenCV goes in blue, green, red order
    blue_img[:, :, 0] = full_layer
    green_img[:, :, 1] = full_layer
    red_img[:, :, 2] = full_layer
    
    cv2.imshow('2x2_grid', np.vstack([
        np.hstack([gray_img, blue_img]), 
        np.hstack([green_img, red_img])
    ]))
    cv2.waitKey(0)
    cv2.destroyWindow('2x2_grid')
    

提交回复
热议问题