- Version: 20261019
- Source Code Copyright: Apache License 2.0 — OpenCV tutorial sample code
- Redistribution Restriction: This source code is integrated by LEADERG INC. Please use it privately. Please DO NOT distribute it to internet.
Console/terminal demo of histogram calculation with OpenCV, supporting Windows 11, Ubuntu Linux 24.04 and macOS 14+ (Apple Silicon), based on the OpenCV histogram-calculation tutorial: the input is split into its B, G and R planes, cv2.calcHist computes a 256-bin histogram per channel, the histograms are min-max normalized to the plot height, and the three curves (blue, green, red) are drawn on a black canvas. Three example images ship under data/; the result is saved to model/histogram/. CPU only: the pip opencv-python build has no GPU backend (no CUDA, no Apple GPU), and one image takes milliseconds.
[Folder Structure]
- CLAUDE.md / AGENTS.md: Program specification (identical content).
- CHANGELOG.md: Change log.
- copyright.txt: Copyright notice.
- Image-OpenCV-Histogram-Calculation-Python.html: This document (usage page).
- data/: Example images: Lena.png (the notebook's example), sample.png, SC-150.png.
- model/histogram/: Result: histogram.png (written by the demo; no example output ships).
- tool/tools.py: Shared helpers.
- src/: The demo and launcher scripts.
- venv/: Python virtual environment, created by 02_venv_create_windows/linux/mac.
[Prerequisites]
- Python 3.14, installed by 01_python_install_windows.bat / 01_python_install_linux.sh / 01_python_install_mac.sh (Windows: winget; Ubuntu: apt, or the deadsnakes PPA when the Ubuntu archive has no python3.14; macOS: Homebrew).
- 02_venv_create_windows.bat / 02_venv_create_linux.sh / 02_venv_create_mac.sh create venv/ with Python 3.14 and install numpy and opencv-python from src/requirements.txt (same packages on every OS).
- GPU: none. CPU only on every OS, because the pip opencv-python build has no GPU backend; no NVIDIA driver or CUDA is needed.
[Launcher Scripts]
1. 01_python_install_windows.bat / 01_python_install_linux.sh / 01_python_install_mac.sh
Install Python 3.14 (winget / apt or deadsnakes PPA, needs sudo / Homebrew). Skips the install when Python 3.14 is already present. On Windows, open a new terminal afterwards.
2. 02_venv_create_windows.bat / 02_venv_create_linux.sh / 02_venv_create_mac.sh
Delete the old venv/, create a fresh one with Python 3.14, install src/requirements.txt. Aborts loudly if pip fails.
3. 03_venv_activate.bat / 03_venv_activate.sh
Activate the venv. On Linux/macOS, source it: source src/03_venv_activate.sh (bash or zsh). If the project folder was renamed after the venv was created, re-create the venv with 02_venv_create_windows/linux/mac first; the other launchers do not need activation.
4. 04_histogram_calculation.bat / 04_histogram_calculation.sh
Run the histogram-calculation demo (extra arguments are forwarded).
5. 99_venv_clean.bat / 99_venv_clean.sh
Delete the venv/ folder.
6. 99_venv_deactivate.bat / 99_venv_deactivate.sh
Deactivate the venv in the current shell. On Linux/macOS, source it: source src/99_venv_deactivate.sh (bash or zsh).
[Python Entry Point]
src/histogram_calculation.py
[--source IMG] [--width 512] [--height 400] [--output-dir DIR] [--show]. Defaults follow the tutorial: data/Lena.png, 256 bins per B/G/R channel, plot 512x400. Saves histogram.png to model/histogram/; --show also opens the result in a window. Each bin is round(width / 256) pixels wide, so pick a multiple of 256 for --width. Works with OpenCV 4 and 5. Runs on the CPU (OpenCV's pip build has no GPU backend); there is no --device option.
[Quick Start]
Windows 11
src\01_python_install_windows.bat
REM open a new terminal
src\02_venv_create_windows.bat
src\04_histogram_calculation.bat
Ubuntu 24.04
bash src/01_python_install_linux.sh
bash src/02_venv_create_linux.sh
bash src/04_histogram_calculation.sh
macOS 14+ (Apple Silicon)
bash src/01_python_install_mac.sh
bash src/02_venv_create_mac.sh
bash src/04_histogram_calculation.sh
Your own image and plot size:
bash src/04_histogram_calculation.sh --source data/SC-150.png --width 768 --height 300
[References]
- OpenCV histogram-calculation tutorial (the notebook's source): docs.opencv.org/master/d8/dbc/tutorial_histogram_calculation.html
This SDK is built in AppForAI - AI Dev Tools.