Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests #53

Merged
merged 5 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions image_similarity_measures/quality_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ def rmse(org_img: np.ndarray, pred_img: np.ndarray, max_p: int = 4095) -> float:
org_img = np.expand_dims(org_img, axis=-1)

rmse_bands = []
for i in range(org_img.shape[2]):
dif = np.subtract(org_img[:, :, i], pred_img[:, :, i])
m = np.mean(np.square(dif / max_p))
s = np.sqrt(m)
rmse_bands.append(s)

diff = org_img - pred_img
mse_bands = np.mean(np.square(diff / max_p), axis=(0, 1))
rmse_bands = np.sqrt(mse_bands)
return np.mean(rmse_bands)


Expand All @@ -66,21 +63,19 @@ def psnr(org_img: np.ndarray, pred_img: np.ndarray, max_p: int = 4095) -> float:
if org_img.ndim == 2:
org_img = np.expand_dims(org_img, axis=-1)

mse_bands = []
for i in range(org_img.shape[2]):
mse_bands.append(np.mean(np.square(org_img[:, :, i] - pred_img[:, :, i])))

return 20 * np.log10(max_p) - 10.0 * np.log10(np.mean(mse_bands))
mse_bands = np.mean(np.square(org_img - pred_img), axis=(0, 1))
mse = np.mean(mse_bands)
return 20 * np.log10(max_p / np.sqrt(mse))


def _similarity_measure(x: np.array, y: np.array, constant: float):
"""
Calculate feature similarity measurement between two images
"""
numerator = 2 * x * y + constant
denominator = x**2 + y**2 + constant
numerator = 2 * np.multiply(x, y) + constant
denominator = np.add(np.square(x), np.square(y)) + constant

return numerator / denominator
return np.divide(numerator, denominator)


def _gradient_magnitude(img: np.ndarray, img_depth: int):
Expand Down
291 changes: 289 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ classifiers=[
"Intended Audience :: Science/Research",
"Development Status :: 5 - Production/Stable",
]
[tool.poetry.group.test.dependencies]
pytest = "^7.3.0"
rasterio = "^1.3.6"


[project.urls]
Source = 'https://github.com/up42/image-similarity-measures'
Expand Down
Loading