You are on page 1of 10

COMPUTER VISION

INTERNAL ASSESSMENT 1

SUBMITTED BY:
AGATHIYAN S
18352204
28/11/2020
1.GAUSSIAN BLUR:

Gaussian blur is the result of blurring an image by a


Gaussian function. It is a widely used effect in graphics software,
typically to reduce image noise and reduce detail. It is also used
as a pre-processing stage before applying our machine learning or
deep learning models.
E.g. of a Gaussian kernel (5×5)
Code:
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('butterfly.jpg')

blur = cv2.GaussianBlur(img, (5, 5), 0)


plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(blur),plt.title('Blurred')
plt.xticks([]), plt.yticks([])
plt.show()

Output:
IMAGE CONTOURS:

Contours can be explained simply as a curve joining all the


continuous points (along the boundary), having same color or
intensity. The contours are a useful tool for shape analysis and
object detection and recognition.

 For better accuracy, use binary images. So before finding


contours, apply threshold or canny edge detection.
 findContours function modifies the source image. So if you
want source image even after finding contours, already store
it to some other variables.
 In OpenCV, finding contours is like finding white object
from black background. So remember, object to be found
should be white and background should be black.
Code:
import numpy as np
import cv2

im = cv2.imread('b.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
image, contours, hierarchy =
cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

To draw the contours, cv2.drawContours function is used.


It can also be used to draw any shape provided you have its
boundary points. Its first argument is source image, second
argument is the contours which should be passed as a Python list,
third argument is index of contours (useful when drawing
individual contour. To draw all contours, pass -1) and remaining
arguments are colour, thickness etc.
2. UNSHARP MASKING OPERATION:

Unsharp masking (USM) is an image sharpening technique,


often available in digital image processing software. Its name
derives from the fact that the technique uses a blurred, or
"unsharp", negative image to create a mask of the original
image.
The unsharp mask is then combined with the original
positive image, creating an image that is less blurry than the
original. The resulting image, although clearer, may be a less
accurate representation of the image's subject.

In the context of signal processing, an unsharp mask is


generally a linear or nonlinear filter that amplifies the high-
frequency components of a signal.

Code:
COLOR IMAGES:
import cv2

image = cv2.imread("butterfly.jpg")
(or)
image = cv2.imread("butterfly.jpg", cv2.IMREAD_GRAYSCALE)
gaussian_3 = cv2.GaussianBlur(image, (0, 0), 2.0)
unsharp_image = cv2.addWeighted(image, 1.5, gaussian_3, -0.5, 0,
image)
sub = cv2.subtract(gaussian_3, unsharp_image)
cv2.imshow('Original', gaussian_3)
cv2.imshow('UnSharp', unsharp_image)
cv2.imshow('Subtract', sub)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Output:
GRAYSCALE IMAGES

3. IMAGE GRADIENTS:
An image gradient is a directional change in the intensity or
colour in an image. The gradient of the image is one of the
fundamental building blocks in image processing.
For example, the Canny edge detector uses image gradient
for edge detection.

EDGE DETECTION:

Edge detection is one of the fundamental operations when


we perform image processing. It helps us reduce the amount of
data (pixels) to process and maintains the structural aspect of the
image. We're going to look into two commonly used edge
detection schemes - the gradient (Sobel - first order derivatives)
based edge detector and the Laplacian (2nd order derivative, so
it is extremely sensitive to noise) based edge detector. Both of
them work with convolutions and achieve the same end goal -
Edge Detection.

SOBEL EDGE DETECTION:

Sobel edge detector is a gradient based method based on


the first order derivatives. It calculates the first derivatives of the
image separately for the X and Y axes.

The operator uses two 3X3 kernels which are convolved


with the original image to calculate approximations of the
derivatives - one for horizontal changes, and one for vertical.
The picture below shows Sobel Kernels in x-dir and y-dir:

Syntax:

cv2.Sobel(src, ddepth, other_options...)

LAPLACIAN EDGE DETECTION:

the Laplacian edge detector uses only one kernel. It


calculates second order derivatives in a single pass.

Syntax:

cv2.Laplacian(src, ddepth, other_options...)

Code:
import cv2
import numpy as np
from matplotlib import pyplot as plt

image = cv2.imread("S.jpg",cv2.IMREAD_GRAYSCALE)

lap = cv2.Laplacian(image, cv2.CV_64F, ksize= 3)


sobelX = cv2.Sobel(image, cv2.CV_64F, 1, 0)
sobelY = cv2.Sobel(image, cv2.CV_64F, 0, 1)

lap = np.uint8(np.absolute((lap)))
sobelX = np.uint8(np.absolute((sobelX)))
sobelY = np.uint8(np.absolute((sobelY)))

sobelCombined = cv2.bitwise_or(sobelX, sobelY)

titles = ['Image', 'Laplacian', 'SobelX', 'SobelY',


'SobelCombined']
images = [image, lap, sobelX, sobelY, sobelCombined]

for i in range(5):
plt.subplot(2, 3, i+1),plt.imshow(images[i], 'gray')
plt.title(titles[i])
plt.xticks([]),plt.yticks([])
plt.show()

Output:

You might also like