How to Read and Display Images Using OpenCV-Python Part - 1
In the previous post we saw how a computer reads an image, moving ahead in that direction in this post we will explore how to read an image in a computer using the python programming language. We will read an image with the help of the OpenCV (Open Source Computer Vision) module available in python. OpenCV is a library which is used for computer vision oriented tasks. In OpenCV all the images are converted to or form NumPy arrrays.
NumPy is a Python library used for working with arrays. There are functions available in NumPy such that it can also be used in the linear algebra and matrices domain. If you guys are no familiar with NumPy and want me to create a separate series on NumPy let me know in the comments below.
For now, understand it like this, we previously saw that the computer reads and stores an image as a matrix, if it is a colored image it will get read/stored as a 3D-array and if it is a grayscale image it will get read/stored as a 2D-array. So when reading images with OpenCV in Python they are stored as a NumPy array.
With that being understood let us move ahead and start the process of reading and displaying an image using OpenCV. Throughout this series we will be using the Pycharm IDE for writing our scripts. To get a step-by-step guide of how to install Python and Pycharm IDE on a windows computer refer this post.
With Pycharm IDE installed in order to be able to read images using OpenCV we will first need to install the opencv-python module. For that head to File section in your Pycharm Project then go to Settings. File -- Settings.
In the window that appears next type opencv-python in the search tab and click on the Install Package button. Close the Settings window, you have successfully installed the OpenCV module.
import cv2
Next the OpenCV module provides us with a method to read images and that method is- image - Path to the image that is to be read into the computer.
- flag - Numerical value that decides whether the image is to be read as a colored image or a grayscale image.
- flag=0 means that the image will be read as a grayscale image
- while, flag=1 means that the image will be read as a colored image.
import cv2
img=cv2.imread("c:/Users/HP/Desktop/harshad.jpg",1)
import cv2
img=cv2.imread("c:/Users/HP/Desktop/harshad.jpg",1)
print(img)
[[[ 20 45 61]
[ 20 45 61]
[ 19 45 61]
...
[ 20 49 64]
[ 20 49 64]
[ 18 49 64]]
[[ 18 43 59]
[ 18 43 59]
[ 17 43 59]
...
[ 21 50 65]
[ 21 50 65]
[ 20 51 66]]
[[ 16 41 57]
[ 16 41 57]
[ 16 41 57]
...
[ 22 51 66]
[ 22 51 66]
[ 21 52 67]]
...
[[ 6 13 22]
[ 5 12 21]
[ 4 10 21]
...
[ 13 61 95]
[ 13 56 77]
[ 15 57 69]]
[[ 6 13 22]
[ 5 12 21]
[ 5 11 22]
...
[ 18 66 102]
[ 11 55 78]
[ 11 55 68]]
[[ 6 13 22]
[ 6 13 22]
[ 5 11 22]
...
[ 18 67 105]
[ 10 54 77]
[ 9 52 67]]]
You can clearly see that this colored image is being stored as a 3D-matrix. Similarly, if we read the image with flag=0 and try to print it, we will find that it gets stored as a 2D-matrix.
Now, let us say we want to see what is the size of the image that we have read. Since the image is being read as a NumPy array we can use the shape attribute to determine the size of the image.
import cv2
img=cv2.imread("c:/Users/HP/Desktop/harshad.jpg",1)
print(img.shape)
The output the above code snippet will be the [number of rows, number of columns, number of channels] because we are reading it as a colored image (flag=1) it will have three channels. As shown in the output the shape of the image is 500x800 and it has three channels. Similarly if we will read the same image as grayscale it will show only (500,800) as grayscale image has only a single channel. To be Continued in Part 2
(500, 800, 3)
Impressive and powerful suggestion by the author of this blog are really helpful to me.
ReplyDeletegrayscale image