Digital Image Processing Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

Dept.

of Information and Communication Technology


Islamic University, Bangladesh

Lab Report On
Course No: ICT - 4206
Course Title: Digital Image Processing Laboratory

SUBMITTED TO,

Dr. Md. Zahidul Islam


Professor
Dept. of Information & Communication
Technology

SUBMITTED BY,

Name: Md. Abu Jubaer Rupok


Roll: 1718002
Reg. No: 1313
Session: 2017-18
INDEX

SI.NO. Experiment Name Page No.

Introduction 3–6

01 To create an opencv-python program to get a gradient image from an 7–9


RGB/grayscale image.
02 To create an opencv-python program to show a histogram of an RGB/grayscale 10 – 13
image.
03 To create an opencv-python program in order to convert a given 14 – 15
RGB/grayscale image to a negative image
04 To create an opencv-python program to enhance color of a given 16 – 17
RGB/grayscale image
05 To create an opencv-python program to enhance brightness of a given 18 – 19
RGB/grayscale image
06 To create an opencv-python program to enhance contrast of a given 20 – 21
RGB/grayscale image.
07 To create an opencv-python program to segment a given RGB/grayscale image 22 – 23
based on thresholding.
08 To create an opencv-python program to segment a given RGB/grayscale image 24 – 25
based on the contour detection algorithm.
09 To create an opencv-python program to segment a given RGB/grayscale image 26 – 28
based on K-means algorithm
10 To create an opencv-python program to sharp a given RGB/grayscale image. 29 – 30

11 To create an opencv-python program to skeletonize a given RGB/grayscale 31 – 32


image
12 To create an opencv-python program to smooth a given RGB/grayscale image 33 – 34
by a 2Dfilter.
13 To create an opencv-python program to smooth a given RGB/grayscale image 35 – 36
by an average filter
14 To create an opencv-python program to smooth a given RGB/grayscale image 37 – 38
by a Gaussian filter
15 To create an opencv-python program to smooth a given RGB/grayscale image 39 – 40
by a median filter
16 To create an opencv-python program to smooth a given RGB/grayscale image 41 – 42
by a bilateral filter

2
Introduction:-
To introduce students with python programming language, & OpenCV :

To install Python on your system, follow these steps:


1. Go to the official Python website at https://www.python.org/downloads/ and download the latest
version of Python.

2. Double click on the downloaded file and install Python for all users, and ensure that Python
is added to your path. Click on Install now to begin. Adding Python to the path will enable us to
use the Python interpreter from any part of the filesystem.

3
3. The Optional Features include common tools and resources for Python and you can install all of them, even
if you don’t plan to use them.

Select some or all of the following options:

➢ Documentation: recommended
➢ pip: recommended if you want to install other Python packages, such as NumPy or pandas
➢ tcl/tk and IDLE: recommended if you plan to use IDLE or follow tutorials that use it
➢ Python test suite: recommended for testing and learning
➢ py launcher and for all users: recommended to enable users to launch Python from the
command line

4. Click next
5. The Advanced Options dialog displays.

4
6. Click Install to start the installation.

7. After the installation is complete, a Setup was successful message displays.

To start writing Python code, you can use a text editor or integrated development environment (IDE)
such as Visual Studio Code, PyCharm, or IDLE.

5
OpenCV:

Open-Source Computer Vision Library (OpenCV)is an open source software library written in
C++ for machine learning and computer vision applications.
To install packages in PyCharm Click, Files > Settings > Project: Name > Project Interpreter
Now click on ‘+’ icon to install new packages

Now search for following packages and install them by clicking ‘Install Package’

• opencv-python

• numpy // Used for numerical operations

• matplotlib // Used to plotting graphs

• opencv-contrib-python

And finally, click apply and Ok. Then, run the python code

6
Experiment No: 01

Experiment Name: To create an opencv-python program to get a gradient image from an


RGB/grayscale image.

Theory:

In computer vision, gradient images are often used to detect edges and other important features in an
image. In this lab report, we will learn how to use OpenCV-Python to create a gradient image from an
RGB or grayscale image. We will also discuss the theory behind gradient images and how they are
computed.

A gradient image is an image where each pixel represents the strength and direction of the change in
intensity of the original image. In other words, a gradient image contains information about the rate of
change of intensity in different directions.
The gradient of an image can be computed using mathematical operators like Sobel, Scharr, or
Laplacian. These operators calculate the difference between adjacent pixels in the x and y directions to
detect edges and other important features in an image.

