Console/terminal demo of OpenCV opening and closing, supporting Windows 11 and Ubuntu Linux 24.04. The input is binarized and then both compound morphology operations are applied. Opening is an erosion followed by a dilation: the erosion wipes out small white specks and the dilation restores the surviving shapes to roughly their original size, so isolated noise disappears without the objects shrinking. Closing is the reverse, a dilation followed by an erosion, which fills small black holes and joins nearby fragments. Two example images ship under data/ (Lena, sample) and the results are saved to model/morphology/. No GPU is involved.
[Folder Structure]
- CLAUDE.md / AGENT.md: program specification (identical content).
- CHANGELOG.md: change log.
- copyright.txt: copyright notice.
- data/: the example images Lena.png (the notebook's example) and sample.png.
- model/morphology/: the results gray.png, binary.png, opening.png and closing.png (ship with the Lena examples).
- tool/tools.py: shared helpers.
- src/: the demo, the launcher scripts and this document.
- venv/: Python virtual environment, created by step 1.
[Prerequisites]
- Operating system: Windows 11 or Ubuntu Linux 24.04.
- Python 3 and pip.
- Dependencies are listed in src/requirements.txt and installed by step 1.
- No GPU required.
[Instructions]
Step 1: create the virtual environment
Delete the old venv/, create a fresh one and install src/requirements.txt. Aborts loudly if pip fails.
Windows 11: src\01_venv_create.bat
Ubuntu 24.04: bash src/01_venv_create.sh
Step 2: activate the virtual environment
Activate the venv you just created. On Linux, source it so the environment variables apply to the current shell.
Windows 11: src\02_venv_activate.bat
Ubuntu 24.04: source src/02_venv_activate.sh
Step 3: run the opening-and-closing demo
Binarize the input, apply both operations and save the results. Extra arguments are forwarded to the Python script.
Windows 11: src\03_opening_closing.bat
Ubuntu 24.04: bash src/03_opening_closing.sh
Your own image and parameters:
bash src/03_opening_closing.sh --source data/sample.png --kernel-shape ellipse --kernel-size 5
Step 4: clear the virtual environment (optional)
Delete the venv/ folder to free disk space when you no longer need it.
Windows 11: src\99_venv_clear.bat
Ubuntu 24.04: bash src/99_venv_clear.sh
[Python Entry Point]
- src/opening_closing.py: [--source IMG] [--threshold 110] [--max-value 255] [--kernel-shape rect|cross|ellipse] [--kernel-size 3] [--open-iterations 2] [--close-iterations 1] [--output-dir DIR] [--show].
- --source: input image, default data/Lena.png.
- --threshold / --max-value: the binarization threshold and maximum value, default 110 and 255.
- --kernel-shape: the shape of the structuring element, rect (default), cross or ellipse.
- --kernel-size: the size of the structuring element, default 3 (3x3). Larger kernels remove or fill larger features.
- --open-iterations / --close-iterations: how many times to repeat each operation, default 2 and 1 (the notebook's values).
- --output-dir: output folder, default model/morphology/. --show also opens the results in windows.
- Saves gray.png, binary.png, opening.png and closing.png.
[References]
- OpenCV morphological transformations tutorial: docs.opencv.org/4.x/d9/d61/tutorial_py_morphological_ops.html
This SDK is built in AppForAI - AI Dev Tools.
Purchase license separately: USD 600, permanent authorization, single APP authorization, single machine authorization, one-year activation, one-year download, one-year update, one-year email technical support.