Welcome to this repository! This project showcases a variety of image processing features implemented using Python. Below, you'll find an overview of the three key features included in this repository:
- Blemish Removal output
blemish_removal_output.mp4
- Chroma Keying Output
chroma_keying_output.mp4
The blemish removal tool is an interactive image-editing utility designed to remove unwanted spots or blemishes from an image. It allows users to click on a blemish in the image, automatically identifies the best replacement patch, and blends the patch seamlessly into the selected area. This is achieved through gradient-based patch selection and seamless cloning using OpenCV.
-
sobel_filter(crop_img)
- Calculates gradients in the x and y directions for a given image patch using the Sobel operator.
- These gradients are used to identify texture changes, which help find smooth patches for replacement.
-
append_dictionary(x, y, r, source)
- Extracts a patch from the image and calculates its gradients using
sobel_filter
. - Returns the gradient information, which helps assess the patch's smoothness.
- Extracts a patch from the image and calculates its gradients using
-
identify_best_patch(x, y, r, source)
- Searches for candidate patches around the blemish location.
- Compares patches and selects the one with the smoothest gradients (lowest combined x and y gradients) for replacement.
-
selected_blemish(x, y, r, source)
- Wrapper function that calls
identify_best_patch
and returns the optimal patch location for a blemish.
- Wrapper function that calls
-
blemish_removal(action, x, y, flags, userdata)
- Handles mouse events, allowing the user to select blemishes interactively:
- Left Mouse Click: Selects a blemish and replaces it with the best-matching patch using OpenCV's
seamlessClone
. - Mouse Release: Updates and displays the modified image.
- Left Mouse Click: Selects a blemish and replaces it with the best-matching patch using OpenCV's
- Handles mouse events, allowing the user to select blemishes interactively:
-
Main Loop
- Sets up the OpenCV window and listens for user actions:
- Key 'C': Resets the image to its original state.
- Esc Key: Exits the application.
- Displays the interactive blemish removal tool.
- Sets up the OpenCV window and listens for user actions:
- Install OpenCV and NumPy:
pip install opencv-python numpy
- Execute the script:
python 02_blemish_removal.py
- Removing small imperfections in portrait images for photo editing.
The chroma keying tool replaces a specific color (e.g., green screen) in a video or image with a new background. This technique is widely used in video editing, film production, and real-time streaming to create visually dynamic content.
-
chroma_key(foreground_frame, background_frame, lower_color, upper_color, softness=0)
- Implements the chroma key (green screen) effect by performing the following steps:
- Converts the foreground image to HSV color space.
- Creates a binary mask to isolate the specified color range (e.g., green screen).
- Optionally applies Gaussian blur to soften the edges of the mask.
- Extracts the subject from the foreground using the inverted mask.
- Resizes the background to match the size of the foreground frame.
- Replaces the masked area with the resized background.
- Implements the chroma key (green screen) effect by performing the following steps:
-
Main Loop
- Reads frames from the foreground (green screen) and background videos.
- Applies the
chroma_key
function frame-by-frame. - If the background video runs out of frames, loops it to ensure continuous playback.
- Displays the composited video output in real-time.
- Exits the application when the 'Q' key is pressed.