Code Monkey home page Code Monkey logo

sewar's Introduction

Buy Me A Coffee

Sewar

Downloads Build Status codecov PyPI version

Sewar is a python package for image quality assessment using different metrics. You can check documentation here.

Implemented metrics

  • Mean Squared Error (MSE)
  • Root Mean Squared Error (RMSE)
  • Peak Signal-to-Noise Ratio (PSNR) [1]
  • Structural Similarity Index (SSIM) [1]
  • Universal Quality Image Index (UQI) [2]
  • Multi-scale Structural Similarity Index (MS-SSIM) [3]
  • Erreur Relative Globale Adimensionnelle de Synthèse (ERGAS) [4]
  • Spatial Correlation Coefficient (SCC) [5]
  • Relative Average Spectral Error (RASE) [6]
  • Spectral Angle Mapper (SAM) [7]
  • Spectral Distortion Index (D_lambda) [8]
  • Spatial Distortion Index (D_S) [8]
  • Quality with No Reference (QNR) [8]
  • Visual Information Fidelity (VIF) [9]
  • Block Sensitive - Peak Signal-to-Noise Ratio (PSNR-B) [10]

Todo

  • Add command-line support for No-reference metrics

Installation

Just as simple as

pip install sewar

Example usage

a simple example to use UQI

>>> from sewar.full_ref import uqi
>>> uqi(img1,img2)
0.9586952304831419

Example usage for command line interface

sewar [metric] [GT path] [P path] (any extra parameters)

An example to use SSIM

foo@bar:~$ sewar ssim images/ground_truth.tif images/deformed.tif -ws 13
ssim : 0.8947009811410856

Available metrics list

mse, rmse, psnr, rmse_sw, uqi, ssim, ergas, scc, rase, sam, msssim, vifp, psnrb 

Contributors

Special thanks to @sachinpuranik99 and @sunwj.

References

[1] "Image quality assessment: from error visibility to structural similarity." 2004)
[2] "A universal image quality index." (2002)
[3] "Multiscale structural similarity for image quality assessment." (2003)
[4] "Quality of high resolution synthesised images: Is there a simple criterion?." (2000)
[5] "A wavelet transform method to merge Landsat TM and SPOT panchromatic data." (1998)
[6] "Fusion of multispectral and panchromatic images using improved IHS and PCA mergers based on wavelet decomposition." (2004)
[7] "Discrimination among semi-arid landscape endmembers using the spectral angle mapper (SAM) algorithm." (1992)
[8] "Multispectral and panchromatic data fusion assessment without reference." (2008)
[9] "Image information and visual quality." (2006)
[10] "Quality Assessment of Deblocked Images" (2011)

sewar's People

Contributors

andrewekhalel avatar magamig avatar sachinpuranik99 avatar sunwj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sewar's Issues

full_ref.msssim() and aspect ratio of images

Hi,

Thank you so much for writing and maintaining this...it's all good work for sure.

I'm finding that whenever I pass images with an aspect ratio greater than about 1.7 or so it always fails....
I'm using all the default params etc and the input sizes are are matching and not tiny etc ....
All your other routines work fine.

I have run thousands of images through it and reliably - the ones with aspect ratio below ~1.7 work fine.

For example, the run below the shapes match and are : (90, 1920, 3)

