You are on page 1of 21

Computer Vision Lab Manual ( Python and Open CV )

Name:- Shalinkumar Solanki Roll no: ITA745 DOS: DOP:

Sign:-
Experiment No.2

Title : Image Processing

A.Image Processing: OpenCV Resize Image, OpenCV Image Rotation

Resize an Image
resize(src, dsize, dst, fx, fy, interpolation)
In general, interpolation is a process of estimating values between known data points.
When graphical data contains a gap, but data is available on either side of the gap or at a few specific points
within the gap. Interpolation allows us to estimate the values within the gap.
In the above resize() function, interpolation flags determine the type of interpolation used for calculating size
of destination image.
Example
Rotate an image
OpenCV uses affine transformation functions for operations on images such as translation and rotation. The
affine transformation is a transformation that can be expressed in the form of a matrix multiplication (linear
transformation) followed by a vector addition (translation).
The cv2 module provides two functions cv2.warpAffine and cv2.warpPerspective, with which you can have
all kinds of transformations. cv2.warpAffine takes a 2x3 transformation matrix while cv2.warpPerspective
takes a 3x3 transformation matrix as input.
To find this transformation matrix for rotation, OpenCV provides a function, cv2.getRotationMatrix2D,
which is as follows −
getRotationMatrix2D(center, angle, scale)
We then apply the warpAffine function to the matrix returned by getRotationMatrix2D() function to obtain
rotated image.
Following program rotates the original image by 90 degrees without changing the dimensions −
Example

b. OpenCV Drawing Functions, Eroding an Image, Blurring an Image, Create Border around
Images, Grayscaling of Images, Scaling, Rotating, Erosion and Dilation of images
OpenCV Drawing Functions

We can draw the various shapes on an image such as circle, rectangle, ellipse, polylines, convex, etc. It is
used when we want to highlight any object in the input image. The OpenCV provides functions for each
shape. Here we will learn about the drawing functions.

Drawing Circle

We can draw the circle on the image by using the cv2.circle() function. The syntax is the following:

1. cv2.circle(img, center, radius, color[,thickness [, lineType[,shift]]])

Parameters:

○ img- It represents the given image.

○ center- Center of the circle

○ radius- Radius of the circle

○ color- Circle color

○ thickness- It denotes the thickness of the circle outline, if it is positive. And negative thickness
means that a filled circle is to be drawn.

○ lineType- Defines the type of the circle boundary.

○ shift- It represents the number of fractional bits in the coordinate of the center and the radius value.

Consider the following example:

1. import numpy as np
2. import cv2
3. img = cv2.imread(r"C:\Users\DEVANSH SHARMA\cat.jpeg",1)
4. cv2.circle(img,(80,80), 55, (0,255,0), -1)
5. cv2.imshow('image',img)
6. cv2.waitKey(0)
7. cv2.destroyAllWindows()

Output:
Drawing Rectangle
The OpenCV provides a function to draw a simple, thick or filled up-right rectangle. The syntax is following:

1. cv2.rectangle(img, pt1, pt2, color[, thickness[,lineType[,shift]]])

Parameters:

○ img- It represents an image.

○ pt1- It denotes vertex of the rectangle.

○ pt2- It denotes the vertex of the rectangle opposite to pt1.

○ color- It denotes the rectangle color of brightness (grayscale image).

○ thickness- It represents the thickness of the lines that makes up the rectangle. Negative values
(CV_FILLED) mean that the function has to draw a filled rectangle.

○ linetype- It represents the types of the line.

○ shift- It represents the number of fractional bits in the point coordinates.

Consider the following example:

1. import numpy as np
2. import cv2
3. img = cv2.imread(r"C:\Users\DEVANSH SHARMA\cat.jpeg",1)
4. cv2.rectangle(img,(15,25),(200,150),(0,255,255),15)
5. cv2.imshow('image',img)
6. cv2.waitKey(0)
7. cv2.destroyAllWindows()
Output:

Drawing Ellipse

