An interactive image processing application built with Streamlit that allows you to automatically replace backgrounds in images using HSV color space segmentation and morphological operations. Perfect for creating professional photos, removing unwanted backgrounds, and creative image editing.
- πΌοΈ Dual Image Upload: Upload foreground and background images
- π― HSV Color Detection: Precise background color targeting
- π§ Real-time Parameter Tuning: Interactive sliders for fine control
- π§Ή Morphological Operations: Noise reduction and mask refinement
- π± Live Preview: See results instantly as you adjust parameters
- πΎ Download Results: Export final images as PNG
- π¨ Visual Pipeline: Step-by-step processing visualization
- Streamlit - Interactive web application
- OpenCV - Computer vision and image processing
- NumPy - Numerical computations
- PIL (Pillow) - Image handling and export
- Matplotlib - Visualization support
- Python 3.7 or higher
- pip package manager
git clone https://github.com/RahulKumar2340029/Image-Segmentation.git
cd Image-Segmentation
pip install streamlit opencv-python numpy pillow matplotlibstreamlit run app.pyThe app will open in your browser at http://localhost:8501
- Foreground Image: Upload the image with the background you want to remove
- Background Image: Upload the new background you want to add
Use the sidebar sliders to define the background color range:
- Hue: Color type (0-179)
- Saturation: Color intensity (0-255)
- Value: Brightness (0-255)
- Enable/Disable: Toggle morphological operations
- Closing Kernel: Fill gaps in background detection
- Opening Kernel: Remove noise and small artifacts
Click "Download Result" to save your processed image
- Preprocessing: Apply Gaussian blur to reduce noise
- Color Space Conversion: Convert BGR to HSV for better color separation
- Background Masking: Create mask using HSV color thresholding
- Morphological Refinement: Apply closing and opening operations
- Foreground Extraction: Invert mask to get foreground
- Background Replacement: Combine foreground with new background
- HSV Thresholding:
mask = cv2.inRange(hsv, lower_bound, upper_bound) - Morphological Closing: Fills small gaps in detected regions
- Morphological Opening: Removes small noise elements
- Bitwise Operations: Combine foreground and background seamlessly
- Uniform Backgrounds: Solid colors work better than complex patterns
- Good Lighting: Even lighting reduces shadows and color variations
- Contrasting Colors: Foreground should contrast well with background
- High Resolution: Better quality images produce cleaner results
- Start with wide HSV ranges and narrow down gradually
- Use larger closing kernels for backgrounds with gaps
- Use smaller opening kernels to preserve detail
- Preview masks to verify detection accuracy
- Product photography background removal
- Portrait background replacement
- Real estate photo enhancement
- Artistic photo compositions
- Social media content creation
- Digital art and design
- Computer vision learning
- Image processing demonstrations
- HSV color space understanding
- JPEG (.jpg, .jpeg)
- PNG (.png)
- High Resolution: Up to 4K images supported
- PNG with transparency support
- Lossless compression for best quality
- Intuitive: Separates color information from lighting
- Robust: Less sensitive to lighting variations
- Effective: Better for color-based segmentation than RGB
- Closing: Connects nearby regions, fills small holes
- Opening: Removes small noise, separates connected objects
- Kernel Size: Larger kernels = stronger effects
- Processing Speed: Real-time for images up to 2MP
- Memory Usage: Scales with image resolution
- Browser Compatibility: Works with all modern browsers
- Mobile Support: Responsive design for mobile devices
- Multiple Background Detection: Handle complex multi-colored backgrounds
- Edge Refinement: Smoother foreground edges using blur/feathering
- Batch Processing: Process multiple images simultaneously
- Advanced Filters: Gaussian blur, sharpen, color correction
- Preset Templates: Common background removal scenarios
- GPU Acceleration: Faster processing for large images
- Machine Learning: AI-based background detection
- Video Support: Background replacement for video files
- API Integration: RESTful API for developers
Background not detected properly:
- Adjust HSV ranges using the sliders
- Try different lighting conditions
- Use more uniform background colors
Edges look rough:
- Increase closing kernel size
- Apply additional blur preprocessing
- Use higher resolution images
Foreground parts missing:
- Narrow down HSV ranges
- Disable morphological operations temporarily
- Check for color similarity between foreground and background
App running slowly:
- Reduce image resolution before upload
- Close other browser tabs
- Check system memory usage
We welcome contributions! Here's how to get involved:
- Fork the repository
- Create a feature branch
- Make your improvements
- Test thoroughly with various images
- Submit a pull request
- Algorithm improvements
- UI/UX enhancements
- Performance optimizations
- Documentation updates
- Bug fixes and testing
MIT License Β© 2024 Rahul Kumar
- OpenCV Community for excellent computer vision tools
- Streamlit Team for the amazing web framework
- Contributors who help improve this tool
β Star this repo if you find it useful!
Perfect for photographers, designers, and anyone who needs quick background replacement
View Repository β’ Report Bug β’ Request Feature