Traceback (most recent call last):
File "c:\Users\NHEL\T_code\Image_work\sewar_tests\side_tests\side_test_sewar testing for images_200_Whay_doesnt_MS-ssim_work_hmmm.py", line 221, in
res_ms_ssim = full_ref.msssim( TILE, lo_IMGS[ k ] ).real
File "C:\Users\NHEL\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sewar\full_ref.py", line 305, in msssim
_ssim, _cs = ssim(GT, P, ws=ws,K1=K1,K2=K2,MAX=MAX,fltr_specs=fltr_specs)
File "C:\Users\NHEL\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sewar\full_ref.py", line 157, in ssim
ssim,cs = _ssim_single(GT[:,:,i],P[:,:,i],ws,C1,C2,fltr_specs,mode)
File "C:\Users\NHEL\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sewar\full_ref.py", line 118, in _ssim_single
GT_sum_sq,P_sum_sq,GT_P_sum_mul = _get_sums(GT,P,win,mode)
File "C:\Users\NHEL\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sewar\utils.py", line 32, in _get_sums
mu1,mu2 = (filter2(GT,win,mode),filter2(P,win,mode))
File "C:\Users\NHEL\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sewar\utils.py", line 60, in filter2
return signal.convolve2d(img, np.rot90(fltr,2), mode=mode)
File "C:\Users\NHEL\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\scipy\signal_signaltools.py", line 1698, in convolve2d
if _inputs_swap_needed(mode, in1.shape, in2.shape):
File "C:\Users\NHEL\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\scipy\signal_signaltools.py", line 84, in _inputs_swap_needed
raise ValueError("For 'valid' mode, one must be at least "
ValueError: For 'valid' mode, one must be at least as large as the other in every dimension

attached input jpeg ::

00t_circ_swatches0__000_000

Many Thanks again -

About Q index

Dear!
I thank for your package, but i need more metrics for pansharpening. Especially, Q index.
You can refer the following paper:A Global Quality Measurement of Pan-Sharpened Multispectral Imagery.
Thank you in advance.
Yours Sincerely.

Errors in reference and code for spectral distortion index

The reference Multispectral and Panchromatic Data Fusion Assessment Without Reference has an obvious error in equation 5 for Spectral Distortion Index. The text says the values on the main diagonal are all equal to one. Thus, values for which r = l convey no information, so the equation should have r != l instead of r != 1. Excluding the main diagonal 1 entries means that L(L-1) entries are used, which corresponds to the denominator used in the equation for averaging. Thus, in no_ref.m your d_lambda routine for spectral distortion index should exclude r = l values on the main diagonal.

ERGAS extremely high

Situation:
tried to compute ERGAS values for fused image pairs and got extreme high values back, in the order of 100.000's. Even for relatively similar images with high CC>98 , Q>95 and small RMSE~30 for RGB images, I got an ergas of >2000.

As I look into publications for similar scenarios
https://www.researchgate.net/figure/ERGAS-values-for-fused-and-original-images_tbl2_262601486 the ergas should be much smaller, I would expect values around 10-15 for my application from visual inspection.

Are there any specific requirements regarding image data format, normalization etc? Looked at the code but didnt found a bug at a first glance.

Make it clear that metric functions expect Numpy arrays as input

Hi there 👋

I was just trying out Sewar and it took me a while to figure out that the functions the library exposes expect numpy arrays as their input.

This wasn't very clear, as the README only offers the following example:

>>> from sewar.full_ref import uqi
>>> uqi(img1,img2)
0.9586952304831419

The documentation website doesn't seem to explain this either, so I was only able to figure it out from the command_line.py source code.

I think doing one or more of the following things would make it easier for new users to get started:

  • Update the README example and show that a Numpy array is created and passed to the metric functions
  • Throw an exception if a metric function doesn't receive a Numpy array. The exception message should mention this.
  • Update the documentation website, mentioning that the GT and P parameters should be a Numpy array
  • For the Mypy users: add type annotations so users' intellisense tells them they're passing the wrong type

sewar uqi usage

Hi
I am able to run sewar uqi from the command line, but have not been able to successfully use the function within a python script.

Considering the following snippet:

from sewar.full_ref import uqi
from PIL import Image

im = Image.open("arctic_fox_PNG41386.png")
im2 = Image.open("arctic_fox_PNG41386.png")

uqi(im, im2)

When I run this, I get the following error:
PngImageFile' object has no attribute 'shape

What object is uqi expecting? I have tried to put the path of the image directly into uqi but does not seem to expect a string value.

cs value

Hi @andrewekhalel ,

First of all, thank you for this repo! I would like to ask that what is the so-called "cs value" in the ssim metric implementation?

Best wishes,
Daniel

MSSSIM sometimes returns 'nan'

..and this is the warning that comes with it:

[...]\sewar\full_ref.py:298: RuntimeWarning: invalid value encountered in power
  return (np.prod(mcs[0:scales-1] ** weights[0:scales-1]) * \
[...]\sewar\full_ref.py:299: RuntimeWarning: invalid value encountered in double_scalars
  (mssim[scales-1] ** weights[scales-1]))

Request for additional metric (q2n: Q4/Q8)

Hi!
Except for those metrics given by author, there is another important metric (called Q4/Q8).
Is it possible to implement it?
If anyone can help me, I'll appreciate.

Thank you in advance.

from scipy.misc import imresize error

F:\paj\neox_tools\hero2>sewar ssim 00080265.png 00325768.png
Traceback (most recent call last):
File "c:\users\zhouhang\miniconda3\lib\runpy.py", line 193, in run_module_as_main
"main", mod_spec)
File "c:\users\zhouhang\miniconda3\lib\runpy.py", line 85, in run_code
exec(code, run_globals)
File "C:\Users\zhouhang\Miniconda3\Scripts\sewar.exe_main
.py", line 5, in
File "c:\users\zhouhang\miniconda3\lib\site-packages\sewar_init
.py", line 18, in
from .no_ref import d_lambda
File "c:\users\zhouhang\miniconda3\lib\site-packages\sewar\no_ref.py", line 5, in
from scipy.misc import imresize
ImportError: cannot import name 'imresize'

about sam

the definition of sam is different from codes that calculate sam

Trying to use SAM as a loss metric in KERAS

I am training a CNN to perform single image super resolution, and I want to use SAM as a loss metric. I imported sewar and defined SAM function, and then added it to the loss metrics in the model arguments as so:

def sam(y_true,y_pred):
    print(tf.shape(y_true))
    print(tf.shape(y_pred))
    return sewar.full_ref.sam(y_true,y_pred)



opt=K.optimizers.Adam(learning_rate=0.0001) # Adam optimizer
model.compile(optimizer=opt,loss=perceptual_loss,metrics=[psnr,ssim,K.losses.mean_squared_error, sam])
history = model.fit_generator(generator=train_generator(),
                              steps_per_epoch=np.ceil(float(len(train_list)) / float(batch_size)),
                              epochs=40, #20
                              verbose=1,
                              validation_data=valid_generator(),
                              shuffle=True,
                              validation_steps=np.ceil(float(len(val_list)) / float(batch_size)))

However, I get the following error:

AssertionError: Supplied images have different sizes (None, 32, 32, 3) and (None, 32, 32, 3)

I am confused as to how to solve this, because it seems to me that the sizes are the same. Can someone help me with this?

UnicodeDecodeError when running setup.py

Running setup.py may raise a UnicodeDecodeError on some systems:

$ pip install sewar
Collecting sewar
  Downloading https://files.pythonhosted.org/packages/0c/5e/9153f0867d94c2c6959c35fda5a323cf3ca8e9979d03426f8444a4b3ba5b/sewar-0.4.3.tar.gz
    ERROR: Command errored out with exit status 1:
     command: /home/gfz-fe/python_GFZ/python/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-ljfrpcw9/sewar/setup.py'"'"'; __file__='"'"'/tmp/pip-install-ljfrpcw9/sewar/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-ljfrpcw9/sewar/pip-egg-info
         cwd: /tmp/pip-install-ljfrpcw9/sewar/
    Complete output (9 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-ljfrpcw9/sewar/setup.py", line 12, in <module>
        long_description=readme(),
      File "/tmp/pip-install-ljfrpcw9/sewar/setup.py", line 6, in readme
        return f.read()
      File "~/python/lib/python3.6/encodings/ascii.py", line 26, in decode
        return codecs.ascii_decode(input, self.errors)[0]
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 722: ordinal not in range(128)
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

To avoid this, just replace this line by with open('README.md', encoding='UTF-8') as f:.

Request for additional metric (q2n: Q4/Q8)

Dears!
Except for those metrics given by author, there is another important q2n metric (also called Q4/Q8).
Please add this metric when updating sewar package.
If anyone know the answer, i want to share it with me, and thank him/her in advance.

No module named sewar

My pycharm shows that i have successfully installed sewar, with python 3.9. But when import it, error occurs.

Any plan to add PSNR-B[1]?

  1. Yim, Changhoon, and Alan Conrad Bovik. "Quality assessment of deblocked images." IEEE Transactions on Image Processing 20.1 (2011): 88-98.

About ERGAS

I am confused the code about ERGAS. I always used one code in matlab. So I ported this code to Python:
`def ergas_matlab(GT,P,r=1):
n_samples = GT.shape[0] * GT.shape[1]
nb = GT.shape[2]
#RMSE
aux = np.sum(np.sum((P - GT)**2, 0), 0)/n_samples
buff_aux = np.sum(np.sum((P - GT), 0), 0)/n_samples
mean_x = np.sum(np.sum(P, 0), 0)/n_samples
rmse_per_band = np.sqrt(aux)
# ergas
mean_y = np.sum(np.sum(GT, 0), 0)/n_samples
ergas = 100rnp.sqrt(np.sum((rmse_per_band/mean_y)**2)/nb)

return ergas`

error in _vifp_single

I think there's an error in the _vifp_single function of the full_ref.py module. The following lines:

gt=gaussian_filter(GT,sigma=S,truncate=T)[::2, ::2]
p=gaussian_filter(P,sigma=S,truncate=T)[::2, ::2]

apply filters at each scale to the original inputs GT and P, as opposed to recursively filtering and subsampling each level so as to generate an image pyramid. The result is a significant difference from the scores output by the author's own implementation at https://github.com/aizvorski/video-quality/blob/master/vifp.py.

second output (loop) is not consistent anymore SSIM

The SSIM returns two values: return np.mean(ssims),np.mean(css)

This works fine, when invoked the first time. Right now I'm looping over two folders (image pairs) and at the second image pair I get a type error (TypeError: 'tuple' object is not callable), which keeps this way, even when I remove the first image from the two folders. So it does always work for the first pair, which says, my images are not corrupt or the like.

Any advice is much appreciated. The other metrics do work fine for me.
print(f'mse|rmse|psnr|rmse_sw|uqi|ergas|scc|vifp|psnrb')

Regards

A Question about "rmse_sw"

Hi,

Thank you for the great python package. I have several questions related to the function rmse_sw.

  1. I would like to ask what is the main difference between the functions rmse and rmse_sw? Can rmse_sw deal with a shifted object in photos without substantially increase the computed RMSE?
  2. I have read your code. Why do we need the uniform_filter when doing rmse_sw? What is the purpose of the uniform_filter?
  3. Could you provide some related paper or other work about "sliding window RMSE"? I had a hard time trying to search for it online.

Thank you very much for your help.

how can we run this code for faster calculation on cuda gpu?

I'm getting this error when i'm trying to run on this code with gpu. could you please help?

Traceback (most recent call last):
File "test_ssim.py", line 123, in
ps=psnr(image_b,image_a)
File "C:\Users\Aamir\Anaconda3\envs\pytorch\lib\site-packages\sewar\full_ref.py", line 64, in psnr
MAX = np.iinfo(GT.dtype).max
File "C:\Users\Aamir\Anaconda3\envs\pytorch\lib\site-packages\numpy\core\getlimits.py", line 506, in init
raise ValueError("Invalid integer data type %r." % (self.kind,))
ValueError: Invalid integer data type 'O'.

Compare two images

I am comparing two images but getting an error

from sewar.full_ref import ssim
img_path1 = "/home/khawar/Desktop/Image-Quality/kodim01.png"
img_path = "/home/khawar/Desktop/Image-Quality/kodim01.png"
result = ssim(img_path, img_path1)
print(result)

Price-matching other repos

NameError: name 'log2' is not defined while calling psnrb()

Hi,
Here are the steps to reproduce the error, it seems that you forgot from math import log2 on top of utils.py:

import cv2 as cv
x = cv.imread('/home/user/Pictures/a.jpg', cv.IMREAD_GRAYSCALE)
y = cv.imread('/home/user/Pictures/b.jpg', cv.IMREAD_GRAYSCALE)
import sewar
sewar.psnrb(x, y)

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/user/.virtualenvs/sq/lib/python3.6/site-packages/sewar/full_ref.py", line 387, in psnrb
    bef = _compute_bef(P)
  File "/home/user/.virtualenvs/sq/lib/python3.6/site-packages/sewar/utils.py", line 144, in _compute_bef
    t = log2(block_size)/log2(min(height, width))
NameError: name 'log2' is not defined

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.