We can draw an ellipse on an image by using the cv2.ellipse() function. It can draw a simple or thick elliptic
arc or can fill an ellipse sector.

1. cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]])
2. cv2.ellipse(img, box, color[, thickness[, lineType]])

Parameters:

○ img - It represents an image.

○ box - It represents alternative ellipse representation via RotatedRect or CvBox2D. It means that
the function is used to draws an ellipse in a curved rectangle.

○ color - It denotes the ellipse color.

○ angle- It denotes the angle of rotation.

○ startAngle - It denotes the initial angle of the elliptic arc in degrees.

○ endAngle - It denotes the ending angle of the elliptic arc in degrees.

○ thickness - It is used to draw thickness of the ellipse arc outline if the value is positive. Otherwise,
this specifies that a filled ellipse is to be drawn.

○ lineType - It denotes the type of the ellipse boundary.

○ shift - It represents the number of fractional bits in the coordinates of the center and values of
axes.
Consider the following example:

1. import numpy as np
2. import cv2
3. img = cv2.imread(r"C:\Users\DEVANSH SHARMA\cat.jpeg",1)
4.
5. cv2.ellipse(img, (250, 150), (80, 20), 5, 0, 360, (0,0,255), -1)
6. cv2.imshow('image',img)

7. cv2.waitKey(0)
8. cv2.destroyAllWindows()

Output:

There are two functions to draw the ellipse. The first function is used to draw the whole ellipse, not an arc
bypassing startAngle=0 and endAngle = 360. The second function of an ellipse is used to draw an ellipse
outline, a filled ellipse, an elliptic arc, or a filled ellipse sector.

Drawing lines

OpenCV provides the line() function to draw the line on the image. It draws a line segment between ptr1 and
ptr2 points in the image. The image boundary clips the line.

1. cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]])

Parameters:

○ img- It represents an image.


○ pt1- It denotes the first point of the line segments.
○ pt2- It denotes the second point of the line segment.
○ color - Represents the Line-color
Dr. Sushopti Gawade Computer Vision Lab
Computer Vision Lab Manual ( Python and Open CV )

○ thickness- Represents the Line thickness


○ lineType- There are various types of line:
○ 8 (or omitted) - 8 connected lines.
○ 4 - 4-connected line.
○ CV__AA- antialiased line
○ shift- It represents the number of fractional bits in the point coordinates. Consider the following example:

1. import numpy as np
2. import cv2
3. img = cv2.imread(r"C:\Users\DEVANSH SHARMA\cat.jpeg",1) 4. cv2.line(img,(10,0),(150,150),(0,0,0),15)
5. cv2.imshow('image',img)
6. cv2.waitKey(0)
7. cv2.destroyAllWindows()
Output:

Write Text on Image

We can write text on the image by using the putText() function. The syntax is given below. 1.

cv2.putText(img, text, org, font, color)

Parameters:

○ img: It represents an image


○ text: It represents a text which we want to write on the image.
○ org: It denotes the Bottom-left corner of the text string in the image. ○ font:
CvFont structure is initialized using InitFont().
○ color: Represents the Text color.

Consider the following example.

1. import numpy as np
2. import cv2
3. font = cv2.FONT_HERSHEY_SIMPLEX
4. # Create a black image.
5. img = cv2.imread(r"C:\Users\DEVANSH SHARMA\cat.jpeg",1) 6.
cv2.putText(img,'Hello shalin',(10,500), font, 1,(255,255,255),2) 7. #Display the
image
8. cv2.imshow("image",img)
9. cv2.waitKey(0)

Output:

Drawing Polylines

We can draw the polylines on the image. OpenCV provides the polylines() function, that is used to draw
polygonal curves on the image. The syntax is given below:

1. cv2.polyLine(img, polys, is_closed, color, thickness=1, lineType=8, shift=0)

Parameters:
○ img - It represents an image.

○ pts - It denotes the array of polygon curves.


