0% found this document useful (0 votes)
16 views3 pages

task3

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)
16 views3 pages

task3

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/ 3

Image Processing to Sharpen an Image

Introduction
This report explains the process of sharpening an image using Python. The image is loaded, converted
to the RGB format for display compatibility with Matplotlib, and processed using a sharpening kernel to
enhance its details. The implementation employs OpenCV, NumPy, and Matplotlib for image manipulation,
computations, and visualization.
The input image is located at C:\Users\Shrikanth\OneDrive\Desktop\task3.jpg. The output, including
the original and sharpened images, is displayed for comparison. Additionally, the sharpened image is saved to
a specified directory for further use.

Objectives
1. Load the image file using OpenCV.
2. Apply a sharpening filter using a predefined kernel.
3. Display the original and sharpened images using Matplotlib.
4. Save the processed sharpened image to the desired location.

Tools and Libraries


The following libraries are utilized in this program:
1. OpenCV (cv2):
o A powerful library for image processing and manipulation.
o Used for loading images, applying filters, and saving results.
2. NumPy (np):
o A Python library for numerical computations.
o Used to define and manipulate the sharpening kernel.
3. Matplotlib (plt):
o A Python library for visualization.
o Used to display the original and sharpened images side-by-side.

Methodology
1. Image Loading
o The image is loaded using OpenCV’s cv2.imread() function with the specified path.
o Error handling ensures the program provides an appropriate message if the image cannot be
loaded.
2. Sharpening Filter
o A predefined sharpening kernel is created using NumPy.
o OpenCV’s cv2.filter2D() function applies the kernel to the image, enhancing its details.
3. Image Conversion for Visualization
o The image is converted from BGR (OpenCV format) to RGB for compatibility with
Matplotlib.
o Both the original and processed images are prepared for visualization.
4. Visualization
o The original and sharpened images are displayed side-by-side using Matplotlib for comparison.
5. Image Saving
o The processed sharpened image is saved to the specified path using OpenCV’s cv2.imwrite()
function.

Program
import cv2
import numpy as np
import matplotlib.pyplot as plt

1
# Image path
image_path = r"C:\Users\Shrikanth\OneDrive\Desktop\task3.jpg"

# Load the image


image = cv2.imread(image_path)
if image is None:
print("Error: Unable to load the image. Check the file path.")
else:
# Convert BGR to RGB for Matplotlib display
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Define a sharpening kernel


sharpening_kernel = np.array([[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]])

# Apply the kernel to the image


sharpened_image = cv2.filter2D(image, -1, sharpening_kernel)

# Convert the sharpened image to RGB


sharpened_image_rgb = cv2.cvtColor(sharpened_image, cv2.COLOR_BGR2RGB)

# Display the images using Matplotlib


plt.figure(figsize=(10, 5))

# Display original image


plt.subplot(1, 2, 1)
plt.imshow(image_rgb)
plt.title('Original Image')
plt.axis('off')

# Display sharpened image


plt.subplot(1, 2, 2)
plt.imshow(sharpened_image_rgb)
plt.title('Sharpened Image')
plt.axis('off')

plt.show()

# Save the sharpened image


output_path = r"C:\Users\Shrikanth\OneDrive\Desktop\sharpened_task3.jpg"
cv2.imwrite(output_path, sharpened_image)
print(f"Sharpened image saved to: {output_path}")

Results
1. Output Path:
The sharpened image is saved successfully to:
C:\Users\Shrikanth\OneDrive\Desktop\sharpened_task3.jpg.

2
2. Original Image:

Fig 1 . Original Image

3. Sharpened Image:

Fig 2. Sharpened Image

Applications
1. Image Enhancement:
o Improves details and edges in images for better clarity.
2. Preprocessing for Computer Vision:
o Enhances features that are crucial for machine learning models.
3. Medical Imaging:
o Enhances critical features in X-rays, MRIs, and other scans.
4. Photography:
o Improves the sharpness of images for professional editing and publishing.

Conclusion
The described process effectively demonstrates the use of Python and its libraries to sharpen an image
and visualize the results. The sharpening kernel enhances details, making it a valuable tool in applications
requiring image preprocessing. By leveraging OpenCV, NumPy, and Matplotlib, the workflow is efficient,
modular, and adaptable for various image processing tasks.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy