computer vision
Practical 1 : - Basic Operation on Image
1A) Program to change the Brightness of Image.
Code : -
import cv2
import [Link] as plt
image = [Link]('[Link]')
image_rgb = [Link](image, cv2.COLOR_BGR2RGB)
# Adjust brightness by increasing beta by 50
adjusted = [Link](image, beta=50)
adjusted_rgb = [Link](adjusted, cv2.COLOR_BGR2RGB)
[Link](1, 2, 1)
[Link]('Original')
[Link](image_rgb)
[Link]('off')
[Link](1, 2, 2)
[Link]('Brightness Adjusted')
[Link](adjusted_rgb)
[Link]('off')
plt.tight_layout()
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
1B) : - To flip the image around the vertical and horizontal line.
Code : -
import cv2
import [Link] as plt
img = [Link](r"C:\Users\Suyash\Desktop\vs studio code\[Link]")
if img is None:
print("Error: Image not found or path is incorrect.")
else:
img_org = [Link](img, cv2.COLOR_BGR2RGB)
[Link](1, 3, 1)
[Link]('Original')
[Link](img_org)
[Link]('off')
v = [Link](img_org, 0)
[Link](1, 3, 2)
[Link]('Vertical Flip')
[Link](v)
[Link]('off')
h = [Link](img_org, 1)
[Link](1, 3, 3)
[Link]('Horizontal Flip')
[Link](h)
[Link]('off')
plt.tight_layout()
[Link]()
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
1.C) : - Display color components of image.
Code : -
import cv2
import [Link] as plt
image = [Link]("[Link]")
# Check if the image was loaded successfully
if image is None:
print("Error: Could not load image. Check the file path.")
exit()
# Convert image from BGR (OpenCV default) to RGB (for matplotlib display)
image_rgb = [Link](image, cv2.COLOR_BGR2RGB)
# Create Blue channel image (set Green and Red to 0)
b = [Link]()
b[:, :, 1] = 0 # Zero Green
b[:, :, 2] = 0 # Zero Red
blue_rgb = [Link](b, cv2.COLOR_BGR2RGB)
# Create Green channel image (set Blue and Red to 0)
g = [Link]()
g[:, :, 0] = 0 # Zero Blue
g[:, :, 2] = 0 # Zero Red
green_rgb = [Link](g, cv2.COLOR_BGR2RGB)
# Create Red channel image (set Blue and Green to 0)
r = [Link]()
r[:, :, 0] = 0 # Zero Blue
r[:, :, 1] = 0 # Zero Green
red_rgb = [Link](r, cv2.COLOR_BGR2RGB)
# Plot the original and color channel images
[Link](figsize=(12, 4))
[Link](1, 4, 1)
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
[Link]('Original Image')
[Link](image_rgb)
[Link]('off')
[Link](1, 4, 2)
[Link]('Blue Channel')
[Link](blue_rgb)
[Link]('off')
[Link](1, 4, 3)
[Link]('Green Channel')
[Link](green_rgb)
[Link]('off')
[Link](1, 4, 4)
[Link]('Red Channel')
[Link](red_rgb)
[Link]('off')
plt.tight_layout()
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
1.D) : - Display of gray scale images
Code : -
import [Link] as plt
import cv2
image = [Link]("[Link]")
image_rgb = [Link](image, cv2.COLOR_BGR2RGB)
[Link](1,2,1)
[Link](image_rgb)
[Link]("Original Image")
[Link](1,2,2)
gray_image = [Link](image_rgb, cv2.COLOR_RGB2GRAY) # Corrected here
[Link](gray_image, cmap='gray')
[Link]("Gray Scaled")
[Link]()
output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
1.E) : - To find the negative of an image
Code : -
import cv2
import [Link] as plt
image=[Link]("[Link]")
[Link](1,2,1)
[Link]([Link](image, cv2.COLOR_BGR2RGB))
[Link]("original image")
[Link](1,2,2)
negative_image=155 - image
[Link]([Link](negative_image, cv2.COLOR_BGR2RGB))
[Link]('Negative Image')
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Practical 2 : - Using histogram for image quality analysis
2.A) : - Calculate the histogram of a given imae
from matplotlib import pyplot as plt
img = [Link]("[Link]",0)
histr = [Link]([img],[0],None,[256],[0,256])
[Link](histr)
[Link]()
output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
2.B) : - Histogram Equalization
Code : -
import cv2
from matplotlib import pyplot as plt
img = [Link]('[Link]',0)
[Link](1,2,1)
histr = [Link]([img],[0],None,[256],[0,256])
[Link](histr)
[Link](1,2,2)
equ = [Link](img)
[Link](equ)
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Practical 3 : - Program for Image Filtering
3.A) : - Low pass filter
1) Average filter
Code : -
import cv2
import [Link] as plt
import numpy as np
img=[Link]("[Link]")
imagee=[Link](img,cv2.COLOR_RGB2BGR)
[Link](1,2,1)
[Link]("Original")
[Link](imagee)
kernel = [Link]((7,7),np.float32)/25
img2=cv2.filter2D(imagee,-1,kernel)
[Link](1,2,2)
[Link]("Average Filter")
[Link](img2)
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
2) Weighted average filter
Code : -
import cv2
import [Link] as plt
import numpy as np
img=[Link]("[Link]")
imagee=[Link](img, cv2.COLOR_RGB2BGR)
[Link](1,2,1)
[Link]("original")
[Link](imagee)
img2= [Link](imagee,(5,7),100)
[Link](1,2,2)
[Link]("Weighted Average Filter")
[Link](img2)
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
3) Median Filter
Code : -
import cv2
import [Link] as plt
import numpy as np
img=[Link]("[Link]")
imagee=[Link](img, cv2.COLOR_RGB2BGR)
[Link](1,2,1)
[Link]("original")
[Link](imagee)
img2= [Link](imagee,7)
[Link](1,2,2)
[Link]("median Filter")
[Link](img2)
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
3.A) High Pass Filter
Code : -
import cv2
import [Link] as plt
import numpy as np
image = [Link]('[Link]')
gray_image = [Link](image, cv2.COLOR_BGR2GRAY)
sobelx = [Link](gray_image, cv2.CV_64F, 1, 0, ksize=5)
sobely = [Link](gray_image, cv2.CV_64F, 0, 1, ksize=5)
sobel_combined = [Link]([Link](sobelx), 0.5,
[Link](sobely), 0.5, 0)
[Link](2, 2, 1)
[Link]([Link](image, cv2.COLOR_BGR2RGB))
[Link]('Original Image')
[Link]('off')
[Link](2, 2, 2)
[Link](gray_image, cmap='gray')
[Link]('Grayscale Image')
[Link]('off')
[Link](2, 2, 3)
[Link](sobelx, cmap='gray')
[Link]('Sobel X')
[Link]('off')
[Link](2, 2, 4)
[Link](sobely, cmap='gray')
[Link]('Sobel Y')
[Link]('off')
[Link]()
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
2) Laplacian operator
Code : -
import cv2
import numpy as np
import [Link] as plt
image = [Link]('[Link]')
gray_image = [Link](image, cv2.COLOR_BGR2GRAY)
laplacian = [Link](gray_image, cv2.CV_64F)
laplacian_abs = np.uint8([Link](laplacian))
[Link](1, 2, 1)
[Link]([Link](image, cv2.COLOR_BGR2RGB))
[Link]('Original Image')
[Link]('off')
[Link](1, 2, 2)
[Link](laplacian_abs, cmap='gray')
[Link]('Laplacian Edge Detection')
[Link]('off')
plt.tight_layout()
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Practical 3B) Design non-linear filtering
Code : -
import cv2
import [Link] as plt
image = [Link]('[Link]')
image_rgb = [Link](image, cv2.COLOR_BGR2RGB)
filtered_image = [Link](image_rgb, d=9, sigmaColor=75, sigmaSpace=75)
[Link](1,2,1)
[Link](image_rgb)
[Link]('Original Image')
[Link](1,2,2)
[Link](filtered_image)
[Link]('Bilateral Filtered Image')
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Practical 4) : -Edge detection with gradient and convolution of ana Image.
Code : -
import cv2
import numpy as np
import [Link] as plt
image = [Link]('[Link]')
gray_image = [Link](image, cv2.COLOR_BGR2GRAY)
sobel_x = [Link](gray_image, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = [Link](gray_image, cv2.CV_64F, 0, 1, ksize=3)
magnitude = [Link](sobel_x, sobel_y)
threshold_value = 100
_, gradient_edges = [Link](magnitude, threshold_value, 255,
cv2.THRESH_BINARY)
gradient_edges = np.uint8(gradient_edges)
lower_threshold = 100
upper_threshold = 200
canny_edges = [Link](gray_image, lower_threshold, upper_threshold)
[Link](figsize=(15, 6))
[Link](1, 3, 1)
[Link]('Original Image')
[Link]([Link](image, cv2.COLOR_BGR2RGB))
[Link]('off')
[Link](1, 3, 2)
[Link]('Gradient-Based Edges')
[Link](gradient_edges, cmap='gray')
[Link]('off')
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
[Link](1, 3, 3)
[Link]('Canny Edges')
[Link](canny_edges, cmap='gray')
[Link]('off')
plt.tight_layout()
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Practical 5) : - Finding Threshold of Image
5.A) Program to find threshold of gray scale image
Code : -
import cv2
import numpy as np
import [Link] as plt
image = [Link]('[Link]')
gray_image = [Link](image, cv2.COLOR_BGR2GRAY)
threshold_value = 127
max_value = 255
ret, thresh_image = [Link](gray_image, threshold_value, max_value,
cv2.THRESH_BINARY)
[Link](1, 2, 1)
[Link]('Original Image')
[Link](gray_image, cmap='gray')
[Link](1, 2, 2)
[Link]('Thresholded Image')
[Link](thresh_image, cmap='gray')
plt.tight_layout()
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
5.B.) Program to find threshold of RGB image.
Code : -
import cv2
import numpy as np
import [Link] as plt
image_bgr = [Link]('[Link]')
image_rgb = [Link](image_bgr, cv2.COLOR_BGR2RGB)
threshold_value = 127
max_value = 255
r, g, b = [Link](image_rgb)
_, thresh_r = [Link](r, threshold_value, max_value, cv2.THRESH_BINARY)
_, thresh_g = [Link](g, threshold_value, max_value, cv2.THRESH_BINARY)
_, thresh_b = [Link](b, threshold_value, max_value, cv2.THRESH_BINARY)
thresh_rgb = [Link]([thresh_r, thresh_g, thresh_b])
[Link](1, 2, 1)
[Link]('Original Image')
[Link](image_rgb)
[Link](1, 2, 2)
[Link]('Thresholded RGB Image')
[Link](thresh_rgb)
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Practical 6 : - Program to estimate and subtract the background of an
image.
Code : -
import cv2
import [Link] as plt
image = [Link]('[Link]')
gray = [Link](image, cv2.COLOR_BGR2GRAY)
_,mask = [Link](gray, 127, 255, cv2.THRESH_BINARY)
background = cv2.bitwise_and(image, image, mask=cv2.bitwise_not(mask))
foreground = cv2.bitwise_and(image, image, mask=mask)
[Link](1,3,1)
[Link]([Link](image, cv2.COLOR_BGR2RGB))
[Link]('Original')
[Link](1,3,2)
[Link]([Link](background, cv2.COLOR_BGR2RGB))
[Link]('Background')
[Link](1,3,3)
[Link]([Link](foreground, cv2.COLOR_BGR2RGB))
[Link]('Foreground')
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Practical 7 : - Program to convert color image to gray and hsv.
Code : -
import cv2
import numpy as np
import [Link] as plt
image_path = '[Link]'
image = [Link](image_path)
gray_image = [Link](image, cv2.COLOR_BGR2GRAY)
hsv_image = [Link](image, cv2.COLOR_BGR2HSV)
[Link](1,3,1)
[Link]('Original Color Image')
[Link]([Link](image, cv2.COLOR_BGR2RGB))
[Link](1,3,2)
[Link]('Grayscale Image')
[Link](gray_image, cmap='gray')
[Link](1,3,3)
[Link]('HSV Image')
[Link](hsv_image[:,:,0],cmap='hsv')
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Practical 8A:- Determination of edge detection using operators.
Code : -
import cv2
import numpy as np
import [Link] as plt
image = [Link]('[Link]')
gray_image = [Link](image, cv2.COLOR_BGR2GRAY)
kernel_size = 5
sigma = 1.0
blurred_image = [Link](gray_image, (kernel_size, kernel_size), sigma)
log_edges = [Link](blurred_image, cv2.CV_64F)
log_edges = [Link](log_edges)
sigma1 = 1.0
sigma2 = 2.0
blurred_image1 = [Link](gray_image, (0, 0), sigma1)
blurred_image2 = [Link](gray_image, (0, 0), sigma2)
dog_edges = blurred_image1 - blurred_image2
dog_edges = [Link](dog_edges)
[Link](figsize=(18, 6))
[Link](1, 3, 1)
[Link]('Original Grayscale Image')
[Link](gray_image, cmap='gray')
[Link]('off')
[Link](1, 3, 2)
[Link]('LoG Edge Detection')
[Link](log_edges, cmap='gray')
[Link]('off')
[Link](1, 3, 3)
[Link]('DoG Edge Detection')
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
[Link](dog_edges, cmap='gray')
[Link]('off')
plt.tight_layout()
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Practical 8.B) :- 2-D DFT and DCT
Code : -
import cv2
import numpy as np
import [Link] as plt
image = [Link]('[Link]', cv2.IMREAD_GRAYSCALE)
image_float = np.float32(image)
dft = [Link](image_float, flags=cv2.DFT_COMPLEX_OUTPUT)
dft_shift = [Link](dft)
magnitude_spectrum = 20 * [Link]([Link](dft_shift[:,:,0], dft_shift[:,:,1]) + 1)
[Link](magnitude_spectrum, cmap='gray')
[Link]('Magnitude Spectrum')
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
8.B.) DCT
Code : -
import cv2
import numpy as np
import [Link] as plt
image = [Link]('[Link]', cv2.IMREAD_GRAYSCALE)
image = np.float32(image)
dct_result = [Link](image)
[Link](figsize=(12, 6))
[Link](1, 2, 1)
[Link]('Original Image')
[Link](image, cmap='gray')
[Link]('off')
[Link](1, 2, 2)
[Link]('DCT Result')
[Link]([Link]([Link](dct_result) + 1), cmap='gray')
[Link]('off')
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Practical 8.C) : - Filtering in Frequency domain.
Code : -
import cv2
import numpy as np
import [Link] as plt
f = [Link]('[Link]', 0)
[Link](figsize=(15, 6))
[Link](1, 5, 1)
[Link](f, cmap='gray')
[Link]('Original')
F = [Link].fft2(f)
Fshift = [Link](F)
[Link](1, 5, 2)
[Link](np.log1p([Link](Fshift)), cmap='gray')
[Link]('FFT Spectrum')
M, N = [Link]
H = [Link]((M, N), dtype=np.float32)
D0 = 10
for u in range(M):
for v in range(N):
D = [Link]((u - M/2)**2 + (v - N/2)**2)
H[u, v] = [Link](-(D**2) / (2 * (D0**2)))
Gshift = Fshift * H
G = [Link](Gshift)
g = [Link]([Link].ifft2(G))
[Link](1, 5, 3)
[Link](g, cmap='gray')
[Link]('Low-Pass Filtered')
HPF = 1 - H
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Gshift = Fshift * HPF
G = [Link](Gshift)
g = [Link]([Link].ifft2(G))
[Link](1, 5, 4)
[Link](g, cmap='gray')
[Link]('High-Pass Filtered')
[Link](1, 5, 5)
[Link](np.log1p([Link](Gshift)), cmap='gray')
[Link]('HPF Spectrum')
plt.tight_layout()
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Practical 9.A.) : - Display of colour images
Code : -
import cv2
import [Link] as plt
img = [Link]('[Link]')
if img is None: exit()
[Link](1, 2, 1)
[Link]("BGR")
[Link](img)
[Link]('off')
img_rgb = [Link](img, cv2.COLOR_BGR2RGB)
[Link](1, 2, 2)
[Link]("RGB")
[Link](img_rgb)
[Link]('off')
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Practical 9.B.) : - Conversion between colour spaces.
Code : -
import cv2
import numpy as np
import [Link] as plt
image_path = '[Link]'
image_bgr = [Link](image_path)
image_rgb = [Link](image_bgr, cv2.COLOR_BGR2RGB)
image_hsv = [Link](image_bgr, cv2.COLOR_BGR2HSV)
image_YCRB = [Link](image_bgr, cv2.COLOR_BGR2YCrCb)
[Link](1,3,1)
[Link]('Original BGR')
[Link]([Link](image_bgr, cv2.COLOR_BGR2RGB))
[Link]('off')
[Link](1,3,2)
[Link]('HSV')
[Link](image_hsv)
[Link]('off')
[Link](1,3,3)
[Link]('YCRB')
[Link](image_YCRB)
[Link]('off')
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Practical 10.A) :- DWT of images
Code : -
import cv2
import pywt
import numpy as np
import [Link] as plt
image = [Link]('[Link]', cv2.IMREAD_GRAYSCALE)
if image is None:
print("Image not found. Check the path or file name.")
exit()
image = np.float32(image)
coeffs2 = pywt.dwt2(image, 'haar')
cA, (cH, cV, cD) = coeffs2
[Link](figsize=(6, 6))
[Link](2, 2, 1)
[Link]('Approximation')
[Link](cA, cmap='gray')
[Link]('off')
[Link](2, 2, 2)
[Link]('Horizontal')
[Link](cH, cmap='gray')
[Link]('off')
[Link](2, 2, 3)
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
[Link]('Vertical')
[Link](cV, cmap='gray')
[Link]('off')
[Link](2, 2, 4)
[Link]('Diagonal')
[Link](cD, cmap='gray')
[Link]('off')
plt.tight_layout()
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
Practical 10.B.) : - Segmentation using watershed transfrom
Code : -
import cv2
import numpy as np
import [Link] as plt
from skimage import morphology
image = [Link]('[Link]')
gray = [Link](image, cv2.COLOR_BGR2GRAY)
_, binary = [Link](gray, 127, 255, cv2.THRESH_BINARY_INV)
dist_transform = [Link](binary, cv2.DIST_L2, 5)
local_maxi = morphology.local_maxima(dist_transform)
markers = np.zeros_like(dist_transform, dtype=np.int32)
markers[local_maxi] = [Link](1, [Link](local_maxi) + 1)
markers[binary == 0] = 0
markers = [Link](image, markers)
image[markers == -1] = [0, 0, 255]
[Link](figsize=(12, 4))
[Link](1, 3, 1)
[Link]([Link](image, cv2.COLOR_BGR2RGB))
[Link]('Watershed Result')
[Link]('off')
[Link](1, 3, 2)
[Link](dist_transform, cmap='jet')
[Link]('Distance Transform')
[Link]('off')
[Link](1, 3, 3)
[Link](local_maxi, cmap='gray')
[Link]('Local Maxima')
[Link]('off')
plt.tight_layout()
Sheth. N. K. T. T. College T. Y. Bsc. Data Science
Computer Vision
[Link]()
Output : -
Sheth. N. K. T. T. College T. Y. Bsc. Data Science