○ npts - It denotes an array of polygon vertex counters.
○ ncounters - It represents the number of curves.
○ is_Closed - It is a flag that indicates whether the drawn polylines are closed or not. ○ color -
Color of polylines.
○ thickness - It represents the Thickness of the polylines edges.
○ lineType - Type of the line segment.
○ shift- It represents the number of fractional bits in the point coordinates. Consider the

following program to draw polylines in image:

1. import numpy as np
2. import cv2
3. img = cv2.imread(r'C:\Users\DEVANSH SHARMA\forest.jpg',cv2.IMREAD_COLOR) 4. #defining
points for polylines
5. pts = np.array([[100,50],[200,300],[700,200],[500,100]], np.int32)
6. # pts = pts.reshape((-1,1,2))
7. cv2.polylines(img, [pts], True, (0,255,255), 3)
8. cv2.imshow('image',img)
9. cv2.waitKey(0)
10. cv2.destroyAllWindows()

Output:

Eroding an Image.
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. cv2.erode()
method is used to perform erosion on the image. The basic idea of erosion is just like soil erosion only, it
erodes away the boundaries of foreground object (Always try to keep foreground in white). It is normally
performed on binary images. It needs two inputs, one is our original image, second one is called structuring
element or kernel which decides the nature of operation. A pixel in the original image (either 1 or 0) will be
considered 1 only if all the pixels under the kernel is 1, otherwise it is eroded (made to zero).
Python program to explain cv2.erode() method

# importing cv2
import cv2
# importing numpy
import numpy as np
# path
path = r'C:\Users\Rajnish\Desktop\geeksforgeeks\geeks.png'
# Reading an image in default mode
image = cv2.imread(path)
# Window name in which image is displayed
window_name = 'Image'
# Creating kernel
kernel = np.ones((5, 5), np.uint8)
# Using cv2.erode() method
image = cv2.erode(image, kernel)
# Displaying the image
cv2.imshow(window_name, image)
Output:
OpenCV Python Program to blur an image

# Python Program to blur image

# Importing cv2 module


import cv2
# bat.jpg is the batman image.
img = cv2.imread('bat.jpg')

# make sure that you have saved it in the same folder


# You can change the kernel size as you want
blurImg = cv2.blur(img,(10,10))
cv2.imshow('blurred image',blurImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Adding borders to the images

Image processing is an interesting field in today’s era of Artificial Intelligence and Machine Learning. We
can see the applications of image processing in our day-to-day life, like whenever we apply filter over any
image (selfie) or when we want to apply some effect like blurring the image, etc.
In this article, we will discuss how to add borders to an image using Python. Python provides a module
called OpenCV that can be used for the same. So before adding borders let’s see a small introduction about
OpenCV.
OpenCV Open Source Computer Vision Library)
● It is an open-source library.
● Designed to solve Computer Vision problems.
● It makes use of a highly optimized library for numerical operations which is Numpy along with
MATLAB style syntax.

To add borders to the images OpenCV has a package copyMakeBorder which helps to make a border
around the image.
# importing required packages

import cv2
# reading the image
virat_img = cv2.imread('geek.jpg')
# making border around image using copyMakeBorder
borderoutput = cv2.copyMakeBorder(
virat_img, 20, 20, 20, 20, cv2.BORDER_CONSTANT, value=[255, 255, 0])

# showing the image with border


cv2.imwrite('output.png', borderoutput)

Output:

Python | Grayscaling of Images using OpenCV


Grayscaling is the process of converting an image from other color spaces e.g. RGB, CMYK, HSV, etc.
to shades of gray. It varies between complete black and complete white.

Importance of grayscaling

● Dimension reduction: For example, In RGB images there are three color channels and three
dimensions while grayscale images are single-dimensional.
● Reduces model complexity: Consider training neural articles on RGB images of 10x10x3 pixels.
The input layer will have 300 input nodes. On the other hand, the same neural network will need
only 100 input nodes for grayscale images.
● For other algorithms to work: Many algorithms are customized to work only on grayscale images
e.g. Canny edge detection function pre-implemented in theOpenCV library works on Grayscale
images only.

