Skip to content

imehranasgari/OpenCV-Color-Masking-Showcase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Of course. Here is a professional, employer-oriented README.md file based on the content of your notebooks.


Advanced Image Manipulation with OpenCV

This project demonstrates a practical application of computer vision techniques to perform targeted image enhancements. The primary focus is on leveraging different color spaces and alpha channels to selectively manipulate image properties, such as color saturation and transparency.


## Problem Statement and Goal of Project

In standard image editing, applying a filter or enhancement affects the entire image uniformly. The goal of this project was to implement a more intelligent approach: selectively enhancing the color saturation of a subject in a photograph without altering the grayscale background. Additionally, I aimed to use this same logic to completely remove the background by making it transparent, a foundational task in image segmentation and composition.


## Solution Approach

My approach involved a two-stage process using OpenCV and NumPy:

  1. Selective Saturation Enhancement:

    • I first converted the input image from the default BGR color space to HSV (Hue, Saturation, Value). The HSV model aligns more closely with human perception of color, allowing me to isolate color intensity (Saturation) from brightness (Value).
    • By analyzing the Saturation channel, I created a binary mask that cleanly separated the colored subject from the near-zero saturation (grayscale) background.
    • I then increased the saturation values across the entire image but used the generated mask to apply this enhancement only to the subject, leaving the background untouched.
  2. Background Removal via Alpha Channel:

    • To make the background transparent, I converted the original 3-channel BGR image into a 4-channel BGRA image. The 'A' represents the Alpha channel, which controls transparency.
    • I assigned the binary mask created in the first stage directly to this new alpha channel. As a result, the areas corresponding to the subject (white in the mask) became fully opaque, while the background (black in the mask) became fully transparent.

## Technologies & Libraries

  • Python
  • OpenCV: Core library for all image processing tasks, including color space conversions, thresholding, and channel manipulation.
  • NumPy: Used for efficient array operations and conditional logic (np.where) to apply the mask.
  • Matplotlib: Utilized for visualizing the original image, individual channels, and the final results.

## Description about Dataset

The project uses a single sample photograph of a person against a plain white background. This image was chosen specifically because it contains both colored elements (the subject) and grayscale elements (the background), making it ideal for demonstrating the effectiveness of saturation-based masking.


## Installation & Execution Guide

  1. Clone the repository to your local machine:
    git clone <repository-url>
  2. Navigate to the project directory:
    cd <repository-name>
  3. Install the required dependencies:
    pip install -r requirements.txt
    pip install opencv-python numpy matplotlib
  4. Launch Jupyter Notebook and open the .ipynb files to view and execute the code:
    jupyter notebook

## Key Results

  • Successfully implemented a pipeline to distinguish colored foreground objects from grayscale backgrounds using the HSV color space.
  • Demonstrated the ability to perform targeted color correction, enhancing subject vibrancy without introducing artifacts or unwanted changes to the background.
  • Effectively generated a transparent background for the image by dynamically creating and embedding an alpha channel mask.

## Sample Output

Here is a visual summary of the process, showing the original image, the isolated HSV channels, and the final modified images.


## Additional Learnings / Reflections

This project was an excellent exercise in moving beyond basic image filtering. I gained a deeper appreciation for the power of the HSV color space as a more intuitive tool for color-based segmentation compared to BGR. Implementing the logic with np.where and bitwise operations solidified my understanding of how masks are applied efficiently at a low level. Furthermore, the process of programmatically creating and assigning an alpha channel was a key learning outcome, bridging the gap between theoretical knowledge and practical application in tasks like data augmentation or preparing graphics for composition.


💡 Some interactive outputs (e.g., plots, widgets) may not display correctly on GitHub. If so, please view this notebook via nbviewer.org for full rendering.


🙏 Acknowledgments

This project represents my initial steps into the practical application of computer vision. The foundational knowledge and guidance for this work were derived from the outstanding OpenCV course taught by Alireza Akhavanpour on the Maktabkhooneh platform. His ability to deconstruct complex topics into clear, actionable steps was instrumental in the successful implementation of this project.


## 👤 Author


## 📄 License

This project is licensed under the Apache 2.0 License – see the LICENSE file for details.

About

Using OpenCV and HSV color space for selective saturation enhancement and background removal via alpha masking.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors