Professional Documents
Culture Documents
GWALIOR
Deemed to be University
(Declared under Distinct Category by Ministry of Education, Government of India)
(NAAC Accredited with A++ Grade)
Lab Report
On
Submitted by:
Yash Gupta
(0901CS211135)
Faculty Mentor:
Prof. Khushboo Agarwal
Assistant Professor
Department of Computer Science and Engineering
Submitted to:
Code to implement
13. watermarking in
spatial domain.
THEORY:
OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning
software library. It provides various functions to manipulate images and videos. In this experiment, we aim
to implement a simple program using Python and OpenCV to read an image file and display it on the
screen.
IMPLEMENTATION:
For every experiment import the following python libraries in your IDE:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as img
Image = img.imread('/content/rabbu.png')Image.shape
Image
plt.imshow(Image)
3|Page
OBSERVATION:
Conclusion:
In this experiment, we successfully implemented a Python program using the OpenCV library to read an
image file and display it on the screen. This demonstrates the basic functionality of image processing using
OpenCV in Python.
4|Page
Experiment No.2
THEORY:
In image processing, the negative of an image is obtained by subtracting each pixel value from the
maximum intensity value. For example, in an 8-bit grayscale image with pixel values ranging from 0 to
255, the negative of a pixel value p can be calculated as 255 - p. In this experiment, we aim to implement a
Python program using the OpenCV library to determine the negative of an input image.
IMPLEMENTATION:
Image = img.imread('/content/rabbu.png')Image.shape
Image
image_negative=255-image_array
plt.figure(figsize=(14, 8))
plt.subplot(1, 2, 1)
plt.imshow(image_array)
5|Page
plt.title("Original Image")
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(image_negative)
plt.title("Negative Image")
plt.axis('off')
OBSERVATION:
Conclusion:
In this experiment, we successfully implemented a Python program using the OpenCV library to determine
the negative of an input grayscale image. This demonstrates the basic functionality of image processing
using OpenCV in Python..
6|Page
Experiment No.3
Objective : Read an image and perform different filtering operations (Average, Median etc.)
THEORY:
Filtering operations are commonly used in image processing to enhance or modify images. Two widely
used filtering operations are the average filter and the median filter. The average filter replaces each pixel
value with the average value of its neighboring pixels, while the median filter replaces each pixel value
with the median value of its neighboring pixels. In this experiment, we aim to implement Python programs
using the OpenCV library to perform both average and median filtering operations on an input image.
IMPLEMENTATION:
plt.subplot(2, 2, 1)
plt.imshow(image_array)
plt.title("Original Image")
plt.axis('off')
plt.subplot(2, 2, 2)
plt.imshow(gaussian_blur)
plt.title("Gaussian Blur")
plt.axis('off')
plt.subplot(2, 2, 3)
plt.imshow(mean_filter)
plt.title("Average Filter")
plt.axis('off')
plt.subplot(2, 2, 4)
plt.imshow(median_filter)
plt.title("Median Filter")
plt.axis('off')
7|Page
OBSERVATION:
Conclusion:
In this experiment, we successfully implemented Python programs using the OpenCV library to perform
average and median filtering operations on an input image. This demonstrates the basic functionality of
image processing using OpenCV in Python..
8|Page
Experiment No.4
THEORY:
Motion blur is a common effect used in image processing to simulate the blurring caused by the relative
motion between the camera and the scene being captured. It is often used to convey a sense of movement
or speed in images. In this experiment, we aim to implement a Python program using the OpenCV library
to create a motion blur effect on an input image.
IMPLEMENTATION:
length=40
angle=60
angle = np.deg2rad(angle)
kernel = np.zeros(length)
center = length
kernel[:] = 1 / length
blurred_image_array = np.zeros_like(image_array)
for i in range(image_array.shape[2]):
plt.figure(figsize=(20, 10))
plt.subplot(1, 2, 1)
plt.imshow(image_array)
plt.title("Original Image")
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(blurred_image_array)
plt.title(f"Motion Blur (Length = {length}, Angle = {angle}°)")
plt.axis('off')
9|Page
OBSERVATION:
Conclusion:
In this experiment, we successfully implemented a Python program using the OpenCV library to create a
motion blur effect on an input image. This demonstrates the basic functionality of image processing using
OpenCV in Python.
10 | P a g e
Experiment No.5
THEORY:
Gray level slicing without background is a technique used in image processing to highlight specific
intensity levels in an image while removing the background. This technique is particularly useful for
enhancing the visibility of objects or features in an image. In this experiment, we aim to implement a
Python program using the OpenCV library to perform gray level slicing without background on an input
grayscale image..
IMPLEMENTATION:
image = Image.open('/content/rabbu.png').convert('L')min_level=125
max_level=195
image_array = np.array(image)
output_array =
np.zeros_like(image_array)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(image_array, cmap='gray')
plt.title("Original Image (Grayscale)")
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(output_array, cmap='gray')
plt.title(f"Gray Level Slicing ({min_level} to {max_level})")
plt.axis('off')
11 | P a g e
OBSERVATION:
Conclusion:
In this experiment, we successfully implemented Python programs using the OpenCV library to perform
average and median filtering operations on an input image. This demonstrates the basic functionality of
image processing using OpenCV in Python..
12 | P a g e
Experiment No.6
THEORY:
Brightness enhancement and suppression are common techniques used in image processing to adjust the
overall brightness of an image. Brightness enhancement increases the brightness of an image, making it
appear lighter, while brightness suppression decreases the brightness, making it appear darker. In this
experiment, we aim to implement Python programs using the OpenCV library to perform both brightness
enhancement and suppression on an input image.
IMPLEMENTATION:
enhancement_value=60
suppression_value=-60
plt.figure(figsize=(15, 5))
plt.subplot(1, 3, 1)
plt.imshow(image_array)
plt.title("Original Image")
plt.axis('off')
plt.subplot(1, 3, 2)
plt.imshow(enhanced_image_array)
plt.title(f"Brightness Enhancement (+{enhancement_value})")
plt.axis('off')
plt.subplot(1, 3, 3)
plt.imshow(suppressed_image_array)
plt.title(f"Brightness Suppression ({suppression_value})")
plt.axis('off')
13 | P a g e
OBSERVATION:
Conclusion:
In this experiment, we successfully implemented Python programs using the OpenCV library to perform
brightness enhancement and suppression on an input image. These techniques allow us to adjust the overall
brightness of the image to achieve desired visual effects.
14 | P a g e
Experiment No.7
Objective :
To create a vision program to find histogram value and display histograph of a grayscale and color image..
THEORY:
A histogram is a graphical representation of the distribution of pixel intensities in an image. It provides
valuable insights into the contrast, brightness, and dynamic range of an image. In this experiment, we aim
to implement Python programs using the OpenCV library to calculate and display histograms for both
grayscale and color images.
IMPLEMENTATION:
image = Image.open('/content/rabbu.png')grayscale_image =
image.convert('L')
grayscale_array = np.array(grayscale_image)
grayscale_hist, grayscale_bins = np.histogram(grayscale_array, bins=256, range=(0, 256))
color_array = np.array(image)
plt.figure(figsize=(12, 8))
plt.subplot(2, 2, 1)
plt.imshow(grayscale_array, cmap='gray')
plt.title("Grayscale Image")
plt.axis('off')
plt.subplot(2, 2, 2)
plt.bar(grayscale_bins[:-1], grayscale_hist, width=1, color='gray')
plt.title("Grayscale Histogram")
plt.xlim(0, 255)
plt.xlabel("Intensity Value")
plt.ylabel("Frequency")
plt.subplot(2, 2, 3)
plt.imshow(color_array)
plt.title("Color Image")
plt.axis('off')
plt.subplot(2, 2, 4)
15 | P a g e
plt.bar(color_bins_r[:-1], color_hist_r, width=1, color='red', alpha=0.7, label='Red
Channel')
plt.bar(color_bins_g[:-1], color_hist_g, width=1, color='green', alpha=0.7, label='Green
Channel')
plt.bar(color_bins_b[:-1], color_hist_b, width=1, color='blue', alpha=0.7, label='Blue
Channel')
plt.title("Color Histograms")
plt.xlim(0, 255)
plt.xlabel("Intensity Value")
plt.ylabel("Frequency")
plt.legend(loc='upper right')
plt.tight_layout()
OBSERVATION:
Conclusion:
In this experiment, we successfully implemented Python programs using the OpenCV library to calculate
and display histograms for both grayscale and color images. Histograms provide valuable insights into the
distribution of pixel intensities, which can aid in image analysis and enhancement.
16 | P a g e
Experiment No.8
IMPLEMENTATION:
grayscale_array = np.array(grayscale_image)
plt.subplot(1, 3, 1)
plt.imshow(image)
plt.title("Original RGB Image")
plt.axis('off')
plt.subplot(1, 3, 2)
plt.imshow(grayscale_array, cmap='gray')
plt.title("Grayscale Image")
plt.axis('off')
plt.subplot(1, 3, 3)
plt.imshow(segmented_array, cmap='gray')
plt.title(f"Segmented Image (Threshold = {threshold})")
plt.axis('off')
17 | P a g e
OBSERVATION:
Conclusion:
In this experiment, we successfully implemented a Python program using the OpenCV library to segment an
RGB image using the thresholding method. Thresholding provides a simple yet effective way to partition an
image into regions based on intensity values, which can be useful for various image analysis tasks.
18 | P a g e
Experiment No.9
Objective : Read a colour image and separate the colour image into red green and blue planes.
THEORY:
A color image consists of three color channels: red, green, and blue. Each channel represents the intensity
of its respective color in the image. In this experiment, we aim to implement a Python program using the
OpenCV library to read a color image and separate it into its red, green, and blue channels.
IMPLEMENTATION:
red_channel = image_array[:, :, 0]
green_channel = image_array[:, :, 1]
blue_channel = image_array[:, :, 2]
plt.figure(figsize=(12, 8))
plt.subplot(2, 2, 1)
plt.imshow(image)
plt.title("Original RGB Image")
plt.axis('off')
plt.subplot(2, 2, 2)
plt.imshow(red_channel, cmap='Reds')
plt.title("Red Channel")
plt.axis('off')
plt.subplot(2, 2, 3)
plt.imshow(green_channel, cmap='Greens')
plt.title("Green Channel")
plt.axis('off')
plt.subplot(2, 2, 4)
plt.imshow(blue_channel, cmap='Blues')
plt.title("Blue Channel")
plt.axis('off')
19 | P a g e
OBSERVATION:
Conclusion:
In this experiment, we successfully implemented a Python program using the OpenCV library to separate a
color image into its red, green, and blue channels. This technique allows us to analyze and manipulate
individual color components of the image separately.
20 | P a g e
Experiment No.10
THEORY:
Gamma correction is a nonlinear operation used to adjust the brightness of an image. It involves applying a
power-law function to the pixel values, which can effectively modify the image's contrast and brightness.
In this experiment, we aim to implement a Python program using the OpenCV library to perform gamma
correction for a given color image.
IMPLEMENTATION:
image = Image.open('/content/rabbu.png')gamma=2.9
image_array = np.array(image) / 255.0
corrected_image = Image.fromarray(corrected_array)
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(image)
plt.title("Original RGB Image")
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(corrected_image)
plt.title(f"Gamma Corrected Image (Gamma = {gamma})")
plt.axis('off')
21 | P a g e
OBSERVATION:
Conclusion:
In this experiment, we successfully implemented a Python program using the OpenCV library to perform
gamma correction for a given color image. Gamma correction can be used to adjust the image's brightness
and contrast according to specific requirements. Adjusting the gamma parameter allows for fine-tuning of
the correction effect.
22 | P a g e
Experiment No.11
THEORY:
Image conversion involves transforming an image from one color space or data format to another. This
process is commonly used in image processing and computer vision applications to preprocess images
before further analysis or processing. In this experiment, we aim to implement a Python program using the
OpenCV library to perform different image conversion techniques, including color space conversions and
data format conversions.
IMPLEMENTATION:
image = Image.open('/content/rabbu.png')grayscale_image =
image.convert('L')
hsv_image = image.convert('HSV')
lab_image = image.convert('LAB')
rotated_image = image.rotate(45)
flipped_vertical_image = image.transpose(Image.FLIP_TOP_BOTTOM)
flipped_horizontal_image = image.transpose(Image.FLIP_LEFT_RIGHT)
plt.figure(figsize=(12, 12))
plt.subplot(3, 3, 1)
plt.imshow(image)
plt.title("Original RGB Image")
plt.axis('off')
plt.subplot(3, 3, 2)
plt.imshow(grayscale_image, cmap='gray')
plt.title("Grayscale Image")
plt.axis('off')
plt.subplot(3, 3, 3)
plt.imshow(hsv_image)
23 | P a g e
plt.title("HSV Image")
plt.axis('off')
plt.subplot(3, 3, 4)
plt.imshow(lab_image)
plt.title("LAB Image")
plt.axis('off')
plt.subplot(3, 3, 5)
plt.imshow(resized_image)
plt.title("Resized Image")
plt.axis('off')
plt.subplot(3, 3, 6)
plt.imshow(rotated_image)
plt.title("Rotated Image (45 degrees)")
plt.axis('off')
plt.subplot(3, 3, 7)
plt.imshow(flipped_horizontal_image)
plt.title("Flipped Horizontally")
plt.axis('off')
plt.subplot(3, 3, 8)
plt.imshow(flipped_vertical_image)
plt.title("Flipped Vertically")
plt.axis('off')
24 | P a g e
OBSERVATION:
Conclusion:
In this experiment, we successfully implemented a Python program using the OpenCV library to perform
different image conversion techniques. These techniques allow us to transform images into different color
spaces or data formats, which can be useful for various image processing tasks and analyses.
25 | P a g e
Experiment No.12
Objective : To create a color image and perform read and write operation.
THEORY:
Creating, reading, and writing color images are fundamental operations in image processing. A color image
is typically represented as a three-dimensional array, where each element represents the intensity of one of
the three color channels (red, green, and blue) at a particular pixel. In this experiment, we will create a
color image programmatically, read it from the disk, and then write it back to the disk using the OpenCV
library.
IMPLEMENTATION:
draw = ImageDraw.Draw(image)
fill="red")
image.save("color_image.png")
print("Image saved as
'color_image.png'")
plt.imshow(image)
plt.title("Created Color Image")
plt.axis('off')
plt.show()
read_image = Image.open("color_image.png")
plt.imshow(read_image)
plt.title("Read Color Image")
plt.axis('off')
26 | P a g e
OBSERVATION:
Conclusion:
In this experiment, we successfully created a color image programmatically, wrote it to the disk, and then
read it back from the disk using the OpenCV library. These operations are fundamental in image processing
and form the basis for various image manipulation tasks.
27 | P a g e
Experiment No.13
THEORY:
Watermarking is a technique used to embed a watermark, which can be a logo, text, or any other
identifying information, into an image for copyright protection or authentication purposes. Spatial domain
watermarking directly modifies the pixel values of the original image to embed the watermark. Alpha
blending is a common method used for spatial domain watermarking, where the watermark is combined
with the original image by adjusting the intensity of pixel values.
IMPLEMENTATION:
draw = ImageDraw.Draw(watermark_image)
font_size = 90
font = ImageFont.truetype("/content/Roboto-BoldItalic.ttf", font_size)
watermarked_image = watermarked_image.convert('RGB')
plt.imshow(watermarked_image)
plt.title("Watermarked Image")
plt.axis('off')
plt.show()
28 | P a g e
OBSERVATION:
Conclusion:
In this experiment, we successfully implemented spatial domain watermarking using alpha blending
technique in Python with the OpenCV library. Spatial domain watermarking directly modifies the pixel
values of the original image to embed the watermark, which can be useful for copyright protection or
authentication purposes. Adjusting the alpha parameter in the add_watermark function allows controlling
the transparency of the watermark.
29 | P a g e
Experiment No.14
IMPLEMENTATION:
# Create a list to store the images at each level of the Gaussian pyramid
gaussian_pyramid = []
30 | P a g e
plt.figure(figsize=(12, 12))
plt.subplot(1, levels, level + 1)
plt.imshow(img_rgb)
plt.title(f'Level {level}')
plt.axis('off')
OBSERVATION:
Conclusion:
In this experiment, we successfully implemented the generation of a Gaussian pyramid with different levels
in Python using the OpenCV library. Gaussian pyramids are useful for various image processing tasks, such
as image blending, image compression, and multi-scale analysis. Adjusting the number of levels allows
controlling the scale and resolution of the pyramid.
31 | P a g e
32 | P a g e