Method 1: Using the cv2.cvtColor() function

Import the OpenCV and read the original image using imread() than convert to grayscale using
cv2.cvtcolor() function. destroyAllWindows() function allows users to destroy or close all windows at any
time after exiting the script.
# import opencv
import cv2
# Load the input image
image = cv2.imread('C:\\Documents\\full_path\\tomatoes.jpg')
cv2.imshow('Original', image)
cv2.waitKey(0)
# Use the cvtColor() function to grayscale the image
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale', gray_image)
cv2.waitKey(0)
# Window shown waits for any key pressing event
cv2.destroyAllWindows()
Input image:
Output Image:

Dr. Sushopti Gawade Computer Vision Lab


Computer Vision Lab Manual ( Python and Open CV )

Scaling

You can perform scaling on an image using the resize() method of the imgproc class. Following is the syntax
of this method.
resize(Mat src, Mat dst, Size dsize, double fx, double fy, int interpolation)

This method accepts the following parameters −


src − A Mat object representing the source (input image) for this operation. dst − A Mat
object representing the destination (output image) for this operation. dsize − A Size object
representing the size of the output image.
fx − A variable of the type double representing the scale factor along the
horizontal axis.
fy − A variable of the type double representing the scale factor along the vertical axis.
Interpolation − An integer variable representing interpolation method.

Example
The following program demonstrates how to apply scale transformation to an image.

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;

import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class Scaling {


public static void main(String args[]) {
// Loading the OpenCV core library
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

// Reading the Image from the file and storing it in to a Matrix object
String file ="E:/OpenCV/chap24/transform_input.jpg";
Mat src = Imgcodecs.imread(file);

// Creating an empty matrix to store the result


Mat dst = new Mat();

// Creating the Size object

Size size = new Size(src.rows()*2, src.rows()*2);

// Scaling the Image


Imgproc.resize(src, dst, size, 0, 0, Imgproc.INTER_AREA);

// Writing the image


Imgcodecs.imwrite("E:/OpenCV/chap24/scale_output.jpg", dst);

System.out.println("Image Processed");
}
}
Assume that following is the input image transform_input.jpg specified in the above program (size −
Width:300px and height:300px).

Rotating

Image used for all the below examples:

# Python program to explain cv2.rotate() method

# importing cv2
import cv2

# path
path = r'C:\Users\user\Desktop\geeks14.png'

# Reading an image in default mode


src = cv2.imread(path)
# Window name in which image is displayed
window_name = 'Image'

# Using cv2.rotate() method


# Using cv2.ROTATE_90_CLOCKWISE rotate
# by 90 degrees clockwise
image = cv2.rotate(src, cv2.ROTATE_90_CLOCKWISE)

# Displaying the image


cv2.imshow(window_name, image)
cv2.waitKey(0)

Output:

Erosion and Dilation of images using OpenCV in python


Morphological operations are a set of operations that process images based on shapes. They apply a
structuring element to an input image and generate an output image. The most basic morphological
operations are two: Erosion and Dilation
Basics of Erosion:
● Erodes away the boundaries of the foreground object
● Used to diminish the features of an image.