Implementation:
In this code, we first load the input image using cv2.imread(). Then, we convert the image to grayscale
using cv2.cvtColor(). We then calculate the gradient using the Sobel operator in both the x and y
directions using cv2.Sobel(). Finally, we combine the gradients in both directions using
cv2.addWeighted() to obtain the final gradient image. We display both the original and gradient images
using cv2.imshow() and wait for a key press to close the windows using cv2.waitKey() and
cv2.destroyAllWindows().

Python Code: (RGB)


import cv2
import numpy as np

# Load the image


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

# Convert the image to grayscale


gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Calculate the gradient using the Sobel operator


grad_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
grad_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
grad = cv2.addWeighted(grad_x, 0.5, grad_y, 0.5, 0)

# Display the original and gradient images


cv2.imshow('Original Image', img)
cv2.imshow('Gradient Image', grad)
cv2.waitKey(0)
cv2.destroyAllWindows()

7
Output:
Original Image:

Gradient Image

8
Here's the code for grayscale images:
import cv2
import numpy as np

# Load the grayscale image


img = cv2.imread('gdr.jpg', cv2.IMREAD_GRAYSCALE)

# Calculate the gradient using the Sobel operator


grad_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=3)
grad_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=3)
grad = cv2.addWeighted(grad_x, 0.5, grad_y, 0.5, 0)

# Display the original and gradient images


cv2.imshow('Original Image', img)
cv2.imshow('Gradient Image', grad)
cv2.waitKey(0)
cv2.destroyAllWindows()

The only difference in this code is that we load the image as grayscale using the
cv2.IMREAD_GRAYSCALE flag in cv2.imread(). The rest of the code is the same as before.

Grayscale Image

Gradient Image

9
Experiment No: 02
Experiment Name: To create an opencv-python program to show a histogram of an RGB/grayscale
image

Theory:
A histogram is a graphical representation of the distribution of values in a dataset. In the context of
digital images, a histogram is a plot that shows the frequency of occurrence of pixel intensity values in
an image. The x-axis of the histogram represents the possible pixel intensity values, while the y-axis
represents the frequency or number of pixels that have that intensity value.
RGB Image Histogram:
For an RGB image, the histogram is often split into three separate graphs, one for each color channel:
red, green, and blue. The red channel graph represents the distribution of red pixel intensities, the green
channel graph represents the distribution of green pixel intensities, and the blue channel graph
represents the distribution of blue pixel intensities.
In the RGB image, each pixel is represented by a combination of red, green, and blue intensities, with
each intensity value ranging from 0 to 255. The histogram for each channel shows the number of pixels
that have a specific intensity value. For example, if there are many pixels with a high red intensity
value, then the red channel graph will have a peak at that intensity value.

Grayscale Image Histogram:


For a grayscale image, the histogram shows the distribution of pixel intensities from 0 to 255. Similar
to the RGB image histogram, the x-axis represents the intensity values, and the y-axis represents the
number of pixels that have that intensity value.
The histogram of a grayscale image provides information about the contrast of the image. An image
with low contrast has a narrow histogram that is concentrated around a few intensity values, while an
image with high contrast has a wide histogram that is spread out across many intensity values.

Histogram Equalization:
Histogram equalization is a technique used to improve the contrast of an image. It works by
redistributing the pixel intensities in the image to span a wider range of intensity values. This is
achieved by applying a transformation function to the image, which maps the original pixel intensities
to new, more evenly distributed intensities. The result is an image with improved contrast and visual
quality.

Implementation:
In this code, we first load the input image using cv2.imread(). Then, we calculate the histogram for
each channel using cv2.calcHist(). Finally, we plot the histograms for each channel using plt.plot(). We
set the x-limit to [0, 256] using plt.xlim(), set a title using plt.title(), and display the plot using
plt.show().

10
Python Code:
Here's the code for RGB images:
import cv2
import numpy as np
from matplotlib import pyplot as plt

# Load the image


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

# Calculate the histogram for each channel


hist_b = cv2.calcHist([img], [0], None, [256], [0, 256])
hist_g = cv2.calcHist([img], [1], None, [256], [0, 256])
hist_r = cv2.calcHist([img], [2], None, [256], [0, 256])

