OpenCV Python Tutorial for Beginners Part 2
By: Himanshu Kanojiya
Tags: Python Programming, Python, Opencv Python, Python3, Opencv

Welcome to OpenCV Python Beginners Tutorial Part 2.

In case you have not read the first part of the OpenCV tutorial Series, then click here to visit First Part of OpenCV tutorial Series.

As you know, we covered some basics & 4basics tasks of OpenCV Python in our first tutorial.

Now, In this tutorial, we will begin the article from where we left off.

Let’s get started:

Task 5. Get Image / Video Frame Size & Shape

Code:

image = cv2.imread("victory.png", cv2.IMREAD_COLOR)

print(image.shape) #to print the shape of an image

print(image.size) #to display the size of an image

Let’s understand the code:

image = cv2.imread("victory.png", cv2.IMREAD_COLOR)

In the First Line, we have loaded the image to the image variable using the cv2.imread method.

print(image.shape)

In the Second Line, we have used an image.shape to get the height, width, and color channel of an image. In case, If an image is in grayscale format, then it will return only height & width. Output value always comes in the tuple.

print(image.size)

In the third Line, we have used image.size to get the size of the image. It is the value of (height * width * color channel).

Task 6. Resize the Image

Code:

image = cv2.imread("victory.png", cv2.IMREAD_COLOR)

image = cv2.resize(image, (300,300)) #resizing method/function

cv2.imshow("IMAGE",image)

cv2.waitKey()

cv2.destroyAllWindows()

Let’s understand the code:

image = cv2.imread("victory.png", cv2.IMREAD_COLOR)

In the First Line, we have loaded the image to the image variable using the cv2.imread method.

image = cv2.resize(image, (300,300)) #resizing method/function

In the Second Line, we have used the cv2.resize method. In this method, we give the image array as the first argument, and to get the desired size, we pass the size in the form of a tuple — (height, width) as the second argument.

Note: Width & Height should be in the integer format.

cv2.imshow("IMAGE",image)

In the third Line, we have used the cv2.imshow() method to create a GUI window on the screen to display the image. In arguments, we need to give GUI window name as the first argument (should be in the string format) and the image array(which we want to show on screen) as the second argument.

cv2.waitKey() cv2.destroyAllWindows()

In the Fourth Line, we have used the cv2.waitKey() method to wait until the user press the close button, key, or specified key.

Once it is detected the key, will release the control & give control to the Fifth Line (cv2.destroyAllWindows()) and close & destroy the GUI window.

Task 7. Create Blank Black/White Image using numpy

Code:

white_img = np.ones((512,512,3), dtype=np.uint8) * 255

black_img = np.zeroes((512,512,3), dtype=np.uint8)

cv2.imshow("White Image",white_img)

cv2.imshow("Black Image", black_img)

cv2.waitKey()

cv2.destroyAllWindows()

Let’s understand the code:

Note: (0–255) range shows how much image dark and light it is. “0” for full black, and “255” for white.

white_img = np.ones((512,512,3), dtype=np.uint8) * 255

In the First Line, we have used one of the numpy methods to create a matrix that contains only one’s. In np.ones(), we passed the tuple as the first argument, which is the size of the blank image matrix (In tuple: the first item is height, and the second item is width, and third is the color channel).

In the second argument, we have passed the data type of matrix, which is np.uint8 (unsigned integer 8). After that, we have multiplied all the value of the matrix with 255 using numpy broadcasting so that we can get a matrix that contains 255.

In case if you don’t know what broadcasting is, then you can read the below mention article: https://numpy.org/doc/stable/user/basics.broadcasting.html

black_img = np.zeroes((512,512,3), dtype=np.uint8)

In the Second Line, we have used the np.zeroes method to create a matrix that contains only zeroes. In np.ones(), we passed the tuple as the first argument, which is the size of the blank image matrix (In tuple: the first item is height, and the second item is width, and third is the color channel).

In the second argument, we have passed the data type of matrix, which is np.uint8 (unsigned integer 8).

cv2.imshow("White Image",white_img) cv2.imshow("Black Image", black_img)

In the third & Fourth Line, we have used the cv2.imshow() method to create a GUI window on the screen to display a black and white image.

cv2.waitKey() cv2.destroyAllWindows()

In the Fifth Line, we have used the cv2.waitKey() method to wait until the user press the close button, key, or specified key.

Once it is detected the key, will release the control & give control to the Sixth Line (cv2.destroyAllWindows()) and close & destroy the GUI window.

Task 8. How to Add Two Images together using the Add function

Code:

imagePotrait = cv2.imread("victory.png", cv2.IMREAD_COLOR)

imageLogo = cv2.imread("logo-creative.png", cv2.IMREAD_COLOR)



