Compare commits
2 Commits
428790ab91
...
be1e3627e7
Author | SHA1 | Date | |
---|---|---|---|
be1e3627e7 | |||
d139f5afcf |
74
image_fusion/evaluate_module/evaluate.py
Normal file
74
image_fusion/evaluate_module/evaluate.py
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
import numpy as np
|
||||||
|
import cv2
|
||||||
|
from skimage.metrics import structural_similarity as ssim
|
||||||
|
from skimage.filters import sobel
|
||||||
|
from sklearn.metrics import mutual_info_score
|
||||||
|
|
||||||
|
|
||||||
|
# Helper to compute mutual information between two grayscale images
|
||||||
|
def evaluate_mutual_information(img1_gray, img2_gray):
|
||||||
|
hist_2d, _, _ = np.histogram2d(img1_gray.ravel(), img2_gray.ravel(), bins=256)
|
||||||
|
pxy = hist_2d / float(np.sum(hist_2d))
|
||||||
|
px = np.sum(pxy, axis=1)
|
||||||
|
py = np.sum(pxy, axis=0)
|
||||||
|
px_py = np.outer(px, py)
|
||||||
|
nzs = pxy > 0
|
||||||
|
mi = np.sum(pxy[nzs] * np.log(pxy[nzs] / px_py[nzs]))
|
||||||
|
return mi
|
||||||
|
|
||||||
|
|
||||||
|
# Compute SSIM between two grayscale images
|
||||||
|
def evaluate_registration_ssim(img1_gray, img2_gray):
|
||||||
|
return ssim(img1_gray, img2_gray)
|
||||||
|
|
||||||
|
|
||||||
|
# Entropy of grayscale image (fusion quality)
|
||||||
|
def evaluate_fusion_entropy(fusion_img):
|
||||||
|
gray = cv2.cvtColor(fusion_img, cv2.COLOR_RGB2GRAY)
|
||||||
|
hist = cv2.calcHist([gray], [0], None, [256], [0, 256])
|
||||||
|
hist = hist.ravel() / hist.sum()
|
||||||
|
entropy = -np.sum(hist * np.log2(hist + 1e-9))
|
||||||
|
return entropy
|
||||||
|
|
||||||
|
|
||||||
|
# Edge strength using Sobel (fusion quality)
|
||||||
|
def evaluate_fusion_edges(fusion_img):
|
||||||
|
gray = cv2.cvtColor(fusion_img, cv2.COLOR_RGB2GRAY)
|
||||||
|
edges = sobel(gray.astype(float) / 255.0)
|
||||||
|
return np.mean(edges)
|
||||||
|
|
||||||
|
|
||||||
|
# SSIM between fused image and one of the sources
|
||||||
|
def evaluate_fusion_ssim(fusion_img, reference_img):
|
||||||
|
fusion_gray = cv2.cvtColor(fusion_img, cv2.COLOR_RGB2GRAY)
|
||||||
|
ref_gray = cv2.cvtColor(reference_img, cv2.COLOR_RGB2GRAY)
|
||||||
|
return ssim(fusion_gray, ref_gray)
|
||||||
|
|
||||||
|
|
||||||
|
# Return all in one place (stub images would be required to test)
|
||||||
|
def summarize_evaluation(img1_gray, img2_gray, fusion_img, ref_img_for_ssim):
|
||||||
|
return {
|
||||||
|
"Registration SSIM": evaluate_registration_ssim(img1_gray, img2_gray),
|
||||||
|
"Mutual Information": evaluate_mutual_information(img1_gray, img2_gray),
|
||||||
|
"Fusion Entropy": evaluate_fusion_entropy(fusion_img),
|
||||||
|
"Fusion Edge Strength": evaluate_fusion_edges(fusion_img),
|
||||||
|
"Fusion SSIM (vs Ref)": evaluate_fusion_ssim(fusion_img, ref_img_for_ssim),
|
||||||
|
}
|
||||||
|
|
||||||
|
# 将所有评价封装成一个高层函数 evaluate_all
|
||||||
|
def evaluate_all(img1_gray, img2_gray, fusion_img, ref_img_for_ssim, verbose=True):
|
||||||
|
"""
|
||||||
|
评估图像配准和融合质量的通用函数
|
||||||
|
:param img1_gray: 可见光灰度图像(原图)
|
||||||
|
:param img2_gray: 红外灰度图像(配准后)
|
||||||
|
:param fusion_img: 融合图像(RGB)
|
||||||
|
:param ref_img_for_ssim: 可见光RGB图,用于对比SSIM
|
||||||
|
:param verbose: 是否打印结果
|
||||||
|
:return: dict 评价指标结果
|
||||||
|
"""
|
||||||
|
results = summarize_evaluation(img1_gray, img2_gray, fusion_img, ref_img_for_ssim)
|
||||||
|
if verbose:
|
||||||
|
print("图像评价指标如下:")
|
||||||
|
for k, v in results.items():
|
||||||
|
print(f"{k}: {v:.4f}")
|
||||||
|
return results
|
26
image_fusion/evaluate_module/evaluation_test.py
Normal file
26
image_fusion/evaluate_module/evaluation_test.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
from evaluate import *
|
||||||
|
|
||||||
|
# 创建模拟图像数据用于测试
|
||||||
|
# img1_gray:原始灰度图像(可见光)
|
||||||
|
# img2_gray:变换后的灰度图像(红外模拟)
|
||||||
|
# fusion_img:融合图像(可见光 + 红外)
|
||||||
|
# ref_img_for_ssim:参考图像(可见光RGB)
|
||||||
|
|
||||||
|
# 创建基础灰度图像(梯度)
|
||||||
|
img1_gray = np.tile(np.linspace(50, 200, 256).astype(np.uint8), (256, 1))
|
||||||
|
|
||||||
|
# 模拟配准后的图像:加一点噪声和平移
|
||||||
|
img2_gray = np.roll(img1_gray, shift=5, axis=1) # 平移模拟配准偏差
|
||||||
|
noise = np.random.normal(0, 5, img2_gray.shape).astype(np.uint8)
|
||||||
|
img2_gray = cv2.add(img2_gray, noise)
|
||||||
|
|
||||||
|
# 创建 RGB 可见光图(重复三个通道)
|
||||||
|
ref_img_for_ssim = cv2.merge([img1_gray] * 3)
|
||||||
|
|
||||||
|
# 创建融合图像(取两个灰度图平均后合并入RGB)
|
||||||
|
fusion_Y = cv2.addWeighted(img1_gray, 0.5, img2_gray, 0.5, 0)
|
||||||
|
fusion_img = cv2.merge([fusion_Y, img1_gray, img2_gray])
|
||||||
|
|
||||||
|
# 运行评价函数
|
||||||
|
scores = evaluate_all(img1_gray, img2_gray, fusion_img, ref_img_for_ssim)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user