# Plot the histograms


plt.plot(hist_b, color='b')
plt.plot(hist_g, color='g')
plt.plot(hist_r, color='r')
plt.xlim([0, 256])
plt.title('Histogram')
plt.show()

Output:
Original Image:

11
Histogram (RGB):

Here's the code for grayscale images:


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

# Load the grayscale image


img = cv2.imread('high_contrast.png', cv2.IMREAD_GRAYSCALE)

# Calculate the histogram


hist = cv2.calcHist([img], [0], None, [256], [0, 256])

# Plot the histogram


plt.plot(hist, color='k')
plt.xlim([0, 256])
plt.title('Histogram')
plt.show()

12
The only difference in this code is that we load the image as grayscale using the
cv2.IMREAD_GRAYSCALE flag in cv2.imread(). The rest of the code is the same as before

Original Image:

Histogram (RGB):

13
Experiment No: 03
Experiment Name: To create an opencv-python program in order to convert a given
RGB/grayscale image to a negative image.

Theory:

Image negative:
An image negative is a version of an image in which the colors or grayscale values are inverted or
reversed. In other words, the negative of an image has the opposite colors or grayscale values of the
original image. For example, in a grayscale image, the darkest pixels become the brightest, and the
brightest pixels become the darkest, while all other grayscale values are reversed accordingly. Similarly,
in a color image, the red, green, and blue color channels are inverted, resulting in an image with the
opposite color scheme.
Image negatives are commonly used in photography and image processing to create artistic effects,
enhance image contrast, or correct color balance.
To convert an RGB or grayscale image to a negative image, we invert the pixel values. In other words,
we subtract the pixel value from the maximum value possible. For a grayscale image, the maximum
value is 255, so to get the negative image, we subtract each pixel value from 255. For an RGB image,
we do the same for each color channel.

14
Python Code:
import cv2

# Load the image


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

# Convert the image to its negative


negative_img = 255 - img

# Display the original and negative images