#Need to set the same size for both loaded images.

#Both images size should be the same before using the add method

imagePotrait = cv2.resize(imagePotrait, (250,250))

imageLogo = cv2.resize(imageLogo, (250,250))



#add method

addedImage = cv2.add(imagePotrait, imageLogo)

cv2.imshow("Added Image", addedImage)

cv2.waitKey()

cv2.destroyAllWindows()

Let’s understand the code:

imagePotrait = cv2.imread("victory.png", cv2.IMREAD_COLOR)

imageLogo = cv2.imread("logo-creative.png", cv2.IMREAD_COLOR)

In the First & Second Line, we have used the cv2.imread method to load both images in color format.

imagePotrait = cv2.resize(imagePotrait, (250,250))

imageLogo = cv2.resize(imageLogo, (250,250))

In the Sixth & Seventh Line, we have used the cv2.resize method to resize both images so that we can use them in the cv2.add. To use the cv2.add, we need to set the same size for both Images. If we don’t set an equal size both for images, then it will raise the error while using cv2.add.

addedImage = cv2.add(imagePotrait, imageLogo)

Finally, In the Tenth, we have used the cv2.add method to add both images. In this method, we need to pass the first & second image as the argument.

cv2.imshow("Added Image", addedImage)

In the Eleventh Line, we have used the cv2.imshow() method to create a GUI window on the screen to display the added image.

cv2.waitKey()

cv2.destroyAllWindows()

In the Twelfth Line, we have used the cv2.waitKey() method to wait until the user press the close button, key, or specified key.

Once it is detected the key, will release the control & give control to the Thirteen Line (cv2.destroyAllWindows()) and close & destroy the GUI window.

Task 9. How to add two together using the cv2.addWeighted method

Code:

imagePotrait = cv2.imread("victory.png", cv2.IMREAD_COLOR)

imageLogo = cv2.imread("logo-creative.png", cv2.IMREAD_COLOR)



#Need to set the same size for both loaded images.

#Both images size should be the same before using the add method

imagePotrait = cv2.resize(imagePotrait, (250,250))

imageLogo = cv2.resize(imageLogo, (250,250))



#add method

addedImage = cv2.addWeighted(imagePotrait, 0.8, imageLogo, 0.2, 0)

cv2.imshow("Added Image", addedImage)

cv2.waitKey()

cv2.destroyAllWindows()

Let’s understand the code:

imagePotrait = cv2.imread("victory.png", cv2.IMREAD_COLOR)

imageLogo = cv2.imread("logo-creative.png", cv2.IMREAD_COLOR)

In the First & Second Line, we have used the cv2.imread method to load images and also passed the flag to read Images in the color format.

imagePotrait = cv2.resize(imagePotrait, (250,250))

imageLogo = cv2.resize(imageLogo, (250,250))

Same as the cv2.add method, We have used the cv2.resize in the Sixth & Seventh Line to resize both images so that we can use them in the cv2.addWeighted. To use the cv2.addWeighted, we need to set the same size for both Images. If we don’t set an equal size both for images, then it will raise the error while using cv2.addWeighted.

addedImage = cv2.addWeighted(imagePotrait, 0.8, imageLogo, 0.2, 0)

In the Tenth Line, we have used the cv2.addWeighted method to add both images together.

cv2.addWeighted(imagePotrait, 0.8, imageLogo, 0.2, 0)

Let’s understand this method more deeply.

In the first argument, We need to pass the first image array, and in the second argument, we need to give the alpha value, which sets the brightness/intensity level of the first array.

In the third argument, We need to pass the second image array, and in the fourth argument, we need to give beta value, which sets the brightness level/intensity of the second array.

In the Fifth argument, We need to pass the gamma value, which will add the gamma value to the sum of both image arrays.

Use Case: If you want to add the second image as a watermark on the first image, then increase the alpha value (should be in the float data type), and try to reduce the beta value (should be in the float data type) as required. In case, not satisfied with the output, then play a little around on alpha & beta values.

cv2.imshow("Added Image", addedImage)

In the Eleventh Line, we have used the cv2.imshow() method to create a GUI window on the screen to display the added image.

cv2.waitKey()

cv2.destroyAllWindows()

In the Twelfth Line, we have used the cv2.waitKey() method to wait until the user press the close button, key, or specified key.

Once it is detected the key, will release the control & give control to the Thirteen Line (cv2.destroyAllWindows()) and close & destroy the GUI window.

In case you have not read the first part of the OpenCV tutorial Series, then click here to visit First Part of OpenCV tutorial Series.

Note: I hope you like the second part of my OpenCV tutorial Series. In case of any mistakes in this tutorial. Please feel free to comment so that I can improve the quality of my articles.