Working of erosion:
1. A kernel(a matrix of odd size(3,5,7) is convolved with the image.
2. A pixel in the original image (either 1 or 0) will be considered 1 only if all the pixels under the
kernel are 1, otherwise, it is eroded (made to zero).
3. Thus all the pixels near the boundary will be discarded depending upon the size of the kernel.
4. So the thickness or size of the foreground object decreases or simply the white region decreases in
the image.

Basics of dilation:
● Increases the object area
● Used to accentuate features

Working of dilation:

1. A kernel(a matrix of odd size(3,5,7) is convolved with the image


2. A pixel element in the original image is ‘1’ if at least one pixel under the kernel is ‘1’. 3. It
increases the white region in the image or the size of the foreground object increases

# Python program to demonstrate erosion and

# dilation of images.

import cv2

import numpy as np

# Reading the input image

img = cv2.imread('input.png', 0)

# Taking a matrix of size 5 as the kernel

kernel = np.ones((5, 5), np.uint8)

# The first parameter is the original image,

# kernel is the matrix with which image is

# convolved and third parameter is the number

# of iterations, which will determine how much

# you want to erode/dilate a given image.

img_erosion = cv2.erode(img, kernel, iterations=1)

img_dilation = cv2.dilate(img, kernel, iterations=1)

cv2.imshow('Input', img)

cv2.imshow('Erosion', img_erosion)
Dr. Sushopti Gawade Computer Vision Lab
Computer Vision Lab Manual ( Python and Open CV )

cv2.imshow('Dilation', img_dilation)

cv2.waitKey(0)

The second image is the eroded form of the original image and the third image is the dilated
form.

c. Convert an image from one color space to another, Filter Color with OpenCV Denoising
of colored images, Visualizing image in different color spaces

Convert an image from one color space to another


An RGB (colored) image has three channels, Red, Blue and Green. A colored image in OpenCV has a shape
in [H, W, C] format, where H, W, and C are image height, width and number of channels. All three channels
have a value range between 0 and 255.
The HSV image also has three channels, the Hue, Saturation and Value channels. In OpenCV, the values of
the Hue channel range from 0 to 179, whereas the Saturation and Value channels range from 0 to 255.
In OpenCV, to convert an RGB image to HSV image, we use the cv2.cvtColor() function. This function is
used to convert an image from one color space to another.
This function takes two arguments− first the input image and second the color conversion method. See the
syntax given below −

Dr. Sushopti Gawade Computer Vision Lab


Computer Vision Lab Manual ( Python and Open CV )

cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)

Steps
To convert an RGB image to HSV image, follow the steps given below −
Import the required library. In all the following Python examples, the required Python library is OpenCV.
Make sure you have already installed it.
import cv2

Read the input RGB image using cv2.imread(). The RGB image read using this method is in BGR format.
Optionally assign the read BGR image to bgr_img.
bgr_img = cv2.imread('water.jpg')

Now convert this BGR image to HSV image as below using cv2.cvtColor() function. Optionally assign the
converted HSV image to hsv_img.
hsv_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2HSV)

Display the above converted HSV image.


cv2.imshow('HSV image', hsv_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Filter Color with OpenCV Denoising of colored images

Denoising of an image refers to the process of reconstruction of a signal from noisy images. Denoising is
done to remove unwanted noise from image to analyze it in better form. It refers to one of the major pre-
processing steps. There are four functions in opencv which is used for denoising of different images.

# importing libraries
import numpy as np
import cv2
from matplotlib import pyplot as plt

# Reading image from folder where it is stored


img = cv2.imread('bear.png')

# denoising of image saving it into dst image


dst = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 15)

# Plotting of source and destination image


plt.subplot(121), plt.imshow(img)
plt.subplot(122), plt.imshow(dst)

plt.show()
Output:

Visualizing image in different color spaces


OpenCV (Open Source Computer Vision) is a computer vision library that contains various functions to
perform operations on pictures or videos. It was originally developed by Intel but was later maintained by
Willow Garage and is now maintained by Itseez. This library is cross-platform that is it is available on
multiple programming languages such as Python, C++ etc.
Let’s discuss different ways to visualize images where we will represent images in different formats
like grayscale, RGB scale, Hot_map, edge map, Spectral map, etc.
RGB Image :
RGB image is represented by linear combination of 3 different channels which are R(Red), G(Green) and
B(Blue). Pixel intensities in this color space are represented by values ranging from 0 to 255 for single
channel. Thus, number of possibilities for one color represented by a pixel is 16 million approximately
[255 x 255 x 255 ].
# Python program to read image as RGB

# Importing cv2 and matplotlib module


import cv2
import matplotlib.pyplot as plt

# reads image as RGB


img = cv2.imread('g4g.png')
# shows the image
plt.imshow(img)

Output:

You might also like