Some Advanced OpenCV Functions For Computer Vision Project Continued

Shiv Last Updated : 23 Jul, 2021
5 min read

This article was published as a part of the Data Science Blogathon

Introduction.

This article will continue from where the previous article(s) left off. If you would like to follow along from the beginning of the OpenCV tutorial, please navigate to the below articles I have written:

This article will seek to enlighten you about more of the interesting operations that can be performed with images using the OpenCV Library in the Python Programming Language.

Opencv introductory image | Advanced OpenCV Functions

Source: Medium.

Requirements.

In order to have maximum takeaways from this informative session, I recommend that you follow along in a Python IDE of your choice. To facilitate the hands-on OpenCV learning experience, we will make use of an image that can be located at this link. Feel free to download the image from the link, or alternatively, you may save the image found below.

Test image | Advanced OpenCV Functions

Source: Wallpaper Flare.

Saving an Image.

The first action to perform would be the action of loading the image into our system memory as follows:

import cv2
 image = cv2.imread("C:/Users/Shivek/Pictures/cool-backgrounds-nature-1920x1200-wallpaper-preview.jpg", cv2.IMREAD_COLOR)
 cv2.imshow('Analytics Vidhya Computer Vision- Mountain View', image)
 cv2.waitKey()
 cv2.destroyAllWindows()

I will omit lengthy explanations for the above code block as those aspects have been covered in
previous articles. The important points to note are:

  • We have imported the necessary Library.
  • We have utilized the imread() method to load the image into our system memory. In this particular instance, we are loading the image in the standard/color format. Please pass the corresponding location of the file on your personal computer.
  • We make use of the imshow() method to display the image on a GUI Window on our computer screen.
  • The waitKey() and destroyAllWindows() methods will be used to terminate the GUI Windows that have been opened on your system as a result of OpenCV operations.

Output to the above code will be seen as below:  

output 1 | Advanced OpenCV Functions

Next, we shall attempt to resize the image, specifically, we would reduce the size of the image as follows:

# resize the image to be pixel dimensions 455 by 150
 resized_image = cv2.resize(src=image, dsize=(455, 150))
 cv2.imshow('Analytics Vidhya Computer Vision- Mountain View', resized_image)
 cv2.waitKey()
 cv2.destroyAllWindows()

We make use of the resize() method while passing in the source image (image to be resized) and the desired size of the image in a tuple containing two integer values. The first value represents the number of rows of pixels and the second is the number of columns of pixels. Do not confuse the shape method with the dsize parameter: shape will return to us the rows, columns, and color channels (if any). The dsize parameter will require that we pass in the number of rows, followed by the number of columns.

Output to the above code will be seen as below:

output resized image | Advanced OpenCV Functions

One may see that upon reducing the image size, we notice that the quality of the resized image seems to have been improved. It is good to know that this, in reality, does not occur- reducing the size of the image does not improve the quality- It simply is due to the nature of the eyes that the image appears sharper. While you look at the resized from a distance, bring your eyes closer to the image, and notice how the image quality degrades (or restores to normal). The explanation for the imperceptible decrease of quality is simple: The more pixels in an image, the clearer the image, hence by reducing the size of the image, we are decreasing the total number of pixels in the image, therefore causing image quality to decrease proportionally.

If one would like to save
the new image (resized image), one would proceed to do so as follows:

# before we begin, let us import the os package
 import os
cv2.imwrite(filename="Mountain View 455x150.png", img=resized_image)
print(os.listdir("C:/Users/Shivek/Desktop/Experimenting/Analytics Vidhya Blogathon/"))
print("Successfully Saved!")

The explanation for the above code block is as follows:

cv2.imwrite("Mountain View 455x150.png", resized_image)

We make use of the imwrite() function offered by the OpenCV module. The imwrite() method will write the data file to the specified file location on the Hard Disk Drive of your system. This function requires two important parameters:

  • filename: This is the name given to the file to be written, which is of string datatype.
  • img: This is the image to be written to the Hard Disk Drive. The image will be
    passed in as an array.

It is important to know that the imwrite() will overwrite any existing files with the same name in the same folder, without requesting confirmation or overwrite permission.

print(os.listdir("C:/Users/Shivek/Desktop/Experimenting/Analytics Vidhya Blogathon/"))

As one can see, the above line of code makes use of the OS module available in Python Programming Language. This module allows us to interact with our system and its associated aspects and acts much like a Built-In Python Command Prompt for our system.

In this particular line of code, we make use of the listdir() method which will effectively provide a list of the contents of a particular specified directory. This includes any and all files, directories, and sub-directories. A directory is basically a folder. Upon receiving the contents of the specified Directory, we are printing the
information to the console for the user to view.

print("Successfully Saved!")

Upon successfully writing the image to the disk, and printing the contents of the directory, we proceed to print a short message to the screen that says “Successfully Saved!”. This aims to provide assurance to the individual that the image writing process to the disk, has been successful.

Output for the above code block will be seen as follows:

output save image | Advanced OpenCV Functions

As can be seen in the above image, upon successful writing, one will see the new data file in their File Explorer.

new data file

The contents of the directory will be seen in the Python IDE Terminal or Console. One will be able to see the new file showing up in the List of Directory Contents that is returned by the os.listdir() method.

Finally, last but not least, the message “Successfully Saved!” will be seen in the Python Console. This will assure us that our writing process has concluded and that too, successfully.

successfully saved | Advanced OpenCV Functions

This concludes my article. I do hope that you enjoyed reading through this article, and have learned new concepts about the OpenCV package in Python.

Please feel free to connect with me on LinkedIn.

Thank you for your time.

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details