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

Help with luminance, contrast and structure #30

Open
Rubiel1 opened this issue Jun 26, 2020 · 1 comment
Open

Help with luminance, contrast and structure #30

Rubiel1 opened this issue Jun 26, 2020 · 1 comment

Comments

@Rubiel1
Copy link

Rubiel1 commented Jun 26, 2020

So, following wiki, I wanted to define luminance ( l ), contrast ( c ) and structure ( s), for that I need the standard deviation: square root of the variance (sigma1_ below). But I also tested the formula $E((X-E(X))^2)$ and it gives me two different results.

Below is the code for $sigma1_= (sigma1_sq)^.5=E(X^2)-E(X)^2$ and $sigma1=E((X-E(X))^2)$ that can be added to here.

    mu1 = F.conv2d(img1, window, padding = window_size//2, groups = channel)
    mu2 = F.conv2d(img2, window, padding = window_size//2, groups = channel)
    mu1_sq = mu1.pow(2)
    mu2_sq = mu2.pow(2)
    mu1_mu2 = mu1*mu2
    sigma1_sq = F.conv2d(img1*img1, window, padding = window_size//2, groups = channel) - mu1_sq
    sigma2_sq = F.conv2d(img2*img2, window, padding = window_size//2, groups = channel) - mu2_sq
    sigma12 = F.conv2d(img1*img2, window, padding = window_size//2, groups = channel) - mu1_mu2
    C1 = 0.01**2
    C2 = 0.03**2
    sigma1 = F.conv2d((img1-mu1).pow(2), window, padding = window_size//2, groups = channel).pow(.5)
    sigma2 = F.conv2d((img2-mu2).pow(2), window, padding = window_size//2, groups = channel).pow(.5)
    sigma1_= sigma1_sq.pow(.5)
    sigma2_= sigma2_sq.pow(.5)
    assert  torch.all(sigma2.eq(sigma2_)) #this returns false
    assert  torch.all(sigma1.eq(sigma1_))

Is there a problem with my code?
Is there any reason to choose one of the two formulas? I assume it has to do with numerical stability.

@lidq92
Copy link

lidq92 commented Aug 15, 2020

@Rubiel1 Your implementation of the second way is wrong. You should be aware of the block-based calculation for SSIM map.
It is not (img1-mu1).pow(2), actually, you need to calculate E[(img1_block_ij-mu1_ij).^2] for all i,j. So the first way is the right way.

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