/  Technology   /  Image Processing Using OpenCV and Python

Image Processing Using OpenCV and Python

What is Image Processing?

Image processing is any form of processing for which the input is an image or a series of images or videos, such as photographs or frames of video. The output of image processing can be either an image or a set of characteristics or parameters related to the image.

Open-CV

OpenCV is a free open source library used in real-time image processing. It mainly focuses on image processing, video capture and analysis including features like face detection and object detection.

Let’s start with the basics.

First install the Open-CV library using command

pip install opencv-python

Let’s see how we can read an image and display an image using Open-CV and python.

1) Reading an Image

Let’s see the syntax for reading an image in our IDE

Syntax

cv2.imread(“parameter 1”,” parameter 2”)

parameter 1: – your image to be read

parameter 2: –

1: Loads a colour image neglecting any transparency of image

0: Loads image in grayscale mode

-1: Loads image as such including alpha channel

 

2) Displaying an Image

Syntax

cv2.imshow(‘Window name’,’ display image’)
cv2.waitKey(0)
cv2.destroyAllWindows()

Let’s see what this code means

cv2.imshow(‘Window name’,’ display image’)

imshow is a method for displaying the image in your output its parameters Window name is used to give the name you want to give to the image and in place of display image provide the variable in which you have stored the path of image.

cv2.waitKey(0)

This line means waiting for some milliseconds for any keyboard event

cv2.destroyAllWindows()

What this line is doing is destroy all the windows that we have created.

Let’s see full code

Syntax

import cv2
img = cv2.imread("C:/Users/Desktop/cute-baby-animals-1558535060.jpg",1)
cv2.imshow('image’, img)
cv2.waitKey(100000)
cv2.destroyAllWindows()

 

Output

 

Image Processing Using OpenCV and Python

 

3) Accessing pixels in an Image

Syntax

pixels = image_name [100,100]
print(pixels)

After you print the pixels you will get the BGR values of the image at that position

Let’s see an example

import cv2
img = cv2.imread("C:/Users/Desktop/cute-baby-animals-1558535060.jpg",1)
pixels = img [100,100]
print(pixels)

 

Output

 

4) Accessing any plane of BGR

Syntax

Blue = image_name [:,:, 0]
print (Blue)

BGR has an index number as 0,1,2 respectively.

Let’s see an example

Syntax

import cv2
img = cv2.imread("C:/Users/Desktop/cute-baby-animals-1558535060.jpg",1)
blue = img[:,:,0]
print(blue)

Output

Image Processing Using OpenCV and Python

Here you can see a blue plane array as it is a huge array it will not be displayed fully. If you want to see the full array you can see using variable explorer.

 

5) Shape and Size of an Image

Syntax

Image_name. shape
Image_name. size

Let’s see an example

Syntax

import cv2
img = cv2.imread("C:/Users/Desktop/cute-baby-animals-1558535060.jpg",1)
print (img. shape)
print (img. size)

Output

 

6) Addition and Subtraction of an image

Now we are going to perform arithmetic operations on an image to keep it simple we will see an example of addition and subtraction. Since we consider images as a matrix we can easily add and subtract it. Let’s see an example

a) ADDITION

Syntax

import cv2
img_1 = cv2.imread("C:/Users/Desktop/vrline21.jpg",1)
img_2 = cv2.imread("C:/Users/Desktop/horizontal.png",1)
img_3 = img_1 + img_2
cv2.imshow("horizontal",img_2)
cv2.imshow("vertical",img_1)
cv2.imshow("Added image”, img_3)
cv2.waitKey(10000)
cv2.destroyAllWindows

Output

Image Processing Using OpenCV and Python

 

b) SUBTRACTION

Syntax

import cv2
img_1 = cv2.imread("C:/Users/suyog/Desktop/vrline21.jpg",1)
img_2 = cv2.imread("C:/Users/suyog/Desktop/horizontal.png",1)
img_3 = img_1 + img_2
img_4 = img_3 - img_2
cv2.imshow("vertical",img_1)
cv2.imshow("horizontal",img_2)
cv2.imshow("Added image",img_3)
cv2.imshow("Subtract image",img_4)
cv2.waitKey(10000)
cv2.destroyAllWindows

Output

Image Processing Using OpenCV and Python

 

7) Image Negative Transformation

This transformation is used for converting the black region into white and viceversa.

S = (L – 1) – R

Where S is the output

R is the input

L-1 is the maximum intensity which is usually 255

Let’s see an example

Syntax

import cv2
img_1 = cv2.imread("C:/Users/Desktop/black.png",1)
img_2 = 255 - img_1
cv2.imshow('Original image',img_1)
cv2.imshow("Negative image",img_2)
cv2.waitKey(10000)
cv2.destroyAllWindows

Output

Image Processing Using OpenCV and Python

 

8) Image Log Transformation

It maps a narrow range of low gray level values in the input image into a wider range of output levels.

Log Transformation is defined by a formula

S =c log(1+r)

Where c is a constant

Let’s see an example

Syntax

import cv2
import numpy as np
img_1 = cv2.imread("C:/Users/suyog/Desktop/black.png",0)
img_2 = np.uint8(np.log1p(img_1))
thresh = 1
img_3 = cv2.threshold(img_2, thresh, 255, cv2.THRESH_BINARY) [1]
cv2.imshow('Original image',img_1)
cv2.imshow("Log image",img_3)
cv2.waitKey(10000)
cv2.destroyAllWindows

To use the mathematical function log we use numpy.

img_2 = np.uint8(np.log1p(img_1))

we have use uint8 to convert it into an integer type and then we use log1p which produces log(1+r).

img_3 = cv2.threshold(img_2, thresh, 255, cv2.THRESH_BINARY) [1]

To view this image we have convert it into a binary image using a threshold function present in cv2 library which takes the arguments as img_2 which is used to convert it into binary. thresh variable is our threshold variable then the maximum intensity 255 then we are converting it into a binary image using THRESH_BINARY function.

Output

In this way we have learn some basics of Image processing using Open-CV and python.

 

 

Leave a comment