Digital image processing basics using OpenCV and python :

By Ahin Subhra Das

Image Processing is most commonly termed as ‘Digital Image Processing’ and It is considered as a subset of Computer Vision. Both Image Processing algorithms and Computer Vision (CV) algorithms take an image as input; however, in image processing, the output is also an image, whereas in computer vision the output can be some features/information about the image.

For image processing first we need to know what is digital image .

Digital image :

A digital image is a representation of a two-dimensional image as a finite set of digital values,called picture elements or pixels.

# Pixel:

A pixel is generally thought of as the smallest single component of a digital image.

Types of digital image:

Generally we consider four type of images:

  1. Binary images
  2. Gray-scale images
  3. Color images
  4. Multispectral images

1.Binary images : Binary images can take one two value 0 and 1 or typically black and white. Binary images takes only 1 binary digit to represent each pixel so it is also known as 1-bit image. e.g. — optical character recognition (OCR).

2.Gray-scale images: These images are also known as monochrome or one-color images. Gray-level images only contain gray level information they do not contain any color information. Available number of different gray levels is determined by the number of bits used for each pixel.

3.Color images: Color images are created as three-band monochrome image data, in which each band of image data corresponds to a different color. In each spectral band there is a gray-level information which is the actual information stored in the digital image.

3 channels red,green and blue

Now we are getting into our main part that is image processing using OpenCV :

First thing first what is OpenCV ?

: OpenCV is the huge python open-source library for the computer vision, machine learning, and image processing and now it plays a major role in real-time operation.

Why we use OpenCV?

  • OpenCV is available for free of cost.
  • Since the OpenCV library is written in C/C++, so it is quit fast. Now it can be used with Python.
  • It require less RAM to usage, it maybe of 60–70 MB.
  • Computer Vision is portable as OpenCV and can run on any device that can run on C.

Installation of the OpenCV :

Windows :: pip install opencv-python

MacOS :: $brew install opencv3 — with-contrib — with-python3

Linux :: $ sudo apt-get install libopencv-dev python-opencv

To check if your installation was successful or not, run the following command in either a Python shell or your command prompt:

import cv2

Basic operations with OpenCV :

OpenCV to Read Image:

OpenCV imread function

The imread() function loads image from the specified file and returns it.

The syntax is:

cv2.imread(filename[,flag])

Parameters:

filename: Name of the file to be loaded

flag: The flag specifies the color type of a loaded image:

  • CV_LOAD_IMAGE_ANYDEPTH — If we set it as flag, it will return 16-bits/32-bits image when the input has the corresponding depth, otherwise convert it to 8-BIT.
  • CV_LOAD_IMAGE_COLOR — If we set it as flag, it always return the converted image to the color one.
  • C V_LOAD_IMAGE_GRAYSCALE — If we set it as flag, it always convert image into the grayscale.

The imread() function returns a matrix, if the image cannot be read because of unsupported file format, missing file, unsupported or invalid format. Currently, the following file formats are supported.

Window bitmaps — *.bmp, *.dib
JPEG files — *.jpeg, *.jpg, *.jpe
Portable Network Graphics — *.png
Portable image format- *.pbm, *.pgm, *.ppm
TIFF files — *.tiff, *.tif

code :

import cv2

img = cv2.imread(r’D:\\ip_camp\\pic1.jpg’, cv2.IMREAD_COLOR)

cv2.imshow(‘image’,img)

cv2.waitKey(0)

cv2.destroyAllWindows()

output:

it will display the following image.

Code explanation :

Using imread(‘path’) and 0 denotes read as grayscale image. This cv2.imshow is using for display the image . cv2.waitKey(0) this is necessary to be required so that the image doesn’t close immediately. It will run continuously until the key press.

OpenCV: Copy image

code:

import cv2

image = cv2.imread(‘D:\\ip_camp\\blueshirt2.jpg’)

imageCopy = image.copy()

