In the Computer Vision, we do the work on images and videos and the starting of any project that involves computer vision, we first read/ load images. And we of-course know that the videos are sequence of images, so we learn how read images and manipulate them.
In this post I will describe four Python Libraries that can load images. These are as follows:- OpenCV: cv2.imread()
- Matplotlib: plt.imread() and image.imread()
- scikit-image: io.imread()
- Pillow: Image.open()
Original Image |
OpenCV:
OpenCV (Open Source Computer Vision) is an open source computer vision and machine learning library. To work with computer vision problems OpenCV is very helpful. We can perform lots of tasks using OpenCV like Recognition, Motion Analysis, Scene Reconstruction, image restoration and many more.Install OpenCV using
pip install opencv-python
import cv2img = cv2.imread('Tulips.jpg')
Note: If the image is not in the same file where you are running python code the specify the path of the image accordingly. See the below code.
import cv2img_path = r'C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg'img = cv2.imread(image_path)
cv2.imshow() is used to display the image. See the below code.
cv2.imshow('image', img)cv2.waitKey(0)cv2.destrouAllWindows()
Another way and most common to display the image is using pl.imshow().Note: Don't forget to import cv2 and to write cv.wait(0) and cv2.destroyAllWindows().
from matplotlib.pyplot as pltplt.imshow(img)
output using plt.imshow() |
This is because openCV reads image as BGR image. So to convert from BGR to RGB use below code.Note the difference between original image and above image.
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
You may be interested in below article.
How to Load and Visualize Multiple Images in Python
Matplotlib:
Matplotlib is a comprehensive library for creating static, animated and interactive visualization in Python. plt.imread() is used to read the image and plt.imshow() tom display the image.
Install Matplotlib using pip install matplotlib
The below code will read and display the image:You may be interested in below article.
How to Load and Visualize Multiple Images in Python
Matplotlib:
Matplotlib is a comprehensive library for creating static, animated and interactive visualization in Python. plt.imread() is used to read the image and plt.imshow() tom display the image.from matplotlib.pyplot as pltimg= plt.imread('Tulips.jpg')plt.imshow(img)
from matplotlib import imageimport matplotlib.pyplot as pltimg= image.imread('Tulips.jpg')plt.imshow(img)
scikit-image:
scikit-image is an open source image processing library for Python Programming Language.Install scikit-image using
pip install scikit-image
io.imread() is used to read the image. See the below Python code:
from skiimage import ioimport matplotlib.pyplot as pltimg= io.imread('Tulips.jpg')plt.imshow(img)
Output: Output is same as the output in Matplotlib
Install Pillow using
Pillow:
PIL(Pyhton Imaging Library), in newer version known as Pillow, is a free open-source library for Python Programming Language used in image processing.Install Pillow using
pip install Pillow==2.2.2
from PIL import Imageimport matplotlib.pyplot as pltimg = Image.open('Tulips.jpg')plt.imshow(img)
Further Readings:
- OpenCV https://docs.opencv.org/master/
- Matplotlib https://matplotlib.org/
- scikit-image https://scikit-image.org/
- Pillow http://www.pythonware.com/products/pil/
Next Post: What is use of numpy.random.seed()
Comments
Post a Comment