SHARE
TWEET

jpeg_quality_score.py

a guest Oct 30th, 2012 341 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys
  5. import numpy as np
  6. import Image
  7.  
  8.  
  9. def jpeg_quality_score(filename):
  10.     img = np.array(Image.open(filename))
  11.  
  12.     M, N = img.shape
  13.     if M < 16 or N < 16:
  14.         return -2
  15.  
  16.     x = np.double(img)
  17.  
  18.     ###########################################################################
  19.     # Feature Extraction:
  20.     ###########################################################################
  21.  
  22.     # 1. horizontal features
  23.  
  24.     d_h = x[:, 1:N] - x[:, 0:(N - 1)]
  25.  
  26.     B_h = np.mean(abs(d_h[:, 7:(8 * (np.floor(N / 8) - 1)):8]))
  27.  
  28.     A_h = (8 * np.mean(abs(d_h)) - B_h) / 7
  29.  
  30.     sig_h = np.sign(d_h)
  31.     left_sig = sig_h[:, 0:(N - 2)]
  32.     right_sig = sig_h[:, 1:(N - 1)]
  33.     Z_h = np.mean((left_sig * right_sig) < 0)
  34.  
  35.     # 2. vertical features
  36.  
  37.     d_v = x[1:M, :] - x[0:(M - 1), :]
  38.  
  39.     B_v = np.mean(abs(d_v[7:(8 * (np.floor(M / 8) - 1)):8, :]))
  40.  
  41.     A_v = (8 * np.mean(abs(d_v)) - B_v) / 7
  42.  
  43.     sig_v = np.sign(d_v)
  44.     up_sig = sig_v[0:(M - 2), :]
  45.     down_sig = sig_v[1:(M - 1), :]
  46.     Z_v = np.mean((up_sig * down_sig) < 0)
  47.  
  48.     # 3. combined features
  49.  
  50.     B = (B_h + B_v) / 2
  51.     A = (A_h + A_v) / 2
  52.     Z = (Z_h + Z_v) / 2
  53.  
  54.     ###########################################################################
  55.     # Quality Prediction
  56.     ###########################################################################
  57.  
  58.     alpha = -245.8909
  59.     beta = 261.9373
  60.     gamma1 = -239.8886 / 10000
  61.     gamma2 = 160.1664 / 10000
  62.     gamma3 = 64.2859 / 10000
  63.  
  64.     score = alpha + beta * (B ** gamma1) * (A ** gamma2) * (Z ** gamma3)
  65.     return score
  66.  
  67.  
  68. if __name__ == "__main__":
  69.     #print jpeg_quality_score("Sego_lily_cm_nb.jpg")
  70.     #print jpeg_quality_score("Sego_lily_cm-150_nb.jpg")
  71.     print jpeg_quality_score(sys.argv[1])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top