Loading repository dataโฆ
Loading repository dataโฆ
NhanPhamThanh-IT / repository
๐จ Transform photos into stunning cartoon artwork using OpenCV! Features advanced edge detection, color quantization & bilateral filtering. Includes Gradio web interface for easy use, Jupyter notebooks for exploration, and modular Python architecture. Perfect for learning computer vision concepts.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
Transform your photos into stunning cartoon-style artwork using the power of Computer Vision and Machine Learning
๐ Live Demo โข ๐ Documentation โข ๐ ๏ธ Installation โข ๐ก Features โข ๐ค Contributing
Cartoonify Images OpenCV is a sophisticated computer vision application that transforms ordinary photographs into captivating cartoon-style artwork. Built with OpenCV, NumPy, and enhanced with a user-friendly Gradio web interface, this project demonstrates the power of image processing techniques including edge detection, color quantization, and bilateral filtering.
Launch the Gradio web application to try cartoonifying your images instantly:
python app/main.py
Then open your browser and navigate to http://localhost:7860
Clone the Repository
git clone https://github.com/NhanPhamThanh-IT/Cartoonify-Images-OpenCV.git
cd Cartoonify-Images-OpenCV
Create Virtual Environment (Recommended)
# Windows
python -m venv cartoonify_env
cartoonify_env\Scripts\activate
# macOS/Linux
python3 -m venv cartoonify_env
source cartoonify_env/bin/activate
Install Dependencies
pip install -r requirements.txt
Verify Installation
python -c "import cv2, numpy, gradio; print('All dependencies installed successfully!')"
| Package | Version | Purpose |
|---|---|---|
| opencv-python | Latest | Core computer vision operations |
| numpy | Latest | Numerical computations and array operations |
| gradio | Latest | Web interface and user interaction |
| matplotlib | Latest | Visualization and plotting (for exploration) |
# Navigate to the project directory
cd Cartoonify-Images-OpenCV
# Launch the web application
python app/main.py
Open your browser and go to http://localhost:7860 to start cartoonifying images!
from app.core.Cartoonizer import Cartoonizer
import cv2
# Initialize the cartoonizer
cartoonizer = Cartoonizer()
# Load and process an image
image = cv2.imread('path/to/your/image.jpg')
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Cartoonify the image
cartoon_image = cartoonizer.cartoonify(image_rgb)
# Save the result
cv2.imwrite('cartoonified_image.jpg', cv2.cvtColor(cartoon_image, cv2.COLOR_RGB2BGR))
# Navigate to exploration directory
cd exploration
# Launch Jupyter Notebook
jupyter notebook notebook.ipynb
Cartoonify-Images-OpenCV/
โ
โโโ ๐ README.md # Comprehensive project documentation
โโโ ๐ LICENSE # MIT License
โโโ ๐ requirements.txt # Python dependencies
โ
โโโ ๐ app/ # Main application directory
โ โโโ ๐ main.py # Gradio web interface entry point
โ โ
โ โโโ ๐ config/ # Configuration management
โ โ โโโ ๐ __init__.py # Package initialization
โ โ โโโ ๐ AppVariables.py # Application constants and settings
โ โ
โ โโโ ๐ core/ # Core processing logic
โ โโโ ๐ __init__.py # Package initialization
โ โโโ ๐ Cartoonizer.py # Main cartoonification algorithms
โ
โโโ ๐ docs/ # Documentation and guides
โ โโโ ๐ gradio.md # Gradio framework guide
โ โโโ ๐ matplotlib-pyplot.md # Matplotlib visualization guide
โ โโโ ๐ opencv.md # OpenCV techniques documentation
โ
โโโ ๐ exploration/ # Research and experimentation
โโโ ๐ notebook.ipynb # Jupyter notebook for exploration
โโโ ๐ images/ # Sample images for testing
โโโ ๐ image.jpg # Test image
The cartoonification process involves several sophisticated computer vision techniques:
def edge_mask(self, img):
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray_blur = cv2.medianBlur(gray, self.blur_value)
edges = cv2.adaptiveThreshold(
gray_blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY, self.line_size, self.blur_value
)
return edges
def color_quantization(self, img):
data = np.float32(img).reshape((-1, 3))
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 20, 0.001)
_, label, center = cv2.kmeans(
data, self.k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS
)
return result.reshape(img.shape)
def apply_bilateral_filter(self, img):
return cv2.bilateralFilter(img, d=7, sigmaColor=200, sigmaSpace=200)
The final cartoon effect is achieved by combining the edge mask with the processed image:
cartoon = cv2.bitwise_and(blurred, blurred, mask=edges)
You can customize the cartoonification process by adjusting these parameters:
| Parameter | Default | Range | Description |
|---|---|---|---|
line_size | 7 | 3-15 | Controls edge line thickness |
blur_value | 7 | 3-15 | Median blur intensity |
k | 9 | 3-20 | Number of color clusters |
# Create a custom cartoonizer with specific parameters
custom_cartoonizer = Cartoonizer(
line_size=5, # Thinner lines
blur_value=9, # More blur
k=12 # More color variety
)
# Apply custom cartoonification
result = custom_cartoonizer.cartoonify(your_image)
Modify app/config/AppVariables.py to customize the web interface:
class AppVariables:
TITLE = "Your Custom Cartoonify App"
DESCRIPTION = "<center>Transform your photos into amazing cartoons!</center>"
# Additional customizable parameters
MAX_IMAGE_SIZE = (1920, 1080)
SUPPORTED_FORMATS = ['jpg', 'jpeg', 'png', 'bmp']
DEFAULT_LINE_SIZE = 7
DEFAULT_BLUR_VALUE = 7
DEFAULT_K_VALUE = 9
The exploration/notebook.ipynb provides:
| Original | Cartoonified | Technique Highlight |
|---|---|---|
| Portrait Photo | Cartoon Portrait | Excellent skin smoothing |
| Landscape | Cartoon Landscape | Perfect color quantization |
| Architecture | Stylized Building | Sharp edge preservation |
| Nature Scene | Artistic Nature | Beautiful color harmony |