cv2.imshow('Original Image', img)
cv2.imshow('Negative Image', negative_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Original image: Image Negative:

15
Experiment No: 04

Experiment Name: To create an opencv-python program to enhance color of a given


RGB/grayscale image.

Theory:

Image Enhancement: Image enhancement refers to the process of improving the visual quality of an
image. It involves techniques that can be used to improve the contrast, brightness, sharpness, color, and
other visual properties of an image. Image enhancement is often used to improve the quality of images
captured in low light conditions or to extract more information from images for analysis or
visualization.

There are various techniques used for image enhancement, some of which are:

• Contrast enhancement: Contrast is the difference in brightness between different parts of an


image. Contrast enhancement involves increasing the difference between the light and dark
areas of an image to make it appear sharper and more distinct.

• Color adjustment: Color adjustment involves changing the color balance of an image to improve
its visual appeal. This can be done by adjusting the brightness, saturation, or hue of an image.

• Sharpening: Sharpening is a technique used to enhance the edges in an image, making them
more distinct and clearer. This can be done by increasing the contrast of the edges or by using
filters to enhance the sharpness of the image.

Image enhancement can be performed manually or automatically, depending on the application and the
desired outcome. It is commonly used in fields such as medical imaging, forensic analysis, and digital
photography, among others.

16
Python Code:
from PIL import Image
from PIL import ImageEnhance

# Opens the image file


image = Image.open('gg.jpg')

# shows image in image viewer


image.show()

# Enhance Color Level


curr_col = ImageEnhance.Color(image)
new_col = 2.5

# Color level enhanced by a factor of 2.5


img_colored = curr_col.enhance(new_col)

# shows updated image in image viewer


img_colored.show()

Output:

Original Image Color Image

17
Experiment No: 05
Experiment Name: To create an opencv-python program to enhance brightness of a given
RGB/grayscale image.

Theory:

Brightness: Brightness is a visual perception of how much light an object emits or reflects, and it is
an important attribute in image enhancement. In image processing, brightness is often used to refer to
the overall level of lightness or darkness in an image. It is controlled by adjusting the intensity values
of the pixels in the image. Increasing the brightness of an image makes it appear lighter and more
washed out, while decreasing the brightness makes it appear darker and more saturated.

Brightness adjustments are commonly used in image enhancement to improve the visibility of details,
enhance contrast, or correct exposure problems. They can be performed by changing the overall
intensity of the image, or by selectively adjusting the brightness of specific regions or objects within
the image. Brightness adjustments can be made manually using software tools like image editors or
automated using algorithms that analyze the image and make adjustments based on predefined rules or
criteria.

Python Code:
from PIL import Image
from PIL import ImageEnhance

# Opens the image file


image = Image.open('gg.jpg')

# shows image in image viewer


image.show()

# Enhance Brightness
curr_bri = ImageEnhance.Brightness(image)
new_bri = 2.5

# Brightness enhanced by a factor of 2.5


img_brightened = curr_bri.enhance(new_bri)

# shows updated image in image viewer


img_brightened.show()

18
Output:

Original Image Brightness Image

19
Experiment No: 06

Experiment Name: To create an opencv-python program to enhance contrast of a given


RGB/grayscale image.

Theory:

Contrast: In image enhancement, contrast refers to the difference in brightness and color intensity
between different parts of an image. Increasing contrast can make an image appear more vivid and
improve its visual appeal.

An image with high contrast has a large difference between the lightest and darkest parts of the image,
while an image with low contrast has a smaller difference. Increasing contrast can help bring out details
in the image, making it appear sharper and more defined. Conversely, decreasing contrast can make an
image appear softer and more subdued.

Contrast can be adjusted using various image editing tools such as brightness and contrast adjustments,
curves adjustments, and levels adjustments. These tools allow users to manipulate the brightness and
darkness of different areas of an image, and adjust the overall contrast to achieve the desired effect.

Python Code:
from PIL import Image
from PIL import ImageEnhance

# Opens the image file


image = Image.open('gg.jpg')

# shows image in image viewer


image.show()

# Enhance Contrast
curr_con = ImageEnhance.Contrast(image)
#new_con = 0.3
new_con = 8.3

# Contrast enhanced by a factor of 0.3


img_contrasted = curr_con.enhance(new_con)

# shows updated image in image viewer


img_contrasted.show()

20
Output:

Original Image Contrast Image

21
Experiment No: 07

Experiment Name: To create an opencv-python program to segment a given RGB/grayscale


image based on thresholding.

Theory:

Image Segmentation: Image segmentation is the process of dividing an image into multiple
regions or segments, each of which corresponds to a distinct object or region of interest within the
image. The goal of image segmentation is to simplify and/or change the representation of an image into
something more meaningful and easier to analyze.

Image segmentation is an important technique in computer vision and image processing, and is used in
a variety of applications such as medical image analysis, object recognition and tracking, and scene
understanding in autonomous vehicles.

There are several methods for performing image segmentation, including thresholding, edge detection,
region growing, and clustering. Each method has its own strengths and weaknesses and the choice of
method depends on the specific application and the characteristics of the image being segmented.

Thresholding: Thresholding is a simple yet powerful technique in image segmentation that is used
to separate an image into different regions based on the pixel intensity values. The basic idea behind
thresholding is to convert a grayscale or color image into a binary image by assigning a pixel value of
1 (white) to pixels whose intensity values are above a certain threshold, and a pixel value of 0 (black)
to pixels whose intensity values are below the threshold.

Implementation:
In this code, we first load the input image using cv2.imread(). Then, we convert it to grayscale using
cv2.cvtColor(). After that, we apply thresholding using cv2.threshold(). We pass the grayscale image,
the threshold value, the maximum value (which is 255 for an 8-bit image), and the thresholding type
(which is cv2.THRESH_BINARY in this case). Finally, we display the thresholded image using
cv2.imshow(), wait for a key press using cv2.waitKey(), and destroy all windows using
cv2.destroyAllWindows().

22
Python Code:
Here's the code for RGB images:
import cv2

# Load the image


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

# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply thresholding
threshold_value = 127 # Set your threshold value here
ret, thresh = cv2.threshold(gray, threshold_value, 255, cv2.THRESH_BINARY)

# Display the result


cv2.imshow('Thresholded Image', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Thresholded Image

23
Experiment No: 08

Experiment Name: To create an opencv-python program to segment a given


RGB/grayscale image based on the contour detection algorithm.

Theory:

Contour Detection Algorithm: Contour detection is a technique in image segmentation that is


used to identify the boundaries of objects or regions in an image. The contours are the continuous lines
or curves that represent the edges of objects or regions within an image.

There are several algorithms that can be used for contour detection, including Canny edge detection,
Sobel edge detection, and Laplacian edge detection. These algorithms work by identifying points in the
image where there is a sudden change in brightness or color, which typically occur at the edges of
objects or regions.

The Canny edge detection algorithm is one of the most widely used algorithms for contour detection.
It involves several steps, including:

• Smoothing the image to reduce noise and spurious edges


• Computing the gradient magnitude and orientation at each pixel using derivative filters
• Applying non-maximum suppression to thin the edges to a single pixel width
• Applying hysteresis thresholding to link the remaining edges into continuous contours.

Python Code:
import cv2

# Load the image


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

# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply thresholding
threshold_value = 127 # Set your threshold value here
ret, thresh = cv2.threshold(gray, threshold_value, 255, cv2.THRESH_BINARY)

# Find contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)

# Draw contours on original image


contour_img = cv2.drawContours(img, contours, -1, (0,255,0), 3)

24
# Display the result
cv2.imshow('Contour Image', contour_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Original Image

Contour Detection Image

25
Experiment No: 09

Experiment Name: To create an opencv-python program to segment a given RGB/grayscale


image based on K-means algorithm.

Theory:

K-means algorithm:

K-means algorithm is a clustering technique that can be used for image segmentation. In image
segmentation, the goal is to divide an image into different regions or segments such that each segment
contains pixels with similar properties, such as color or texture. K-means algorithm can be used to
group pixels with similar properties into clusters, which can then be used to define the different
segments of the image.

The K-means algorithm works by first selecting a number of clusters (K) and initializing them with
random pixel values. Each pixel in the image is then assigned to the nearest cluster based on its color
or texture properties. The mean of each cluster is then calculated, and the pixel assignments are updated
based on the new cluster means. This process is repeated until convergence, which is typically achieved
when the cluster means no longer change significantly.

To implement K-means algorithm, you can follow these general steps:

• Choose the number of clusters K that you want to form.

• Initialize the K cluster centers randomly. This can be done by selecting K pixels at random from
the image or by generating K random color or texture values.

• Assign each pixel in the image to the nearest cluster center. This can be done by computing the
distance between each pixel and each cluster center and assigning the pixel to the cluster with
the closest center.

• Recalculate the cluster centers by taking the mean color or texture value of all pixels assigned
to each cluster.

• Repeat steps 3 and 4 until the cluster centers no longer change significantly, or until a maximum
number of iterations is reached.

• Assign each pixel in the image to the final cluster that it belongs to.

26
Python Code:
import cv2
import numpy as np

# Load the image


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

# Reshape the image to a 2D array of pixels and 3 color channels


pixel_vals = img.reshape((-1, 3))

# Convert the pixel values to float


pixel_vals = np.float32(pixel_vals)

# Set the parameters for K-means clustering


criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
k = 8 # Set the number of clusters here

# Apply K-means clustering


ret, labels, centers = cv2.kmeans(pixel_vals, k, None, criteria, 10,
cv2.KMEANS_RANDOM_CENTERS)

# Convert the centers to uint8


centers = np.uint8(centers)

# Reshape the labels to the original image shape


labels = labels.reshape((img.shape[0], img.shape[1]))

# Create a mask image based on the labels


mask = np.zeros_like(img)

for i in range(k):
mask[labels == i] = centers[i]

# Display the result


cv2.imshow('Segmented Image', mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

27
Output:

Original Image

Segmented Image (k-means):

28
Experiment No: 10

Experiment Name: To create an opencv-python program to sharp a given RGB/grayscale image.

Theory:

Image Sharpening: Image sharpening is a technique in image processing that enhances the edges and
details of an image, making it appear clearer and more defined. The goal of image sharpening is to
improve the visual quality of an image by emphasizing the edges and edges details, which may have
been lost due to factors such as blurring, noise, or low resolution.

Image sharpening is widely used in various applications, including photography, medical imaging, and
computer vision.

Sharpen Filter: A sharpen filter is a type of image filter used in image processing to enhance the edges
and details of an image, making it appear clearer and more defined. There are several types of sharpen
filters, but one of the most common is the Laplacian filter. The Laplacian filter works by computing
the second derivative of the image intensity with respect to position, which highlights regions of the
image where the intensity changes rapidly, such as edges and corners.
To implementing image sharpening using the Laplacian filter:

• Load the image that you want to sharpen using an image processing library such as OpenCV or
Pillow.

• Convert the image to grayscale, as sharpening is often applied to the luminance (brightness)
component of an image rather than the color components.

• Apply a Gaussian blur to the image to remove noise and other high-frequency components that
may interfere with the sharpening process.

• Apply the Laplacian filter to the image using convolution. The Laplacian filter can be
implemented as a kernel, which is a small matrix of numbers that is used to modify the values
of neighboring pixels.

• Add the Laplacian-filtered image back to the original image using a suitable weighting factor,
such as 0.5 or 0.7. This will enhance the edges and details in the image while preserving the
overall appearance.

29
Python Code:
img = cv2.imread('shrp.jpg')

# Create a sharpening kernel


kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])

# Apply the kernel to the input image


sharpened_img = cv2.filter2D(img, -1, kernel)

# Display the result


cv2.imshow('Sharpened Image', sharpened_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:
Original Image:

Sharpned Image:

30
Experiment No: 11

Experiment Name: To create an opencv-python program to skeletonize a given RGB/grayscale


image.

Theory:

Skeletonize: Skeletonization, also known as thinning, is a morphological operation used to extract the
essential features of an object in an image while preserving its shape and topology. It involves
iteratively removing pixels from the boundary of the object until only a one-pixel-wide skeletonized
version of the object remains. The result is a simplified representation of the object that can be used for
further analysis or processing. Skeletonization is commonly used in image processing for applications
such as object recognition, shape analysis, and pattern matching.

Algorithm:
1. Import necessary libraries: PIL, cv2, matplotlib.pyplot, and numpy.
2. Define a function named "skeletonize" that takes an input image as an argument.
3. Create a structuring element using cv2.getStructuringElement() function.
4. Create an empty array with the same size and data type as the input image using np.zeros()
function.
5. Set a flag variable "flag" to zero.
6. Create a while loop with condition "while (not flag)".
7. Inside the while loop, erode the input image using cv2.erode() function and the structuring
element.
8. Dilate the eroded image using cv2.dilate() function and the structuring element.
9. Subtract the dilated image from the input image using cv2.subtract() function.
10. Use cv2.bitwise_or() function to apply the opened image to the output image.
11. Copy the eroded image to the input image.
12. Calculate the number of zeros in the input image using cv2.countNonZero() function.
13. If the number of zeros is equal to the size of the image, set the flag variable to 1, else set it to 0.
14. Create a kernel using np.ones() function.
15. Dilate the output image using cv2.dilate() function and the kernel.
16. Apply median blur to the dilated image using cv2.medianBlur() function and kernel size 5.
17. Threshold the blurred image using cv2.threshold() function with threshold value 127 and max
value 255.
18. Save the resulting image as 'skeleton.png' using cv2.imwrite() function.
19. Display the original input image and the skeletonized image using cv2.imshow() function.
20. Wait for a key press using cv2.waitKey() function.

31
Python Code:
import numpy as np
import cv2

img = cv2.imread('Thumb.png',0)
ret,img = cv2.threshold(img,127,255,cv2.THRESH_BINARY)

# Function for skeletonizing the image


def findSkeleton(im):
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
out = np.zeros(im.shape,np.uint8)

flag = 0
while(not flag):
eroded = cv2.erode(im, element)
opened = cv2.dilate(eroded, element)
opened = cv2.subtract(im,opened)
out = cv2.bitwise_or(out,opened)
im = eroded.copy()
zeros = img.size - cv2.countNonZero(im)
flag = 1 if (zeros == img.size) else 0
return out

output = findSkeleton(img)

kernel = np.ones((3,3),np.uint8)
output = cv2.dilate(output,kernel)
output = cv2.medianBlur(output, 5)
ret,thresh = cv2.threshold(output,127,255,cv2.THRESH_BINARY_INV)

res = np.hstack((img, thresh))


cv2.imshow("output", cv2.resize(res, dsize=None,fx=0.5, fy=0.5))
cv2.imwrite("task1_output.png", res)
cv2.waitKey(0)

Output:

32
Experiment No: 12

Experiment Name: To create an opencv-python program to smooth a given RGB/grayscale


image by a 2Dfilter.

Theory:

2Dfilter: A 2D filter is a mathematical operation performed on a 2D array of values, such as an image,


to modify or extract certain features. It involves sliding a small window, called a kernel or filter, over
the input array and computing a new value at each location based on the values in the window. The
kernel is typically a small matrix of coefficients that defines the weights of neighboring pixels in the
computation of the new value.
The most common 2D filter is the convolution filter, which multiplies the kernel with the input image
to produce an output image. Other types of 2D filters include the Gaussian filter, the median filter, and
the Sobel filter, among others. These filters can be used for tasks such as smoothing, edge detection,
noise reduction, and feature extraction in images.

Algorithm:

1. Load the input image using cv2.imread() and store it in a variable (e.g., img).
2. Define the kernel using np.ones() and specify the kernel size (e.g., 3x3).
3. Divide the kernel by the sum of its elements to ensure that the output image has the same
brightness as the input image.
4. Apply the filter to the input image using cv2.filter2D() function and store the result in a variable
(e.g., dst).
5. Save the smoothed image using cv2.imwrite() function and specify the filename.
6. Display the original image using cv2.imshow() function.
7. Call the function to smooth the image by the 2D filter.
8. Display the smoothed image using cv2.imshow() function.
9. Wait for a key event using cv2.waitKey(0) function.

33
Python Code:
import cv2
import numpy as np

# Load the image


image = cv2.imread('inp3.jpeg')

# Check if the image is grayscale or color


if len(image.shape) == 2:
# If the image is grayscale, convert it to color
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)

# Define the kernel for the 2D filter


kernel = np.ones((5,5), np.float32)/25

# Apply the filter to the image


smoothed_image = cv2.filter2D(image, -1, kernel)

# Display the original and smoothed images side by side


cv2.imshow('Original Image', image)
cv2.imshow('Smoothed Image', smoothed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Original Image: 2D Filter:

34
Experiment No: 13

Experiment Name: To create an opencv-python program to smooth a given RGB/grayscale


image by an average filter.

Theory:

Average filter: An average filter, also known as a mean filter, is a type of linear filter commonly used
in digital image processing. It operates by replacing the value of each pixel with the average value of
its neighboring pixels. The size of the neighborhood is typically defined by a square or rectangular
window, and the values in the window are all given equal weight. The result is a smoothing effect on
the image, which can help to reduce noise and blur sharp edges. However, it can also result in loss of
detail and blurring of important features, so it must be used judiciously.

Algorithm:

1. Import the necessary libraries - cv2, matplotlib.pyplot, numpy


2. Load an image using cv2.imread() function and store it in a variable.
3. Define a function smooth_image_2D_filter which takes the image as an argument.
4. Define a kernel of size (3,3) with all values as 1/9.
5. Use cv2.filter2D() function to apply the kernel on the image and store the result in a variable.
6. Save the smoothed image using cv2.imwrite() function.
7. Display the original and smoothed images using cv2.imshow() function.
8. Wait for the user to press a key using cv2.waitKey() function.

35
Python Code:
import cv2
import numpy as np

# Load the input image


img = cv2.imread('inp2.jpeg')

# Apply an average filter with a kernel size of 5x5


kernel = np.ones((5, 5), np.float32) / 25
filtered_img = cv2.filter2D(img, -1, kernel)

# Display the result


cv2.imshow('Filtered Image', filtered_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Original Image: Average Filter:

36
Experiment No: 14

Experiment Name: To create an opencv-python program to smooth a given RGB/grayscale


image by a Gaussian filter.

Theory:

Gaussian filter: Gaussian filter is a type of linear filter used in image processing to smooth or blur an
image. It works by convolving the image with a Gaussian function to produce a smoothed image with
reduced noise and details. The Gaussian filter is designed to give more weight to the central pixels and
less weight to the surrounding pixels, creating a smoothing effect that is more natural and less likely to
create artifacts than other filters.

Algorithm:

1. Import the necessary libraries: cv2, matplotlib.pyplot, and numpy.


2. Load the input image using cv2.imread() function and store it in a variable named "img".
3. Define a function named "smooth_image_gaussian_filter" that takes the input image as a
parameter.
4. Inside the function, define a 3x3 Gaussian kernel using np.ones() and np.float32() functions, and
pass it to the cv2.GaussianBlur() function along with the input image and sigma value of 0. This
will apply a Gaussian filter to the image.
5. Store the output in a variable named "dst".
6. Save the smoothed image using the cv2.imwrite() function with the filename
"smoothed_gaussian_filter.png".
7. Display the original image using the cv2.imshow() function.
8. Call the "smooth_image_gaussian_filter" function with the input image as a parameter.
9. Display the smoothed image using the cv2.imshow() function and read the image using the
cv2.imread() function with the filename "smoothed_gaussian_filter.png".
10. Wait for a key event using the cv2.waitKey() function with the parameter 0. This will display
the images until a key is pressed.

37
Python Code:
import cv2

# Load the input image


img = cv2.imread('inp2.jpeg')

# Apply a Gaussian filter with a kernel size of 5 and a sigma value of 1


filtered_img = cv2.GaussianBlur(img, (5, 5), 1)

# Display the result


cv2.imshow('Filtered Image', filtered_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Original Image: Gaussian Filter:

38
Experiment No: 15

Experiment Name: To create an opencv-python program to smooth a given RGB/grayscale image


by a median filter.

Theory:

Median Filter: A median filter is a digital signal processing filter that replaces each pixel's value with
the median value of its neighboring pixels. It is commonly used for image denoising to remove salt-
and-pepper noise, which can occur during image acquisition or transmission. The median filter is
effective in preserving edges and small details in an image while reducing noise.

Algorithm:
1. Import the required libraries: cv2, matplotlib, and numpy
2. Load the input image using the cv2.imread() function
3. Define a function named "smooth_image_median_filter" that takes the input image as an
argument.
4. In the function, define a kernel of size 5x5 using the numpy.ones() function and divide it by 25
to get a normalized kernel.
5. Apply the median filter using the cv2.medianBlur() function with a kernel size of 5x5 on the
input image.
6. Save the smoothed image using the cv2.imwrite() function with a filename
'smoothed_median_filter.png'
7. Display the original and smoothed images using the cv2.imshow() and cv2.imread() functions
respectively.
8. Wait for a key event using the cv2.waitKey() function

39
Python Code:
import cv2

# Load the input image


img = cv2.imread('inp2.jpeg')

# Apply a median filter with a kernel size of 3


filtered_img = cv2.medianBlur(img, 3)

# Display the result


cv2.imshow('Filtered Image', filtered_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Original Image: Median Filter:

40
Experiment No: 16

Experiment Name: To create an opencv-python program to smooth a given RGB/grayscale image


by a bilateral filter.

Theory:

Bilateral filter: A bilateral filter is a non-linear, edge-preserving smoothing filter that is used to reduce
noise and smooth images while preserving edges. It is a type of filter that takes into account both the
spatial and range distances between the pixels in an image. The bilateral filter uses a Gaussian filter in
the spatial domain and a range filter in the intensity domain to smooth the image. The range filter
weights the contribution of each pixel to the filtered output based on how similar its intensity value is
to the central pixel's intensity value. The bilateral filter is effective in smoothing images while
preserving edges, and it is commonly used in image processing and computer vision applications.

Algorithm:

1. Load the image 'abc.jpg' using cv2.imread() function and store it in a variable 'img'.
2. Define a function smooth_image_bilateral_filter(img) that takes an image as an argument.
3. Define a kernel of size 5x5 with all elements as 1 and divided by 25 using np.ones() and
np.float32() functions.
4. Apply the bilateral filter using cv2.bilateralFilter() function with 3 arguments: image, diameter
of each pixel neighborhood, color sigma, and space sigma. Store the result in a variable 'dst'.
5. Save the smoothed image using cv2.imwrite() function with the name
'smoothed_bilateral_filter.png'.
6. Display the original image using cv2.imshow() function with the title 'Original Image' and image
'img'.
7. Call the function smooth_image_bilateral_filter(img).
8. Display the smoothed image using cv2.imshow() function with the title 'Smoothed Image' and
image read from the saved file 'smoothed_bilateral_filter.png'.
9. Wait for the user to press any key using cv2.waitKey(0) function.

41
Python Code:
import cv2

# Load the input image


img = cv2.imread('inp2.jpeg')

# Apply a bilateral filter with a kernel size of 9 and a sigma value of 75


filtered_img = cv2.bilateralFilter(img, 9, 75, 75)

# Display the result


cv2.imshow('Filtered Image', filtered_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Original Image: Bilateral Filter:

42

You might also like