Loading, displaying, and saving images with opencv
Computer vision is an interdisciplinary scientific field that deals with how computers can be made to gain high-level understanding from digital images or videos. From the perspective of engineering, it seeks to automate tasks that the human visual system can do.
OpenCV is a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel, it was later supported by Willow Garage then Itseez. The library is cross-platform and free for use under the open-source BSD license.
Now as you are aware of opencv and CV at the same time let’s get started with computer vision classes to become expert in this field.
The objective of this post is for you to get familiar with OpenCV library. We’ll be using OpenCV inside the vast majority of posts in our blog, so you’ll want to familiarize yourself with the library quickly. By the end of this blog post you’ll be able to:
- Load an image from disk using the cv2.imread function.
- Display the image on your screen using cv2.imshow .
- Write your image back to disk in a different image file format using cv2.imwrite .
- Use the command line to execute Python scripts.
so let’s get started:
first we will load the image than save it and then show it for that lets make a python file named load.py
first we will load the necessary utilities. We’ll use argparse to handle parsing our command line arguments. Then, cv2 is imported— cv2 is our OpenCV library and contains our image processing functions. As you’ll see in future lessons, the cv2 and argparse packages will be used in almost every topic we cover. first we will load the necessary utilities.
The only argument we need is –image : the path to our image on disk. Finally, we parse the arguments and store them in a dictionary called args .
by command line argument we can automate our code isn’t we . now to load and display we will use the cv2 library
The cv2.imread function returns a NumPy array representing the image. since images are represented as NumPy arrays, we can simply use the shape attribute to examine the width, height, and the number of channels .
All we are doing here is providing the path to the file (the first argument) and then the image we want to save (the second argument). It’s that simple. In this case we’ll just hardcode the path to newimage.jpg . For practice, once you have finished this lesson, you should download the code and update it to utilize a second command line argument to write the image to disk.
To run our script and display our image, we simply open up a terminal window and execute the following command:
|1||$ python load.py –image abc.png|
In this lesson we dipped our toes into the vast world of computer vision. We utilized the OpenCV library to load an image off disk, display it to our screen, and write it back to file in a different image format.
We also explored how to use our terminal and supply command line arguments.
github link:click here.