Create a white background using OpenCV in Python
This post follows up on the in-detail idea of Creating a White background using OpenCV in Python. Let us discuss the main idea behind it. We can make a white background image using the np.ones()
function. It produces an n-dimensional NumPy array of the specified size with all members set to 1. This array is multiplied by 255 to have a white picture. Now that all the components are set to 255, use the cv2.imshow()
or plt.imshow()
procedures to display its results in a white background image.
Importing all the required modules:
import cv2 import numpy as np
Method 1- Creating the white background using np.full method:
np. full allows the user to fill a value-filled new array of the specified form and the type is returned by the full()
method. The syntax of the provided method will be numpy.full(shape, fill_value, dtype=None, order='C')
Code Example:
# importing the important libraries import cv2 import numpy as np # create an array using np.full # 255 is code for white color array = np.full((500, 500, 3), 255, dtype = np.uint8) # display the image cv2.imshow("image", array) cv2.waitKey(0)
Output:
Method 2- By creating an array using np.zeros():
A new array with zeros and the specified form and type is returned by the np.zeros()
method. The syntax of the method is provided as numpy.zeros(shape, dtype = None, order = 'C')
Code Example:
# importing the important modules import numpy as np import cv2 # creating array using np.zeroes() array = np.zeros([500, 500, 3], dtype = np.uint8) # setting RGB color values as 255,255,255 it is the vale of white color array[:, :] = [255, 255, 255] # display the image cv2.imshow("image", array) cv2.waitKey(0)
Output:
With this second method, we have concluded our article. Learn, Motion Detection using OpenCV in Python.
Leave a Reply