Image Basics with OpenCV

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 ="C:/Users/pnagaraj/Pictures/opencv/flower.jpg")


However, as we are moving forward, we will be using the OpenCV directly to read the images.

Import statements:

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.

Color coding in OpenCV :

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)
  • image_to_convert : The image that we want to convert from one color-coding to other
  • ENUM_COLOR_CODING : From which color code to which color that image has to be converted. (in our case, it is cv2.COLOR_BGR2RGB).
img = cv2.imread("C:/Users/pnagaraj/Pictures/opencv/flower.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)


Write images to the system:

After processing the images, we need to save them on the file system so that we can use it for verification purposes.

cv2.imwrite("path_and_name_of_image.jpg", image_date_to_write)

Black and White Images

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)
  • Path of the images
  • The color scale that we need to read (by default it is BGR)
img = cv2.imread("C:/Users/pnagaraj/Pictures/opencv/flower.jpg", cv2.IMREAD_GRAYSCALE)


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 imshow() function.

# 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")


Operations on Image Size with OpenCV

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))
print("Resized Image size : ", resize_image.shape)
The position of the Height and width arguments changes compared with image.shape property.


Image resize based on ratio:

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)
print("Resized Image size : ", resize_image.shape)


Flip Operations on Image OpenCV

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:

  • Neutral flip(pull top to bottom)
  • Positive flips (pull right to the left)
  • Negative flip(pull right to the left then pull top to bottom)


# Image flip operations
img = cv2.imread("C:/Users/pnagaraj/Pictures/opencv/flower.jpg")
flip_image = cv2.flip(img, 0) #last param alone changes


Open image in new window

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)
OpenCV opens the image tab in the size of the image if the image is very, then you will have the problem of closing, so it better-using cv2.destroyAllWindows() to close the tab. I resized the image so that I can take the below screenshot.


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Subscribe to See Videos

Subscribe to my Youtube channel for new videos : Subscribe Now