0% found this document useful (0 votes)
25 views7 pages

Digital Image Processing Techniques

Assignment

Uploaded by

utkarshp177
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)
25 views7 pages

Digital Image Processing Techniques

Assignment

Uploaded by

utkarshp177
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

DIGITAL IMAGE PROCESSING

ASSIGNMENT

Name -Utkarsh Pandey


Ad. No. – 22SCSE1010238
Section - 6
DIGITAL IMAGE PROCESSING ASSIGNMENT
SECTION-6

1. Write a program that converts a color image to grayscale using OpenCV.


import cv2
def convert_to_grayscale(image_path,
output_path):
color_image = cv2.imread(image_path)
if color_image is
None:
print("Error: Unable to load image. Please check the file path.")
return
grayscale_image = cv2.cvtColor(color_image,
cv2.COLOR_BGR2GRAY)
cv2.imwrite(output_path,
grayscale_image)
print(f"Grayscale image saved at
{output_path}")
if __name__ ==
"__main__":
input_image_path = "DIP_SUBJECT.JPG"
output_image_path = "grayscale_image.jpg"
convert_to_grayscale(input_image_path,
output_image_path)

INPUT IMAGE OUTPUT IMAGE

2. Write a Python function that performs histogram equalization on a grayscale image to


enhance its contrast.
import cv2
import numpy as np
def histogram_equalization(image_path,
output_path):
grayscale_image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
if grayscale_image is None:
print("Error: Unable to load image. Please check the file
path.") return equalized_image =
cv2.equalizeHist(grayscale_image) cv2.imwrite(output_path,
equalized_image) print(f"Equalized image saved at {output_path}")
if __name__ ==
"__main__":
input_image_path = "DIP_SUBJECT.JPG" output_image_path
= "equalized_image.jpg"
histogram_equalization(input_image_path, output_image_path)

INPUT IMAGE OUTPUT IMAGE


3.
Implement a Gaussian filter to smooth an image and reduce noise. Display the original
and smoothed images.

import cv2 import numpy as np from


matplotlib import pyplot as plt
def
apply_gaussian_filter(image_path):
image = cv2.imread(image_path)
if image is None:
print("Error: Unable to load image. Please check the file
path.") return smoothed_image = cv2.GaussianBlur(image, (5,
5), 0) plt.subplot(1, 2, 1) plt.title("Original Image")
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) plt.subplot(1, 2,
2) plt.title("Smoothed Image")
plt.imshow(cv2.cvtColor(smoothed_image, cv2.COLOR_BGR2RGB))
plt.show()
if __name__ ==
"__main__":
input_image_path = "DIP_SUBJECT.JPG"
apply_gaussian_filter(input_image_path)
4.

Write a Python program to reduce noise in an image using a bilateral filter while
preserving edges.

import cv2 from matplotlib import


pyplot as plt
def
apply_bilateral_filter(image_path):
image = cv2.imread(image_path)
if image is None:
print("Error: Unable to load image. Please check the file
path.") return filtered_image = cv2.bilateralFilter(image,
9, 75, 75) plt.subplot(1, 2, 1) plt.title("Original Image")
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) plt.subplot(1, 2,
2) plt.title("Filtered Image")
plt.imshow(cv2.cvtColor(filtered_image, cv2.COLOR_BGR2RGB))
plt.show()
if __name__ ==
"__main__":
input_image_path = "DIP_SUBJECT.JPG"
apply_bilateral_filter(input_image_path)
5.

Write a Python function to apply the Canny edge detection algorithm to an image and
visualize the detected edges.
import cv2 from matplotlib import
pyplot as plt
def
apply_canny_edge_detection(image_path):
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
if image is None:
print("Error: Unable to load image. Please check the file
path.") return edges = cv2.Canny(image, 100, 200)
plt.subplot(1, 2, 1) plt.title("Original Image")
plt.imshow(image, cmap="gray") plt.subplot(1, 2, 2)
plt.title("Edge Detected Image") plt.imshow(edges, cmap="gray")
plt.show()
if __name__ ==
"__main__":
input_image_path = "DIP_SUBJECT.JPG"
apply_canny_edge_detection(input_image_path)
6.

You might also like