0% found this document useful (0 votes)
24 views18 pages

CV File

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views18 pages

CV File

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Experiment – 1

Write a program for Thresholding.


Thresholding converts a grayscale image into a binary image by setting a
threshold value. Pixels with intensity values above the threshold are set to white
(1), and those below the threshold are set to black (0). This process simplifies
the image, making it easier to analyze and process.
Input:
import cv2
# Load a grayscale image
image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# Apply global thresholding
_, binary_image = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
# Display the result
cv2.imshow('Binary Image', binary_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:

Input Image Output Image


Experiment – 2

Write a program for Erosion.

Erosion is a basic morphological operation in computer vision and image


processing, used to process binary images. It shrinks the white regions
(foreground) in an image, which can be useful for removing small noise or
separating two connected objects.
Input:
import cv2
import numpy as np
# Load a binary image
image = cv2.imread(r"C:\Users\somes\Desktop\binary_image.jpg",
cv2.IMREAD_GRAYSCALE)
# Define a structuring element
kernel = np.ones((5, 5), np.uint8)
# Apply erosion
eroded_image = cv2.erode(image, kernel, iterations=1)
# Display the result
cv2.imshow("Eroded Image", eroded_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
Experiment – 3

Write a program for Dilation.


Input:
import cv2
import numpy as np
# Load a binary image
image = cv2.imread(r"C:\Users\somes\Desktop\binary_image.jpg",
cv2.IMREAD_GRAYSCALE)
# Define a structuring element
kernel = np.ones((5, 5), np.uint8)
# Apply dilation
dilated_image = cv2.dilate(image, kernel, iterations=1)
# Display the result
cv2.imshow('Dilated Image', dilated_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:
Experiment – 4

Write a program for Opening in Image Processing.


Input:
import cv2
import numpy as np
# Load a binary image
image = cv2.imread(r"C:\Users\somes\Desktop\binary_image.jpg",
cv2.IMREAD_GRAYSCALE)
# Define a structuring element
kernel = np.ones((5, 5), np.uint8)
# Apply opening
opened_image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
# Display the result
cv2.imshow("Opened Image", opened_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:
Experiment – 5

Write a program for Closing in Image Processing.


Input:
import cv2
import numpy as np
# Load a binary image
image = cv2.imread(r"C:\Users\somes\Desktop\binary_image.jpg",
cv2.IMREAD_GRAYSCALE)
# Define a structuring element
kernel = np.ones((5, 5), np.uint8)
# Apply closing
closed_image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
# Display the result
cv2.imshow("Closed Image", closed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:
Experiment – 6

Write a program for Color Transform.


Input:
import cv2
# Load an RGB image
image = cv2.imread(r"C:\Users\somes\Desktop\binary_image.jpg")
# Convert RGB to Grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Convert RGB to HSV
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Convert RGB to LAB
lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
# Convert RGB to YCrCb
ycrcb_image = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
# Display the results
cv2.imshow('Grayscale Image', gray_image)
cv2.imshow('HSV Image', hsv_image)
cv2.imshow('LAB Image', lab_image)
cv2.imshow('YCrCb Image', ycrcb_image)
cv2.waitKey(0)

cv2.destroyAllWindows()
Experiment – 7

Write a program on histogram equalization and CLAHE using OpenCV.


Input:
import cv2
# Load a grayscale image
image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# Apply Histogram Equalization
equalized_image = cv2.equalizeHist(image)
# Apply CLAHE
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
clahe_image = clahe.apply(image)
# Display the results
cv2.imshow('Original Image', image)
cv2.imshow('Histogram Equalized Image', equalized_image)
cv2.imshow('CLAHE Image', clahe_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:
Experiment – 8

Write a program using Color adjustment using curves.


Input:
import cv2
import numpy as np
# Load an image
image = cv2.imread('image.jpg')
# Create a lookup table for the curve adjustment
lookup_table = np.array([((i / 255.0) ** 0.5) * 255 for i in np.arange(0,
256)]).astype('uint8')
# Apply the curve adjustment
adjusted_image = cv2.LUT(image, lookup_table)
# Display the result
cv2.imshow('Adjusted Image', adjusted_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Experiment - 9

Write a program on Image Filtering.


Input:
import cv2
import numpy as np
# Load an image
image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# Apply Gaussian Filter
gaussian_blur = cv2.GaussianBlur(image, (5, 5), 0)
# Apply Median Filter
median_blur = cv2.medianBlur(image, 5)
# Apply Sobel Filter
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5)
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5)
# Apply Laplacian Filter
laplacian = cv2.Laplacian(image, cv2.CV_64F)
# Display the results
cv2.imshow('Original Image', image)
cv2.imshow('Gaussian Blur', gaussian_blur)
cv2.imshow('Median Blur', median_blur)
cv2.imshow('Sobel X', sobel_x)
cv2.imshow('Sobel Y', sobel_y)
cv2.imshow('Laplacian', laplacian)
cv2.waitKey(0)
cv2.destroyAllWindows()
Experiment - 10

Write a program on Convolution in Image Processing.


Input:
import cv2
import numpy as np
# Load an image
image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# Define a kernel (e.g., Sobel kernel for edge detection)
kernel = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
# Apply convolution
convolved_image = cv2.filter2D(image, -1, kernel)
# Display the result
cv2.imshow('Convolved Image', convolved_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Experiment – 11

Write a program on Image Gradient.


Input :
import cv2
import numpy as np
# Load a grayscale image
image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# Calculate the gradients using the Sobel operator
grad_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
grad_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
# Calculate the gradient magnitude and direction
magnitude = cv2.magnitude(grad_x, grad_y)
direction = cv2.phase(grad_x, grad_y, angleInDegrees=True)
# Display the results
cv2.imshow('Gradient Magnitude', magnitude)
cv2.imshow('Gradient Direction', direction)
cv2.waitKey(0)
cv2.destroyAllWindows()
Experiment - 12

Write a program on Edge Detection.


Input:
import cv2
import numpy as np
# Load a grayscale image
image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# Apply Sobel Filter
sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
# Calculate the gradient magnitude
gradient_magnitude = cv2.magnitude(sobel_x, sobel_y)
# Display the result
cv2.imshow('Edge Detection', gradient_magnitude)
cv2.waitKey(0)
cv2.destroyAllWindows()
Experiment - 13

Write a program on Feature Detection.


Input:
import cv2
import numpy as np
# Load a grayscale image
image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# Apply Harris corner detector
corners = cv2.cornerHarris(image, 2, 3, 0.04)
# Highlight the corners in the original image
image[corners > 0.01 * corners.max()] = [0, 0, 255]
# Display the result
cv2.imshow('Corners', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

You might also like