cv2.circle(imageCopy, (100, 100), 30, (255, 0, 0), -1)

cv2.imshow(‘image’, image)

cv2.imshow(‘image copy’, imageCopy)

cv2.waitKey(0)

cv2.destroyAllWindows()

output:

Code explanation :

we simply need to call the circle function of the cv2 module, passing as first input the image, as second a tuple with the x and y coordinates of the center of the circle, as third the radius, as fourth a tuple with the color in RGB and as fifth the thickness (-1 means drawing a filled circle).

we add the circle for indicate the copied image .

OpenCV: Converting an color image to HSV

code:

import cv2

image = cv2.imread(‘C:/Users/N/Desktop/Test.jpg’)

hsvImage = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

cv2.imshow(‘Original image’,image)

cv2.imshow(‘HSV image’, hsvImage)

cv2.waitKey(0)

cv2.destroyAllWindows()

output:

Code explanation :

we need to take in consideration that, when calling the imread function, the image will be stored in BGR format. Thus, to convert to HSV, we should use the COLOR_BGR2HSV code.

As output, the cvtColor will return the converted image, which we will store in a variable.As output, the cvtColor will return the converted image, which we will store in a variable.

After this, we will display both images. To show each image, we simply need to call the imshow function. It receives as first input a string with the name to be assigned to the window that will show the image, and as second input the image.

OpenCV: Converting an color image to grayscale image

code :

import cv2

image = cv2.imread(‘D:\\ip_camp\\blueshirt2.jpg’)
grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

cv2.imshow(‘RGB image’, image)
cv2.imshow(‘Grayscale image’, grayImage)
cv2.waitKey(0)
cv2.destroyAllWindows()

output:

Code explanation :

Grayscale image is very needy method in digital image processing.

As first input, this function cv2.imread receives the original image. As second input, cv2.cvtColor it receives the color space conversion code. Since we want to convert our original image from the BGR color space to gray, we use the code COLOR_BGR2GRAY.

Now, to display the images, we simply need to call the imshow function of the cv2 module. This function receives as first input a string with the name to assign to the window, and as second argument the image to show.

OpenCV: Resize an image

Code :

import cv2

img = cv2.imread(r’D:\\ip_camp\\blueshirt2.jpg’, 1)
scale = 60
width = int(img.shape[1] * scale / 100)
height = int(img.shape[0] * scale / 100)
dim = (width, height)
# resize image
resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)

print(‘Resized Dimensions : ‘, resized.shape)

cv2.imshow(“Resized image”, resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output :

Code explanation :

Resizing of image means changing the dimension of the image, its width or height as well as both. Also the aspect ratio of the original image could be retained by resizing an image. OpenCV provides cv2.resize() function to resize the image. The syntax is given as:

cv2.resize(src, dsize[, dst[, fx[,fy[,interpolation]]])

Parameters:

  • src — source/input image (required).
  • dsize — desired size for the output image(required)
  • fx — Scale factor along the horizontal axis.(optional)
  • fy — Scale factor along the vertical axis.
  • Interpolation(optional) — This flag uses following methods:
  • INTER_NEAREST — A nearest-interpolation INTER_AREA — resampling using pixel area relation. When we attempt to do image zoom, it is similar to the INTER_NEAREST method.
  • INTER_CUBIC — A bicubic interpolation over 4×4 pixel neighborhood.
  • INTER_LANCOZS4 — Lanczos interpolation over 8×8 pixel neighborhood.

Several ways of resizing the images

There are many ways to resize the image. Below are some examples to perform resize operation:

  1. Retain Aspect Ratio ( height to width ratio of the image is retained)
  • Downscale(Decrement in the size of the image)
  • Upscale(Increment in the size of image)

2.Do not preserve Aspect Ratio

  • Resize only the width
  • Resize only the height

3.Resize the specified width and height

I think this blog helps them who are new in OpenCV .

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store