CV-Mini Project 2
CV-Mini Project 2
Technology, Nawabshah.
Roll no : 21ai47
Date : 12-Aug-2024
1 |21AI47
Thesis: Enhancing Images Using Python: A Practical
Implementation
Abstract
This thesis presents a practical implementation of an image enhancement application using
Python. The application utilizes OpenCV's Deep Learning Super-Resolution (DNN-SR) model
to upscale images and improve their quality. It integrates various image processing
techniques, including CLAHE (Contrast Limited Adaptive Histogram Equalization) and
bilateral filtering, and provides a graphical user interface (GUI) for user interaction. The
project demonstrates the capability of Python in handling advanced image processing tasks
with a user-friendly interface.
Introduction
With the advent of digital imaging, the need for high-quality images has increased
significantly. Image enhancement is a crucial aspect of improving image quality, especially
for applications requiring clear and detailed visuals. This thesis explores the development of
a Python-based image enhancement tool that allows users to upscale and improve images
using advanced algorithms and a stylish GUI.
Objectives
Develop an Image Enhancement Tool:
Integrate additional image processing techniques to further enhance the image quality.
Design a GUI that allows users to select, enhance, and save images with ease.
Optimize the application for quality and performance, ensuring enhanced images are clear
and accurately processed.
2 |21AI47
Methodology
1. Image Processing Techniques:
Deep Learning Super-Resolution (DNN-SR): The ESPCN_x4.pb model, part of the Efficient Sub-
Pixel Convolutional Network (ESPCN) architecture, is designed for high-performance image super-
resolution tasks. This model leverages deep learning techniques to enhance image resolution by a
factor of four, effectively reconstructing finer details and textures that are typically lost in low-
resolution images. By utilizing sub-pixel convolution layers, the ESPCN optimally processes features
at multiple scales, enabling it to produce high-quality upscaled images with minimal artifacts. This
model is particularly valuable in applications such as digital photography, video enhancement, and
medical imaging, where clarity and detail are paramount. Its efficiency and effectiveness make it a
popular choice for researchers and developers working in the field of computer vision and image
processing.
Bilateral Filtering: Used to reduce noise while maintaining edge sharpness, enhancing the
overall image quality.
2. GUI Development:
Toolkit Used: Tkinter and ThemedTk libraries are used to create a stylish and functional GUI.
Features:
Image Saving: Save the enhanced image with a unique, random filename to avoid
overwriting.
Implementation
Import Required Libraries:
import cv2
import numpy as np
import os
import tkinter as tk
3 |21AI47
from tkinter import filedialog, messagebox, ttk
import uuid
Class Definition: The ImageEnhancer class encapsulates the functionality for loading,
processing, and saving images, as well as the GUI components.
GUI Components:
Enhance Image Button: Processes the selected image using the enhancement algorithms.
Save Image Button: Saves the processed image with a unique, random filename.
Image Upscaling:
return upscaled_image
return blended_image
def save_image(self):
if hasattr(self, 'enhanced_image'):
4 |21AI47
output_folder = 'Enhanced'
os.makedirs(output_folder, exist_ok=True)
random_filename = f"{uuid.uuid4().hex}.jpg"
cv2.imwrite(output_path, self.enhanced_image)
Results
Screenshots:
Before After
5 |21AI47
Before After
GUI Screenshot:
Shows the main interface with buttons for selecting and processing images.
6 |21AI47
After selecting Image we Click to Enhance Image.
7 |21AI47
Performance:
The application successfully enhances image quality by upscaling and applying advanced
filtering techniques.
Conclusion:
This Python-based image enhancement tool demonstrates the effectiveness of modern
image processing techniques and their integration into a user-friendly application. The use
of DNN-SR for upscaling, combined with CLAHE and bilateral filtering, results in improved
image quality, making this tool a valuable asset for users seeking high-quality image
enhancements.
8 |21AI47
Full Code
import cv2
import numpy as np
import os
import tkinter as tk
import uuid
class ImageEnhancer:
self.root = root
self.root.title("Image Enhancer")
self.image_path = None
self.enhanced_image = None
self.style = ttk.Style()
self.frame.pack(fill=tk.BOTH, expand=True)
9 |21AI47
self.select_image_button = ttk.Button(self.frame, text="Select Image",
command=self.load_image)
self.select_image_button.pack(pady=10)
self.enhance_button.pack(pady=10)
self.save_button.pack(pady=10)
self.image_label = ttk.Label(self.frame)
self.image_label.pack(pady=10)
def load_image(self):
if file_path:
self.image_path = file_path
self.show_image(file_path)
self.enhance_button.config(state=tk.NORMAL)
image = Image.open(image_path)
new_width = max_width
10 |21AI47
new_height = int((max_width / width) * height)
else:
new_height = max_height
self.tk_image = ImageTk.PhotoImage(image)
self.image_label.config(image=self.tk_image)
def enhance_image(self):
if self.image_path:
image = cv2.imread(self.image_path)
if image is None:
return
sr = cv2.dnn_superres.DnnSuperResImpl_create()
model_path = "models/ESPCN_x4.pb"
if not os.path.exists(model_path):
return
try:
11 |21AI47
sr.readModel(model_path)
sr.setModel("espcn", 4)
except cv2.error as e:
return
intermediate_image = sr.upsample(image)
filter_opacity = 0.50
cl_l_channel = clahe.apply(l_channel)
clahe_opacity = 0.20
12 |21AI47
final_image = self.blend_images(blended_image, clahe_enhanced_image, clahe_opacity)
[-0.5, 3, -0.5],
self.enhanced_image = final_image
self.save_button.config(state=tk.NORMAL)
self.show_image_from_cv(final_image)
return upscaled_image
return blended_image
image = Image.fromarray(image)
13 |21AI47
if width > height:
new_width = max_width
else:
new_height = max_height
self.tk_image = ImageTk.PhotoImage(image)
self.image_label.config(image=self.tk_image)
def save_image(self):
if hasattr(self, 'enhanced_image'):
output_folder = 'Enhanced'
os.makedirs(output_folder, exist_ok=True)
random_filename = f"{uuid.uuid4().hex}.jpg"
cv2.imwrite(output_path, self.enhanced_image)
if __name__ == "__main__":
app = ImageEnhancer(root)
root.mainloop()
14 |21AI47
Future Work:
Algorithm Optimization: Explore additional algorithms and techniques for further image
improvement.
Extended Features: Include options for adjusting filter parameters and other advanced
image processing functions.
15 |21AI47