How can I make empty grayscale image in OpenCV?

Question:

I want to know how to make a empty grayscale image in OpenCV.

First, I know how to make a empty ‘color image’ like this.

import cv2  
import numpy as np
blank_image = np.zeros((height,width,3), dtype = np.uint8)

Then, how can I make a empty ‘grayscale’ image with specific height and width?

Asked By: Great

||

Answers:

import cv2  
import numpy as np
gray_level = 127
gray_image = gray_level * np.ones((height,width,3), dtype = np.uint8)

If you don’t need 3 channels, make that np.ones((height, width), dtype=np.uint8)

Answered By: Francesco Callari
import numpy as np
gray_image = np.full((height, width), gray_value, dtype=np.uint8)
Answered By: fmw42
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.