Monday, 13 November 2023

Create a blank image in OpenCV

Following snippet create a blank image, every pixel in the image is coated with white color.

# Define the dimensions of the 2D array
rows = 500
cols = 500

# Create a 2D NumPy array filled with 4s
frame = np.full((rows, cols), 255, dtype='uint8')
cv.imshow('White blank image', frame)

blank_white_image.py

import cv2 as cv
import numpy as np

# Define the dimensions of the 2D array
rows = 500
cols = 500

# Create a 2D NumPy array filled with 4s
frame = np.full((rows, cols), 255, dtype='uint8')
cv.imshow('White blank image', frame)

cv.waitKey(0)

Output


 

Blank black image

Fill each pixel with the value 0.

 

blank_black_image.py

import cv2 as cv
import numpy as np

# Define the dimensions of the 2D array
rows = 500
cols = 500

# Create a 2D NumPy array filled with 4s
frame = np.full((rows, cols), 0, dtype='uint8')
cv.imshow('White blank image', frame)

cv.waitKey(0)

 

Output

 


 

Let’s create a blank image with blue color

 

Create a three dimensional array and fill each value with [255, 0, 0].


random_image = np.zeros((height, width, 3), dtype=np.uint8)
random_image[:] = [255, 0, 0]

blank_image_with_blue_color.py

import cv2 as cv
import numpy as np
import random

# Define the dimensions of the blank image
width, height = 1000, 500

random_image = np.zeros((height, width, 3), dtype=np.uint8)
random_image[:] = [255, 0, 0]

# Display the random color image
cv.imshow('Blue Color Image', random_image)
cv.waitKey(0)
cv.destroyAllWindows()

Output




 

Previous                                                    Next                                                    Home

No comments:

Post a Comment