Professional Documents
Culture Documents
Sign:-
Experiment No.2
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:
Parameters:
○ 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.
○ shift- It represents the number of fractional bits in the coordinate of the center and the radius value.
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:
Parameters:
○ 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.
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:
○ 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.
○ 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.
○ 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.
Parameters:
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:
We can write text on the image by using the putText() function. The syntax is given below. 1.
Parameters:
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:
Parameters:
○ img - It represents an 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
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])
Output:
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.
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:
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)
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;
// 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);
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
# importing cv2
import cv2
# path
path = r'C:\Users\user\Desktop\geeks14.png'
Output:
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:
# dilation of images.
import cv2
import numpy as np
img = cv2.imread('input.png', 0)
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
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)
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
plt.show()
Output:
Output: