fusion-metrics 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fusion_metrics-1.0.0/LICENSE +26 -0
- fusion_metrics-1.0.0/NOTICE +27 -0
- fusion_metrics-1.0.0/PKG-INFO +236 -0
- fusion_metrics-1.0.0/README.md +213 -0
- fusion_metrics-1.0.0/fusion_metrics/__init__.py +68 -0
- fusion_metrics-1.0.0/fusion_metrics/_matlab.py +84 -0
- fusion_metrics-1.0.0/fusion_metrics/basic.py +160 -0
- fusion_metrics-1.0.0/fusion_metrics/cli.py +93 -0
- fusion_metrics-1.0.0/fusion_metrics/fmi.py +173 -0
- fusion_metrics-1.0.0/fusion_metrics/fsim.py +123 -0
- fusion_metrics-1.0.0/fusion_metrics/info.py +197 -0
- fusion_metrics-1.0.0/fusion_metrics/variants.py +57 -0
- fusion_metrics-1.0.0/fusion_metrics.egg-info/PKG-INFO +236 -0
- fusion_metrics-1.0.0/fusion_metrics.egg-info/SOURCES.txt +18 -0
- fusion_metrics-1.0.0/fusion_metrics.egg-info/dependency_links.txt +1 -0
- fusion_metrics-1.0.0/fusion_metrics.egg-info/entry_points.txt +2 -0
- fusion_metrics-1.0.0/fusion_metrics.egg-info/requires.txt +3 -0
- fusion_metrics-1.0.0/fusion_metrics.egg-info/top_level.txt +1 -0
- fusion_metrics-1.0.0/pyproject.toml +31 -0
- fusion_metrics-1.0.0/setup.cfg +4 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vu Viet Anh (Vũ Việt Anh)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
-------------------------------------------------------------------------------
|
|
24
|
+
This license covers the original code of this repository. Some metrics are
|
|
25
|
+
translations of third-party code that remain under their authors' own terms
|
|
26
|
+
(educational and research use only); see NOTICE.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
fusion-metrics — third-party notices
|
|
2
|
+
====================================
|
|
3
|
+
|
|
4
|
+
The Python package is released under the MIT License (see LICENSE), EXCEPT for the
|
|
5
|
+
parts listed below, which are translations of third-party MATLAB code and remain under
|
|
6
|
+
their original authors' terms. Those terms allow use, copying and modification for
|
|
7
|
+
EDUCATIONAL AND RESEARCH PURPOSES ONLY, without fee, provided the copyright notice and
|
|
8
|
+
the original authors' names are kept, and the authors are cited in any publication that
|
|
9
|
+
uses the code. Commercial use of these parts requires permission from their authors.
|
|
10
|
+
|
|
11
|
+
Python port Original work
|
|
12
|
+
----------------------------------- ------------------------------------------------------
|
|
13
|
+
fusion_metrics/fsim.py (Q_FSIM) FSIM — Copyright (c) 2010 Lin Zhang, Lei Zhang,
|
|
14
|
+
Xuanqin Mou and David Zhang. Phase congruency
|
|
15
|
+
(phasecong2, lowpassfilter) after Peter Kovesi.
|
|
16
|
+
fusion_metrics/info.py (Q_VIFF) VIFF — Copyright (c) 2012 Yu Han. "A new image fusion
|
|
17
|
+
performance metric based on visual information
|
|
18
|
+
fidelity", Information Fusion 14 (2013).
|
|
19
|
+
fusion_metrics/variants.py VIF — H. R. Sheikh and A. C. Bovik, "Image information
|
|
20
|
+
(VIF_Sheikh) and visual quality", IEEE TIP 15 (2006), LIVE / The
|
|
21
|
+
University of Texas at Austin.
|
|
22
|
+
|
|
23
|
+
The MATLAB reference files in matlab/ and matlab_added/ are distributed for
|
|
24
|
+
verification. Files that carry their own copyright notice keep it unchanged and are
|
|
25
|
+
governed by it, in particular: Q_FSIM.m, Q_VIFF.m, Q_S.m and MS_SSIM.m (SSIM,
|
|
26
|
+
Copyright (c) 2003 Zhou Wang), and Q_UIQI.m (Copyright (c) 2001 The University of Texas
|
|
27
|
+
at Austin).
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fusion-metrics
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Objective image-fusion quality metrics (Q_ABF, MI, NCIE, FMI, FSIM, VIF, ...) ported line by line from MATLAB and verified to round-off precision
|
|
5
|
+
Author-email: Vu Viet Anh <vietanhresearcher@gmail.com>
|
|
6
|
+
Maintainer-email: Vu Viet Anh <vietanhresearcher@gmail.com>
|
|
7
|
+
License: MIT (see LICENSE and NOTICE for third-party-derived metrics)
|
|
8
|
+
Project-URL: Homepage, https://github.com/VuVietAnh2309/fusion-metrics
|
|
9
|
+
Project-URL: Issues, https://github.com/VuVietAnh2309/fusion-metrics/issues
|
|
10
|
+
Keywords: image fusion,medical image fusion,infrared visible fusion,evaluation metrics,image quality,matlab
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Image Processing
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
License-File: NOTICE
|
|
19
|
+
Requires-Dist: numpy
|
|
20
|
+
Requires-Dist: scipy
|
|
21
|
+
Requires-Dist: opencv-python
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
<div align="center">
|
|
25
|
+
|
|
26
|
+
# fusion-metrics
|
|
27
|
+
|
|
28
|
+
**Objective quality metrics for image fusion — ported line by line from MATLAB and verified to round-off precision.**
|
|
29
|
+
|
|
30
|
+
Developed by **[Vu Viet Anh](https://vuvietanh2309.github.io/)** (Vũ Việt Anh) · AI Engineer & Researcher, Hanoi University of Science and Technology
|
|
31
|
+
|
|
32
|
+
[](https://github.com/VuVietAnh2309/fusion-metrics/actions/workflows/test.yml)
|
|
33
|
+
[](https://github.com/VuVietAnh2309/fusion-metrics/blob/main/LICENSE)
|
|
34
|
+

|
|
35
|
+

|
|
36
|
+

|
|
37
|
+
|
|
38
|
+
[Quick start](#-quick-start) · [Metrics](#-metrics) · [CLI](#-command-line) · [Verification](#-verification) · [Citation](#-citation) · [Tiếng Việt](https://github.com/VuVietAnh2309/fusion-metrics/blob/main/README.vi.md)
|
|
39
|
+
|
|
40
|
+
</div>
|
|
41
|
+
|
|
42
|
+
<picture>
|
|
43
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/VuVietAnh2309/fusion-metrics/main/assets/verification-dark.svg">
|
|
44
|
+
<img alt="Verification pipeline: test images are scored by the original MATLAB files and by the Python port; values agree to within 1e-12" src="https://raw.githubusercontent.com/VuVietAnh2309/fusion-metrics/main/assets/verification.svg">
|
|
45
|
+
</picture>
|
|
46
|
+
|
|
47
|
+
## Why
|
|
48
|
+
|
|
49
|
+
Image-fusion papers report Q<sup>AB/F</sup>, MI, NCIE, FMI, VIF… but compute them with scattered MATLAB scripts and
|
|
50
|
+
ad-hoc Python re-implementations that quietly disagree: a different gray conversion, a one-pixel shift in
|
|
51
|
+
`conv2(...,'same')`, `uint8` saturation in a histogram, a different edge-thinning rule. The same method can then
|
|
52
|
+
score differently from paper to paper.
|
|
53
|
+
|
|
54
|
+
`fusion-metrics` gives one reference implementation that anyone can `pip install`:
|
|
55
|
+
|
|
56
|
+
- **MATLAB-faithful** — each function is a line-by-line port of a reference `.m` file, keeping its name, argument
|
|
57
|
+
order and numerical quirks.
|
|
58
|
+
- **Verified, not assumed** — a self-test compares every metric with values produced by the original `.m` files;
|
|
59
|
+
agreement is at round-off level (≤ 1e-12). It runs in CI on Python 3.10–3.13.
|
|
60
|
+
- **Ready for benchmarks** — score one image from Python, or a whole method folder from the command line to CSV.
|
|
61
|
+
|
|
62
|
+
## 🚀 Quick start
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install git+https://github.com/VuVietAnh2309/fusion-metrics.git
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
import fusion_metrics as fm
|
|
70
|
+
|
|
71
|
+
A = fm.read_gray("MRI.png") # MATLAB imread + rgb2gray -> uint8
|
|
72
|
+
B = fm.read_gray("PET.png") # pseudo-colour images are converted to gray
|
|
73
|
+
F = fm.read_gray("fused.png")
|
|
74
|
+
|
|
75
|
+
fm.Q_ABF(A, B, F) # one metric
|
|
76
|
+
fm.evaluate(A, B, F, "information") # a preset -> {"Q_MI": ..., "Q_NCIE": ..., ...}
|
|
77
|
+
fm.evaluate(A, B, F) # all 18 metrics
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## 📐 Metrics
|
|
81
|
+
|
|
82
|
+
`Q_X(A, B, F)` takes the two sources and the fused image; no-reference metrics take only `F`.
|
|
83
|
+
Inputs are grayscale `uint8` or `double` in [0, 255].
|
|
84
|
+
|
|
85
|
+
| Function | Metric | Original reference |
|
|
86
|
+
|---|---|---|
|
|
87
|
+
| `Q_MLI(F)` | Mean local intensity, [0, 1] | — |
|
|
88
|
+
| `Q_En(F)` | Entropy | Roberts et al., *J. Appl. Remote Sens.* 2008 |
|
|
89
|
+
| `SF(F)` | Spatial frequency (4 directions) | Eskicioglu & Fisher, *IEEE TCOM* 1995 |
|
|
90
|
+
| `Q_AG(F)` | Average gradient (forward differences, [0, 1]) | — |
|
|
91
|
+
| `AG_VIFB(F)` | Average gradient, VIFB definition (0–255) | Zhang et al., VIFB, *CVPRW* 2020 |
|
|
92
|
+
| `Q_MI(A,B,F)` | Mutual information MI(A,F) + MI(B,F), bits | Qu et al., *Electron. Lett.* 2002 |
|
|
93
|
+
| `Q_NCIE(A,B,F)` | Nonlinear correlation information entropy | Wang, Shen & Jin, 2008 |
|
|
94
|
+
| `Q_TE_Nava(A,B,F)` | Tsallis entropy, q = 0.43137 | Nava et al., *Proc. SPIE* 2007 |
|
|
95
|
+
| `Q_FMI(A,B,F)` | Feature mutual information on edge maps, w = 3 | Haghighat et al., *Comput. Electr. Eng.* 2011 |
|
|
96
|
+
| `Q_ABF(A,B,F)` | Edge-based Q<sup>AB/F</sup> | Xydeas & Petrović, *Electron. Lett.* 2000 |
|
|
97
|
+
| `Q_M(A,B,F)` | Multiscale edge preservation (Haar, 2 levels) | Wang & Liu, *ICSP* 2008 |
|
|
98
|
+
| `Q_SF(A,B,F)` | Relative spatial-frequency error | Zheng et al., *Inf. Fusion* 2007 |
|
|
99
|
+
| `Q_FSIM(A,B,F)` | Feature similarity, (FSIM(A,F) + FSIM(B,F)) / 2 | Zhang et al., *IEEE TIP* 2011 |
|
|
100
|
+
| `Q_PSNR(A,B,F)` | Mean of PSNR(F,A) and PSNR(F,B) | — |
|
|
101
|
+
| `Q_CC(A,B,F)` | Mean correlation coefficient | — |
|
|
102
|
+
| `Q_SCD(A,B,F)` | Sum of the correlations of differences | Aslantas & Bendes, *AEU* 2015 |
|
|
103
|
+
| `VIF_Sheikh(A,B,F)` | VIF(A,F) + VIF(B,F) | Sheikh & Bovik, *IEEE TIP* 2006 |
|
|
104
|
+
| `Q_VIFF(A,B,F)` | Visual information fidelity for fusion | Han et al., *Inf. Fusion* 2013 |
|
|
105
|
+
|
|
106
|
+
The taxonomy follows Liu et al., *“Objective assessment of multiresolution image fusion algorithms for context
|
|
107
|
+
enhancement in night vision”*, IEEE TPAMI 2012. **Please cite the original paper of every metric you report.**
|
|
108
|
+
|
|
109
|
+
> [!NOTE]
|
|
110
|
+
> Two pairs look alike but are different metrics: `Q_AG` vs `AG_VIFB` (different differences and scale), and
|
|
111
|
+
> `Q_VIFF` (Han et al.) vs `VIF_Sheikh` (Sheikh & Bovik). State which one you use.
|
|
112
|
+
|
|
113
|
+
### Presets
|
|
114
|
+
|
|
115
|
+
| Preset | Metrics |
|
|
116
|
+
|---|---|
|
|
117
|
+
| `image` | `Q_MLI`, `Q_En`, `SF`, `AG_VIFB` |
|
|
118
|
+
| `information` | `Q_MI`, `Q_NCIE`, `Q_TE_Nava`, `Q_FMI` |
|
|
119
|
+
| `feature` | `Q_ABF`, `Q_M`, `Q_SF`, `Q_FSIM` |
|
|
120
|
+
| `similarity` | `Q_PSNR`, `Q_CC`, `Q_SCD`, `VIF_Sheikh`, `Q_VIFF` |
|
|
121
|
+
| `all` | all 18 |
|
|
122
|
+
|
|
123
|
+
## 💻 Command line
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
# one fused image
|
|
127
|
+
fusion-metrics MRI.png PET.png fused.png --preset feature
|
|
128
|
+
|
|
129
|
+
# a whole method: files matched by name across three folders -> per-image CSV + mean
|
|
130
|
+
fusion-metrics --dirs data/MRI data/PET results/MyMethod --preset all --csv mymethod.csv
|
|
131
|
+
|
|
132
|
+
fusion-metrics --list # metrics and presets
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Add `--json` for machine-readable output, or `--reader cv` to use the OpenCV gray conversion instead of MATLAB's.
|
|
136
|
+
|
|
137
|
+
## ⚠️ Conventions that change the numbers
|
|
138
|
+
|
|
139
|
+
- **Gray conversion.** `read_gray` reproduces MATLAB `imread` + `rgb2gray` (0.298936, 0.587043, 0.114021, rounded) —
|
|
140
|
+
use it to match MATLAB. `read_gray_cv` uses OpenCV's 0.299 / 0.587 / 0.114, common in Python code bases. Loading with
|
|
141
|
+
`cv2.imread(path, 0)` directly gives yet another result.
|
|
142
|
+
- **Data type matters for `Q_MI`.** On `uint8` input MATLAB saturates `255 + 1` to 255, merging the top gray level into
|
|
143
|
+
the previous histogram bin. The port reproduces this from the array's dtype — read images as `uint8`.
|
|
144
|
+
- **Edge maps for `Q_FMI`.** MATLAB's `edge()` thins edges inside a compiled mex file. The port reproduces its rule
|
|
145
|
+
(non-maximum suppression with `<=` towards the left/upper and `<` towards the right/lower neighbour), which matches
|
|
146
|
+
values computed in MATLAB.
|
|
147
|
+
- **Summation order.** `Q_FMI` branches on exact zero tests, so its sums follow MATLAB's left-to-right order;
|
|
148
|
+
NumPy's pairwise summation would shift results by up to 5e-3.
|
|
149
|
+
|
|
150
|
+
## ✅ Verification
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
python verify/selftest.py # ~20 s, prints "ALL OK"; no MATLAB/Octave needed
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
The self-test scores 7 seeded synthetic cases chosen to hit every code path — structured, noise, saturated 0/255,
|
|
157
|
+
low range [0, 40], {0, 1} images, odd size 255×257, and 512×512 — and compares the Python values with those produced by
|
|
158
|
+
the original `.m` files in GNU Octave:
|
|
159
|
+
|
|
160
|
+
- **17/17 metrics agree** with relative error ≤ 1.7e-12 (most ≈ 1e-15), and `edge_sobel` reproduces the stored edge
|
|
161
|
+
maps pixel for pixel.
|
|
162
|
+
- One documented exception: `Q_VIFF` on the *saturated* case differs by 2.7e-5. Its perfectly flat regions put local
|
|
163
|
+
variances at round-off level, right at the `.m` file's 1e-10 thresholds, where the result depends on the summation
|
|
164
|
+
order inside `filter2` (which differs between MATLAB, Octave and SciPy). On real images `Q_VIFF` agrees to 1e-14.
|
|
165
|
+
|
|
166
|
+
A cross-check on real PET/SPECT–MRI pairs from the public Harvard Whole Brain Atlas gave the same picture: every metric
|
|
167
|
+
within 4e-12 of the `.m` files on identical pixel arrays.
|
|
168
|
+
|
|
169
|
+
<details>
|
|
170
|
+
<summary><b>Regenerate the reference values (needs GNU Octave)</b></summary>
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
conda create -p ./octave -c conda-forge octave octave-image octave-signal
|
|
174
|
+
cd verify
|
|
175
|
+
python make_reference_cases.py && octave-cli run_reference.m \
|
|
176
|
+
&& python selftest.py --export && python selftest.py
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
`verify/octave_shims/` supplies `dwt2` (Haar, MATLAB's default `'sym'` mode — Octave has no Wavelet Toolbox) and
|
|
180
|
+
`imgradientxy` with MATLAB's border convention. They are only used when running the `.m` files in Octave.
|
|
181
|
+
|
|
182
|
+
</details>
|
|
183
|
+
|
|
184
|
+
## 🗂️ Repository layout
|
|
185
|
+
|
|
186
|
+
```
|
|
187
|
+
fusion-metrics/
|
|
188
|
+
├── fusion_metrics/ # the Python package (+ CLI in cli.py)
|
|
189
|
+
├── matlab/ # reference .m implementations
|
|
190
|
+
├── matlab_added/ # MATLAB references for AG_VIFB and VIF_Sheikh
|
|
191
|
+
├── verify/ # self-test, reference cases/values, Octave scripts and shims
|
|
192
|
+
├── examples/ # minimal API example
|
|
193
|
+
└── assets/ # README figures
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
## 🧭 Roadmap
|
|
197
|
+
|
|
198
|
+
The `matlab/` folder holds 15 more reference metrics without a Python port yet: `Q_C`, `Q_CB`, `Q_CV`, `Q_CE`, `Q_CI`,
|
|
199
|
+
`Q_EI`, `Q_H`, `Q_NMI`, `Q_P`, `Q_S`, `Q_TE`, `Q_UIQI`, `Q_Y`, `Q_NABF`, `MS_SSIM`. Contributions are welcome —
|
|
200
|
+
a port is accepted once it passes the self-test against its `.m` file.
|
|
201
|
+
|
|
202
|
+
## 📝 Citation
|
|
203
|
+
|
|
204
|
+
If `fusion-metrics` helps your work, please cite it (GitHub's **“Cite this repository”** button gives the same entry)
|
|
205
|
+
**and** the original paper of each metric you report:
|
|
206
|
+
|
|
207
|
+
```bibtex
|
|
208
|
+
@software{vu2026fusionmetrics,
|
|
209
|
+
author = {Vu, Viet Anh},
|
|
210
|
+
title = {fusion-metrics: MATLAB-faithful objective quality metrics for image fusion},
|
|
211
|
+
year = {2026},
|
|
212
|
+
version = {1.0.0},
|
|
213
|
+
url = {https://github.com/VuVietAnh2309/fusion-metrics}
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## 👤 Author
|
|
218
|
+
|
|
219
|
+
**Vu Viet Anh** (Vũ Việt Anh) — author and maintainer of the Python package, CLI and verification suite.<br/>
|
|
220
|
+
AI Engineer & MSc student in Computer Science, Hanoi University of Science and Technology (HUST).
|
|
221
|
+
|
|
222
|
+
[](mailto:vietanhresearcher@gmail.com)
|
|
223
|
+
[](https://orcid.org/0009-0006-6977-5922)
|
|
224
|
+
[](https://vuvietanh2309.github.io/)
|
|
225
|
+
[](https://github.com/VuVietAnh2309)
|
|
226
|
+
|
|
227
|
+
## 📄 License
|
|
228
|
+
|
|
229
|
+
The package is MIT-licensed. `Q_FSIM`, `Q_VIFF` and `VIF_Sheikh` are translations of third-party code that remains
|
|
230
|
+
under its authors' terms (educational and research use only) — see [NOTICE](https://github.com/VuVietAnh2309/fusion-metrics/blob/main/NOTICE). Reference `.m` files keep their
|
|
231
|
+
original copyright headers.
|
|
232
|
+
|
|
233
|
+
The `.m` files in `matlab/` are the reference implementations used for verification; files derived from published
|
|
234
|
+
toolboxes keep their original headers.
|
|
235
|
+
|
|
236
|
+
Questions, bug reports and new metric ports: [GitHub Issues](https://github.com/VuVietAnh2309/fusion-metrics/issues).
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# fusion-metrics
|
|
4
|
+
|
|
5
|
+
**Objective quality metrics for image fusion — ported line by line from MATLAB and verified to round-off precision.**
|
|
6
|
+
|
|
7
|
+
Developed by **[Vu Viet Anh](https://vuvietanh2309.github.io/)** (Vũ Việt Anh) · AI Engineer & Researcher, Hanoi University of Science and Technology
|
|
8
|
+
|
|
9
|
+
[](https://github.com/VuVietAnh2309/fusion-metrics/actions/workflows/test.yml)
|
|
10
|
+
[](https://github.com/VuVietAnh2309/fusion-metrics/blob/main/LICENSE)
|
|
11
|
+

|
|
12
|
+

|
|
13
|
+

|
|
14
|
+
|
|
15
|
+
[Quick start](#-quick-start) · [Metrics](#-metrics) · [CLI](#-command-line) · [Verification](#-verification) · [Citation](#-citation) · [Tiếng Việt](https://github.com/VuVietAnh2309/fusion-metrics/blob/main/README.vi.md)
|
|
16
|
+
|
|
17
|
+
</div>
|
|
18
|
+
|
|
19
|
+
<picture>
|
|
20
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/VuVietAnh2309/fusion-metrics/main/assets/verification-dark.svg">
|
|
21
|
+
<img alt="Verification pipeline: test images are scored by the original MATLAB files and by the Python port; values agree to within 1e-12" src="https://raw.githubusercontent.com/VuVietAnh2309/fusion-metrics/main/assets/verification.svg">
|
|
22
|
+
</picture>
|
|
23
|
+
|
|
24
|
+
## Why
|
|
25
|
+
|
|
26
|
+
Image-fusion papers report Q<sup>AB/F</sup>, MI, NCIE, FMI, VIF… but compute them with scattered MATLAB scripts and
|
|
27
|
+
ad-hoc Python re-implementations that quietly disagree: a different gray conversion, a one-pixel shift in
|
|
28
|
+
`conv2(...,'same')`, `uint8` saturation in a histogram, a different edge-thinning rule. The same method can then
|
|
29
|
+
score differently from paper to paper.
|
|
30
|
+
|
|
31
|
+
`fusion-metrics` gives one reference implementation that anyone can `pip install`:
|
|
32
|
+
|
|
33
|
+
- **MATLAB-faithful** — each function is a line-by-line port of a reference `.m` file, keeping its name, argument
|
|
34
|
+
order and numerical quirks.
|
|
35
|
+
- **Verified, not assumed** — a self-test compares every metric with values produced by the original `.m` files;
|
|
36
|
+
agreement is at round-off level (≤ 1e-12). It runs in CI on Python 3.10–3.13.
|
|
37
|
+
- **Ready for benchmarks** — score one image from Python, or a whole method folder from the command line to CSV.
|
|
38
|
+
|
|
39
|
+
## 🚀 Quick start
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
pip install git+https://github.com/VuVietAnh2309/fusion-metrics.git
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
import fusion_metrics as fm
|
|
47
|
+
|
|
48
|
+
A = fm.read_gray("MRI.png") # MATLAB imread + rgb2gray -> uint8
|
|
49
|
+
B = fm.read_gray("PET.png") # pseudo-colour images are converted to gray
|
|
50
|
+
F = fm.read_gray("fused.png")
|
|
51
|
+
|
|
52
|
+
fm.Q_ABF(A, B, F) # one metric
|
|
53
|
+
fm.evaluate(A, B, F, "information") # a preset -> {"Q_MI": ..., "Q_NCIE": ..., ...}
|
|
54
|
+
fm.evaluate(A, B, F) # all 18 metrics
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## 📐 Metrics
|
|
58
|
+
|
|
59
|
+
`Q_X(A, B, F)` takes the two sources and the fused image; no-reference metrics take only `F`.
|
|
60
|
+
Inputs are grayscale `uint8` or `double` in [0, 255].
|
|
61
|
+
|
|
62
|
+
| Function | Metric | Original reference |
|
|
63
|
+
|---|---|---|
|
|
64
|
+
| `Q_MLI(F)` | Mean local intensity, [0, 1] | — |
|
|
65
|
+
| `Q_En(F)` | Entropy | Roberts et al., *J. Appl. Remote Sens.* 2008 |
|
|
66
|
+
| `SF(F)` | Spatial frequency (4 directions) | Eskicioglu & Fisher, *IEEE TCOM* 1995 |
|
|
67
|
+
| `Q_AG(F)` | Average gradient (forward differences, [0, 1]) | — |
|
|
68
|
+
| `AG_VIFB(F)` | Average gradient, VIFB definition (0–255) | Zhang et al., VIFB, *CVPRW* 2020 |
|
|
69
|
+
| `Q_MI(A,B,F)` | Mutual information MI(A,F) + MI(B,F), bits | Qu et al., *Electron. Lett.* 2002 |
|
|
70
|
+
| `Q_NCIE(A,B,F)` | Nonlinear correlation information entropy | Wang, Shen & Jin, 2008 |
|
|
71
|
+
| `Q_TE_Nava(A,B,F)` | Tsallis entropy, q = 0.43137 | Nava et al., *Proc. SPIE* 2007 |
|
|
72
|
+
| `Q_FMI(A,B,F)` | Feature mutual information on edge maps, w = 3 | Haghighat et al., *Comput. Electr. Eng.* 2011 |
|
|
73
|
+
| `Q_ABF(A,B,F)` | Edge-based Q<sup>AB/F</sup> | Xydeas & Petrović, *Electron. Lett.* 2000 |
|
|
74
|
+
| `Q_M(A,B,F)` | Multiscale edge preservation (Haar, 2 levels) | Wang & Liu, *ICSP* 2008 |
|
|
75
|
+
| `Q_SF(A,B,F)` | Relative spatial-frequency error | Zheng et al., *Inf. Fusion* 2007 |
|
|
76
|
+
| `Q_FSIM(A,B,F)` | Feature similarity, (FSIM(A,F) + FSIM(B,F)) / 2 | Zhang et al., *IEEE TIP* 2011 |
|
|
77
|
+
| `Q_PSNR(A,B,F)` | Mean of PSNR(F,A) and PSNR(F,B) | — |
|
|
78
|
+
| `Q_CC(A,B,F)` | Mean correlation coefficient | — |
|
|
79
|
+
| `Q_SCD(A,B,F)` | Sum of the correlations of differences | Aslantas & Bendes, *AEU* 2015 |
|
|
80
|
+
| `VIF_Sheikh(A,B,F)` | VIF(A,F) + VIF(B,F) | Sheikh & Bovik, *IEEE TIP* 2006 |
|
|
81
|
+
| `Q_VIFF(A,B,F)` | Visual information fidelity for fusion | Han et al., *Inf. Fusion* 2013 |
|
|
82
|
+
|
|
83
|
+
The taxonomy follows Liu et al., *“Objective assessment of multiresolution image fusion algorithms for context
|
|
84
|
+
enhancement in night vision”*, IEEE TPAMI 2012. **Please cite the original paper of every metric you report.**
|
|
85
|
+
|
|
86
|
+
> [!NOTE]
|
|
87
|
+
> Two pairs look alike but are different metrics: `Q_AG` vs `AG_VIFB` (different differences and scale), and
|
|
88
|
+
> `Q_VIFF` (Han et al.) vs `VIF_Sheikh` (Sheikh & Bovik). State which one you use.
|
|
89
|
+
|
|
90
|
+
### Presets
|
|
91
|
+
|
|
92
|
+
| Preset | Metrics |
|
|
93
|
+
|---|---|
|
|
94
|
+
| `image` | `Q_MLI`, `Q_En`, `SF`, `AG_VIFB` |
|
|
95
|
+
| `information` | `Q_MI`, `Q_NCIE`, `Q_TE_Nava`, `Q_FMI` |
|
|
96
|
+
| `feature` | `Q_ABF`, `Q_M`, `Q_SF`, `Q_FSIM` |
|
|
97
|
+
| `similarity` | `Q_PSNR`, `Q_CC`, `Q_SCD`, `VIF_Sheikh`, `Q_VIFF` |
|
|
98
|
+
| `all` | all 18 |
|
|
99
|
+
|
|
100
|
+
## 💻 Command line
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
# one fused image
|
|
104
|
+
fusion-metrics MRI.png PET.png fused.png --preset feature
|
|
105
|
+
|
|
106
|
+
# a whole method: files matched by name across three folders -> per-image CSV + mean
|
|
107
|
+
fusion-metrics --dirs data/MRI data/PET results/MyMethod --preset all --csv mymethod.csv
|
|
108
|
+
|
|
109
|
+
fusion-metrics --list # metrics and presets
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Add `--json` for machine-readable output, or `--reader cv` to use the OpenCV gray conversion instead of MATLAB's.
|
|
113
|
+
|
|
114
|
+
## ⚠️ Conventions that change the numbers
|
|
115
|
+
|
|
116
|
+
- **Gray conversion.** `read_gray` reproduces MATLAB `imread` + `rgb2gray` (0.298936, 0.587043, 0.114021, rounded) —
|
|
117
|
+
use it to match MATLAB. `read_gray_cv` uses OpenCV's 0.299 / 0.587 / 0.114, common in Python code bases. Loading with
|
|
118
|
+
`cv2.imread(path, 0)` directly gives yet another result.
|
|
119
|
+
- **Data type matters for `Q_MI`.** On `uint8` input MATLAB saturates `255 + 1` to 255, merging the top gray level into
|
|
120
|
+
the previous histogram bin. The port reproduces this from the array's dtype — read images as `uint8`.
|
|
121
|
+
- **Edge maps for `Q_FMI`.** MATLAB's `edge()` thins edges inside a compiled mex file. The port reproduces its rule
|
|
122
|
+
(non-maximum suppression with `<=` towards the left/upper and `<` towards the right/lower neighbour), which matches
|
|
123
|
+
values computed in MATLAB.
|
|
124
|
+
- **Summation order.** `Q_FMI` branches on exact zero tests, so its sums follow MATLAB's left-to-right order;
|
|
125
|
+
NumPy's pairwise summation would shift results by up to 5e-3.
|
|
126
|
+
|
|
127
|
+
## ✅ Verification
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
python verify/selftest.py # ~20 s, prints "ALL OK"; no MATLAB/Octave needed
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
The self-test scores 7 seeded synthetic cases chosen to hit every code path — structured, noise, saturated 0/255,
|
|
134
|
+
low range [0, 40], {0, 1} images, odd size 255×257, and 512×512 — and compares the Python values with those produced by
|
|
135
|
+
the original `.m` files in GNU Octave:
|
|
136
|
+
|
|
137
|
+
- **17/17 metrics agree** with relative error ≤ 1.7e-12 (most ≈ 1e-15), and `edge_sobel` reproduces the stored edge
|
|
138
|
+
maps pixel for pixel.
|
|
139
|
+
- One documented exception: `Q_VIFF` on the *saturated* case differs by 2.7e-5. Its perfectly flat regions put local
|
|
140
|
+
variances at round-off level, right at the `.m` file's 1e-10 thresholds, where the result depends on the summation
|
|
141
|
+
order inside `filter2` (which differs between MATLAB, Octave and SciPy). On real images `Q_VIFF` agrees to 1e-14.
|
|
142
|
+
|
|
143
|
+
A cross-check on real PET/SPECT–MRI pairs from the public Harvard Whole Brain Atlas gave the same picture: every metric
|
|
144
|
+
within 4e-12 of the `.m` files on identical pixel arrays.
|
|
145
|
+
|
|
146
|
+
<details>
|
|
147
|
+
<summary><b>Regenerate the reference values (needs GNU Octave)</b></summary>
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
conda create -p ./octave -c conda-forge octave octave-image octave-signal
|
|
151
|
+
cd verify
|
|
152
|
+
python make_reference_cases.py && octave-cli run_reference.m \
|
|
153
|
+
&& python selftest.py --export && python selftest.py
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
`verify/octave_shims/` supplies `dwt2` (Haar, MATLAB's default `'sym'` mode — Octave has no Wavelet Toolbox) and
|
|
157
|
+
`imgradientxy` with MATLAB's border convention. They are only used when running the `.m` files in Octave.
|
|
158
|
+
|
|
159
|
+
</details>
|
|
160
|
+
|
|
161
|
+
## 🗂️ Repository layout
|
|
162
|
+
|
|
163
|
+
```
|
|
164
|
+
fusion-metrics/
|
|
165
|
+
├── fusion_metrics/ # the Python package (+ CLI in cli.py)
|
|
166
|
+
├── matlab/ # reference .m implementations
|
|
167
|
+
├── matlab_added/ # MATLAB references for AG_VIFB and VIF_Sheikh
|
|
168
|
+
├── verify/ # self-test, reference cases/values, Octave scripts and shims
|
|
169
|
+
├── examples/ # minimal API example
|
|
170
|
+
└── assets/ # README figures
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## 🧭 Roadmap
|
|
174
|
+
|
|
175
|
+
The `matlab/` folder holds 15 more reference metrics without a Python port yet: `Q_C`, `Q_CB`, `Q_CV`, `Q_CE`, `Q_CI`,
|
|
176
|
+
`Q_EI`, `Q_H`, `Q_NMI`, `Q_P`, `Q_S`, `Q_TE`, `Q_UIQI`, `Q_Y`, `Q_NABF`, `MS_SSIM`. Contributions are welcome —
|
|
177
|
+
a port is accepted once it passes the self-test against its `.m` file.
|
|
178
|
+
|
|
179
|
+
## 📝 Citation
|
|
180
|
+
|
|
181
|
+
If `fusion-metrics` helps your work, please cite it (GitHub's **“Cite this repository”** button gives the same entry)
|
|
182
|
+
**and** the original paper of each metric you report:
|
|
183
|
+
|
|
184
|
+
```bibtex
|
|
185
|
+
@software{vu2026fusionmetrics,
|
|
186
|
+
author = {Vu, Viet Anh},
|
|
187
|
+
title = {fusion-metrics: MATLAB-faithful objective quality metrics for image fusion},
|
|
188
|
+
year = {2026},
|
|
189
|
+
version = {1.0.0},
|
|
190
|
+
url = {https://github.com/VuVietAnh2309/fusion-metrics}
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## 👤 Author
|
|
195
|
+
|
|
196
|
+
**Vu Viet Anh** (Vũ Việt Anh) — author and maintainer of the Python package, CLI and verification suite.<br/>
|
|
197
|
+
AI Engineer & MSc student in Computer Science, Hanoi University of Science and Technology (HUST).
|
|
198
|
+
|
|
199
|
+
[](mailto:vietanhresearcher@gmail.com)
|
|
200
|
+
[](https://orcid.org/0009-0006-6977-5922)
|
|
201
|
+
[](https://vuvietanh2309.github.io/)
|
|
202
|
+
[](https://github.com/VuVietAnh2309)
|
|
203
|
+
|
|
204
|
+
## 📄 License
|
|
205
|
+
|
|
206
|
+
The package is MIT-licensed. `Q_FSIM`, `Q_VIFF` and `VIF_Sheikh` are translations of third-party code that remains
|
|
207
|
+
under its authors' terms (educational and research use only) — see [NOTICE](https://github.com/VuVietAnh2309/fusion-metrics/blob/main/NOTICE). Reference `.m` files keep their
|
|
208
|
+
original copyright headers.
|
|
209
|
+
|
|
210
|
+
The `.m` files in `matlab/` are the reference implementations used for verification; files derived from published
|
|
211
|
+
toolboxes keep their original headers.
|
|
212
|
+
|
|
213
|
+
Questions, bug reports and new metric ports: [GitHub Issues](https://github.com/VuVietAnh2309/fusion-metrics/issues).
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# fusion-metrics — Copyright (c) 2026 Vu Viet Anh (Vũ Việt Anh) <vietanhresearcher@gmail.com>
|
|
2
|
+
# MIT License (third-party-derived metrics: see NOTICE). https://github.com/VuVietAnh2309/fusion-metrics
|
|
3
|
+
"""fusion_metrics: objective quality metrics for image fusion, faithfully ported from MATLAB.
|
|
4
|
+
|
|
5
|
+
Every function keeps the name and argument order of its reference .m file (../matlab/*.m):
|
|
6
|
+
Q_X(A, B, F) with A, B the two source images and F the fused image (grayscale, uint8 or double
|
|
7
|
+
in [0, 255]). No-reference metrics take only F.
|
|
8
|
+
|
|
9
|
+
Read images with `read_gray` (= MATLAB imread + rgb2gray) to get exactly the numbers the
|
|
10
|
+
MATLAB reference implementations produce.
|
|
11
|
+
"""
|
|
12
|
+
from ._matlab import read_gray, read_gray_cv, rgb2gray
|
|
13
|
+
from .basic import Q_MLI, Q_AG, Q_En, SF, Q_SF, Q_PSNR, Q_CC, Q_SCD, Q_MI
|
|
14
|
+
from .info import Q_ABF, Q_VIFF, Q_M, Q_NCIE, Q_TE_Nava
|
|
15
|
+
from .fsim import Q_FSIM
|
|
16
|
+
from .fmi import Q_FMI, edge_sobel
|
|
17
|
+
from .variants import AG_VIFB, VIF_Sheikh
|
|
18
|
+
|
|
19
|
+
__version__ = "1.0.0"
|
|
20
|
+
__author__ = "Vu Viet Anh (Vũ Việt Anh)"
|
|
21
|
+
__email__ = "vietanhresearcher@gmail.com"
|
|
22
|
+
|
|
23
|
+
# name -> (function, needs source images)
|
|
24
|
+
METRICS = {
|
|
25
|
+
"Q_MLI": (Q_MLI, False), "Q_AG": (Q_AG, False), "Q_En": (Q_En, False), "SF": (SF, False),
|
|
26
|
+
"Q_SF": (Q_SF, True), "Q_PSNR": (Q_PSNR, True), "Q_CC": (Q_CC, True), "Q_SCD": (Q_SCD, True),
|
|
27
|
+
"Q_MI": (Q_MI, True), "Q_ABF": (Q_ABF, True), "Q_VIFF": (Q_VIFF, True), "Q_M": (Q_M, True),
|
|
28
|
+
"Q_NCIE": (Q_NCIE, True), "Q_TE_Nava": (Q_TE_Nava, True), "Q_FSIM": (Q_FSIM, True),
|
|
29
|
+
"Q_FMI": (Q_FMI, True),
|
|
30
|
+
# widely used alternative definitions; MATLAB references in ../matlab_added/
|
|
31
|
+
"AG_VIFB": (AG_VIFB, False), "VIF_Sheikh": (VIF_Sheikh, True),
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
# Ready-made metric groups (Liu et al., TPAMI 2012 taxonomy, plus image statistics).
|
|
35
|
+
PRESETS = {
|
|
36
|
+
"image": ["Q_MLI", "Q_En", "SF", "AG_VIFB"], # statistics of F alone
|
|
37
|
+
"information": ["Q_MI", "Q_NCIE", "Q_TE_Nava", "Q_FMI"], # information theory
|
|
38
|
+
"feature": ["Q_ABF", "Q_M", "Q_SF", "Q_FSIM"], # edges / features
|
|
39
|
+
"similarity": ["Q_PSNR", "Q_CC", "Q_SCD", "VIF_Sheikh", "Q_VIFF"], # similarity to sources
|
|
40
|
+
}
|
|
41
|
+
PRESETS["all"] = list(METRICS)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def evaluate(A, B, F, names=None):
|
|
45
|
+
"""Compute the requested metrics (a list of names or a PRESETS key; default: all)."""
|
|
46
|
+
if isinstance(names, str):
|
|
47
|
+
names = PRESETS[names]
|
|
48
|
+
names = names or list(METRICS)
|
|
49
|
+
out = {}
|
|
50
|
+
for n in names:
|
|
51
|
+
fn, src = METRICS[n]
|
|
52
|
+
out[n] = fn(A, B, F) if src else fn(F)
|
|
53
|
+
return out
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def evaluate_files(path_a, path_b, path_f, names=None, reader=None):
|
|
57
|
+
"""Same as evaluate() but reading the three files.
|
|
58
|
+
|
|
59
|
+
reader=read_gray (default) follows MATLAB (imread + rgb2gray); reader=read_gray_cv uses the
|
|
60
|
+
OpenCV gray conversion common in Python fusion code bases."""
|
|
61
|
+
reader = reader or read_gray
|
|
62
|
+
return evaluate(reader(path_a), reader(path_b), reader(path_f), names)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
read_gray_paper = read_gray_cv # backwards-compatible alias
|
|
66
|
+
|
|
67
|
+
__all__ = list(METRICS) + ["METRICS", "PRESETS", "evaluate", "evaluate_files", "read_gray",
|
|
68
|
+
"read_gray_cv", "rgb2gray", "edge_sobel", "__version__"]
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# fusion-metrics — Copyright (c) 2026 Vu Viet Anh (Vũ Việt Anh) <vietanhresearcher@gmail.com>
|
|
2
|
+
# MIT License (third-party-derived metrics: see NOTICE). https://github.com/VuVietAnh2309/fusion-metrics
|
|
3
|
+
"""Small helpers that reproduce MATLAB built-ins exactly (as used by the .m metrics)."""
|
|
4
|
+
import cv2
|
|
5
|
+
import numpy as np
|
|
6
|
+
from scipy.signal import convolve2d, correlate2d
|
|
7
|
+
|
|
8
|
+
EPS = np.finfo(np.float64).eps # MATLAB eps
|
|
9
|
+
RGB2GRAY = (0.298936021293775, 0.587043074451121, 0.114020904255103) # MATLAB rgb2gray
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def mround(x):
|
|
13
|
+
"""MATLAB round: halves away from zero (numpy rounds halves to even)."""
|
|
14
|
+
x = np.asarray(x, dtype=np.float64)
|
|
15
|
+
return np.sign(x) * np.floor(np.abs(x) + 0.5)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def to_uint8(x):
|
|
19
|
+
"""MATLAB uint8(x): round half away from zero, saturate to [0, 255]."""
|
|
20
|
+
return np.clip(mround(x), 0, 255).astype(np.uint8)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def rgb2gray(rgb):
|
|
24
|
+
"""MATLAB rgb2gray for a uint8 RGB array (H, W, 3) in R, G, B order -> uint8."""
|
|
25
|
+
rgb = rgb.astype(np.float64)
|
|
26
|
+
y = RGB2GRAY[0] * rgb[..., 0] + RGB2GRAY[1] * rgb[..., 1] + RGB2GRAY[2] * rgb[..., 2]
|
|
27
|
+
return to_uint8(y)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def read_gray(path):
|
|
31
|
+
"""MATLAB imread + rgb2gray, the input convention of the reference .m implementations.
|
|
32
|
+
|
|
33
|
+
A single-channel file is returned unchanged; a 3-channel file (pseudo-colour PET/SPECT or a
|
|
34
|
+
colour fused image) is converted with MATLAB rgb2gray. Returns uint8 (H, W).
|
|
35
|
+
"""
|
|
36
|
+
im = cv2.imread(path, cv2.IMREAD_UNCHANGED)
|
|
37
|
+
if im is None:
|
|
38
|
+
raise FileNotFoundError(path)
|
|
39
|
+
if im.ndim == 2:
|
|
40
|
+
return im.astype(np.uint8)
|
|
41
|
+
if im.shape[2] == 4:
|
|
42
|
+
im = im[..., :3]
|
|
43
|
+
return rgb2gray(im[..., ::-1]) # OpenCV is BGR
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def read_gray_cv(path):
|
|
47
|
+
"""OpenCV convention common in Python fusion code: cv2 BGR2GRAY (0.299, 0.587, 0.114,
|
|
48
|
+
rounded) scaled to [0,1], then x*255 truncated to uint8. Use it to match numbers produced by
|
|
49
|
+
such code; use read_gray() to match the MATLAB reference implementations."""
|
|
50
|
+
im = cv2.imread(path, cv2.IMREAD_UNCHANGED)
|
|
51
|
+
if im is None:
|
|
52
|
+
raise FileNotFoundError(path)
|
|
53
|
+
if im.ndim == 3:
|
|
54
|
+
im = cv2.cvtColor(im[..., :3], cv2.COLOR_BGR2GRAY)
|
|
55
|
+
x = im.astype(np.float64) / 255.0
|
|
56
|
+
return np.clip(x * 255.0, 0, 255).astype(np.uint8)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def conv2_same(x, k):
|
|
60
|
+
"""MATLAB conv2(x, k, 'same'): the central part of the full convolution, starting at index
|
|
61
|
+
floor(size(k)/2) in each dimension (scipy's mode='same' differs by one pixel for even kernels)."""
|
|
62
|
+
k = np.asarray(k, dtype=np.float64)
|
|
63
|
+
full = convolve2d(x, k, mode="full")
|
|
64
|
+
r0, c0 = k.shape[0] // 2, k.shape[1] // 2
|
|
65
|
+
return full[r0:r0 + x.shape[0], c0:c0 + x.shape[1]]
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def filter2_valid(k, x):
|
|
69
|
+
"""MATLAB filter2(k, x, 'valid') (correlation)."""
|
|
70
|
+
return correlate2d(x, k, mode="valid")
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def fspecial_gaussian(n, sigma):
|
|
74
|
+
"""MATLAB fspecial('gaussian', n, sigma) for a square odd/even size n."""
|
|
75
|
+
siz = (n - 1) / 2.0
|
|
76
|
+
y, x = np.meshgrid(np.arange(-siz, siz + 1), np.arange(-siz, siz + 1), indexing="ij")
|
|
77
|
+
h = np.exp(-(x * x + y * y) / (2.0 * sigma * sigma))
|
|
78
|
+
h[h < EPS * h.max()] = 0
|
|
79
|
+
s = h.sum()
|
|
80
|
+
return h / s if s != 0 else h
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def as_double(x):
|
|
84
|
+
return np.asarray(x).astype(np.float64)
|