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

Attempts to calc psnr independently result in differences #14

Open
robmarkcole opened this issue Feb 3, 2022 · 4 comments
Open

Attempts to calc psnr independently result in differences #14

robmarkcole opened this issue Feb 3, 2022 · 4 comments

Comments

@robmarkcole
Copy link

robmarkcole commented Feb 3, 2022

I am using greyscale pngs which have been contrast stretched to have pixel values in range 0-255. I have a value for test_psnr, but when I independently calculate this value I get a different result. However I note that in your implementation of PSNR the max_val is set to 1, whereas elsewhere implementations use 255. Also in the enhance method you multiply by 255 then clip to 0,255, which is at odds with using max_val is set to 1? My functions are below: can you advise what is the issue resulting in the differences?

def contrast_stretch(img: np.array) -> np.array:
    """Contrast stretch an image
    Return pixels in range 0 to 255.
    Parameters
    ----------
    img : np.array
        Image to be contrast stretched
    Returns
    -------
    np.array
        Contrast stretched image
    """
    img_min = img.min()
    img_max = img.max()
    return (img - img_min) / (img_max - img_min)*255

def psnr(img1: np.array, img2: np.array, max_value: float = 255.0) -> float:
    """
    Compute the PSNR between two images.
    For 8bit greyscale max value is 255 and min is zero.
    """
    mse = np.mean((img1 - img2) ** 2)
    return round(20 * math.log10(max_value / math.sqrt(mse)), 1)
@isaaccorley
Copy link
Owner

Thanks for catching the bug in the enhance method. I haven't updated this in a while but I was planning on scrapping the metrics and using torchmetrics instead (i.e. SSIM, MSSIM, MSE, PSNR) since they work in distributed training settings.

@robmarkcole
Copy link
Author

There are also many exotic metrics in https://piq.readthedocs.io/en/latest/overview.html

@isaaccorley
Copy link
Owner

Nice find. I haven't seen that library before.

@isaaccorley
Copy link
Owner

I think I'm going to just refactor and remove the metrics and recommend users user piq or torchmetrics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants