Guest User

Untitled

a guest
Jul 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. import numpy as np
  2. import skimage.color as color
  3. from skimage.measure import compare_ssim
  4.  
  5.  
  6. def compute_difference(im, imgt, SRF = 4):
  7. '''
  8. COMPUTE_DIFFERENCE: compute image quality
  9.  
  10. Input:
  11. - im: super-resolved image (numpy array HxWxC)
  12. - imgt: groundtruth high-resolution image (numpy array HxWxC)
  13. - SRF: super-resolution factor
  14. Output:
  15. - psnr: Peak signal-to-noise ratio
  16. - ssim: Structural similarity index
  17. - ssim_map: SSIM map
  18. '''
  19. # =========================================================================
  20. # Retrieve only luminance channel
  21. # =========================================================================
  22. h,w,c = im.shape
  23. im = color.rgb2ycbcr(im)
  24. imgt = color.rgb2ycbcr(imgt)
  25. im_y = im[:,:,0]
  26. imgt_y = imgt[:,:,0]
  27.  
  28. # =========================================================================
  29. # Remove border pixels as some methods (e.g., A+) do not predict border pixels
  30. # =========================================================================
  31. cropPix = SRF
  32. im_y = np.around(im_y[cropPix:h-cropPix, cropPix:w-cropPix]).astype(np.double)
  33. imgt_y = np.around(imgt_y[cropPix:h-cropPix, cropPix:w-cropPix]).astype(np.double)
  34.  
  35. # =========================================================================
  36. # Compute Peak signal-to-noise ratio (PSNR)
  37. # =========================================================================
  38. mse = np.mean(np.mean((im_y-imgt_y)**2,1),0)
  39. psnr = 10*np.log10(255*255/mse)
  40.  
  41. # =========================================================================
  42. # Compute Structural similarity index (SSIM index)
  43. # =========================================================================
  44. ssim, ssim_map = compare_ssim(im_y, imgt_y, K1=0.01, K2=0.03, gaussian_weights=True,data_range=256)
  45.  
  46. return psnr, ssim, ssim_map
Add Comment
Please, Sign In to add comment