In the numpy Images relation chapter, we have learned to open the images with the help of PIL and Numpy, matplotlib.
import matplotlib.pyplot as plt from PIL import Image img = Image.open("C:/Users/pnagaraj/Pictures/opencv/flower.jpg") plt.imshow(img)
However, as we are moving forward, we will be using the OpenCV directly to read the images.
Below are the import statements that we will be using throughout the article so that I will be skipping the imports in my program, but you should if you are working with an IDE. I am working with Jupyter notebook.
import cv2 import matplotlib.pyplot as plt
cv2 is the module that we need to import for the OpenCV.
We can use the
imread() function to read the image files in OpenCV. Reading the above image with OpenCV.
I hope you find the difference between images opened by matplotlib and OpenCV compared with the original image...
Matplotlib produced an exact image as Original, but OpenCV changed some colors and opened the image. Let's understand why it is happening.
OpenCV treats the images as BGR color, but in actual, all the images are RGB colors. Matplotlib treats the image with RGB so that it is providing the same image.
So when OpenCV read the Image as Matrix/Array, the position of the RED color as BLUE color. Because of this reason, wherever the RED is there, It will be treated as BLUE in OpenCV by default.
To get the original RGB color, we need to convert the BGR image to an RGB image. We can convert the image using
cvtColor() function present in OpenCV.
img = cv2.cvtColor(image_to_convert, ENUM_COLOR_CODING)
img = cv2.imread("C:/Users/pnagaraj/Pictures/opencv/flower.jpg") img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) plt.imshow(img)
After processing the images, we need to save them on the file system so that we can use it for verification purposes.
In our application, we might need to change the color images into white & black images. In such cases, we can read the images as White and Black while loading itself.
imread() function accepts two parameter
img = cv2.imread(path_of_images, color_scale)
img = cv2.imread("C:/Users/pnagaraj/Pictures/opencv/flower.jpg", cv2.IMREAD_GRAYSCALE) plt.imshow(img)
I explained in the last article why the above image is not black and white, by default
imshow() will not pick any color mapping (cmap), so we see the image in the Viridis color scale.
To make the above image to black and white then we need to provide the "gray" as value for cmap for
# gray images img = cv2.imread("C:/Users/pnagaraj/Pictures/opencv/flower.jpg", cv2.IMREAD_GRAYSCALE) plt.imshow(img, cmap="gray")
In case, if you have already read the image, then you can use the
cvtColor() function to convert the color image to White and Black.
# convert gray images img = cv2.imread("C:/Users/pnagaraj/Pictures/opencv/flower.jpg") img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) plt.imshow(img, cmap="gray")
Resizing is operation to make the larger pixel image to fit into smaller pixel image, the quality of the image goes down based on the target size of the image.
OpenCV can alter the size of images; sometimes, we will have limitation like what size image we can process. In such cases, resizing the image will help us to proceed further on the operation required.
resize() function in OpenCV helps to change the size of the image.
cv2.resize(image_t-_resize, (width_of_target, height_of_target))
resizing is not same as Cropping
# Image size operations img = cv2.imread("C:/Users/pnagaraj/Pictures/opencv/flower.jpg") print("Original Image size : ", img.shape) resize_image = cv2.resize(img, (1000, 200)) plt.imshow(resize_image) print("Resized Image size : ", resize_image.shape)
Sometimes we need to change the Image based on the ratio rather than pixel size.
cv2.resize(image_to_resize, (0, 0), image_to_resize, width_ration, height_ration)
# Image size operations img = cv2.imread("C:/Users/pnagaraj/Pictures/opencv/flower.jpg") print("Original Image size : ", img.shape) resize_image = cv2.resize(img, (0, 0), img, 0.3, 0.2) plt.imshow(resize_image) print("Resized Image size : ", resize_image.shape)
Flipping an image becomes necessary when we get a bunch of images because you cannot expect all the cameras to send the same kind of images. Sometimes objects on the images may not be normal, so detach such things we need to flip an image.
flip is not the same as rotate
OpenCV provides 3 types of flips:
# Image flip operations img = cv2.imread("C:/Users/pnagaraj/Pictures/opencv/flower.jpg") flip_image = cv2.flip(img, 0) #last param alone changes plt.imshow(flip_image)
We can open the image in a new window outside the Notebook. You need to use the cv2.destroyAllWindows() command to close the image window. If you try to close manually, then the new window might crash. Notebook restarts the kernel automatically, but you need to run imports and other things again.
resize_image = cv2.resize(img, (0, 0), img, 0.3, 0.3) cv2.imshow("Puppy", resize_image) cv2.waitKey(0) cv2.destroyAllWindows()