How to fill OpenCV image with one solid color?

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

How to fill OpenCV image with one solid color?

相关标签:
8条回答
  • 2020-12-01 12:24

    I personally made this python code to change the color of a whole image opened or created with openCV . I am sorry if it's not good enough , I am beginner

    0 讨论(0)
  • 2020-12-01 12:26

    Here's how to do with cv2 in Python:

    # Create a blank 300x300 black image
    image = np.zeros((300, 300, 3), np.uint8)
    # Fill image with red color(set each pixel to red)
    image[:] = (0, 0, 255)
    

    Here's more complete example how to create new blank image filled with a certain RGB color

    import cv2
    import numpy as np
    
    def create_blank(width, height, rgb_color=(0, 0, 0)):
        """Create new image(numpy array) filled with certain color in RGB"""
        # Create black blank image
        image = np.zeros((height, width, 3), np.uint8)
    
        # Since OpenCV uses BGR, convert the color first
        color = tuple(reversed(rgb_color))
        # Fill image with color
        image[:] = color
    
        return image
    
    # Create new blank 300x300 red image
    width, height = 300, 300
    
    red = (255, 0, 0)
    image = create_blank(width, height, rgb_color=red)
    cv2.imwrite('red.jpg', image)
    
    0 讨论(0)
提交回复
热议问题