Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import sys
- import numpy as np
- import Image
- def jpeg_quality_score(filename):
- img = np.array(Image.open(filename))
- M, N = img.shape
- if M < 16 or N < 16:
- return -2
- x = np.double(img)
- ###########################################################################
- # Feature Extraction:
- ###########################################################################
- # 1. horizontal features
- d_h = x[:, 1:N] - x[:, 0:(N - 1)]
- B_h = np.mean(abs(d_h[:, 7:(8 * (np.floor(N / 8) - 1)):8]))
- A_h = (8 * np.mean(abs(d_h)) - B_h) / 7
- sig_h = np.sign(d_h)
- left_sig = sig_h[:, 0:(N - 2)]
- right_sig = sig_h[:, 1:(N - 1)]
- Z_h = np.mean((left_sig * right_sig) < 0)
- # 2. vertical features
- d_v = x[1:M, :] - x[0:(M - 1), :]
- B_v = np.mean(abs(d_v[7:(8 * (np.floor(M / 8) - 1)):8, :]))
- A_v = (8 * np.mean(abs(d_v)) - B_v) / 7
- sig_v = np.sign(d_v)
- up_sig = sig_v[0:(M - 2), :]
- down_sig = sig_v[1:(M - 1), :]
- Z_v = np.mean((up_sig * down_sig) < 0)
- # 3. combined features
- B = (B_h + B_v) / 2
- A = (A_h + A_v) / 2
- Z = (Z_h + Z_v) / 2
- ###########################################################################
- # Quality Prediction
- ###########################################################################
- alpha = -245.8909
- beta = 261.9373
- gamma1 = -239.8886 / 10000
- gamma2 = 160.1664 / 10000
- gamma3 = 64.2859 / 10000
- score = alpha + beta * (B ** gamma1) * (A ** gamma2) * (Z ** gamma3)
- return score
- if __name__ == "__main__":
- #print jpeg_quality_score("Sego_lily_cm_nb.jpg")
- #print jpeg_quality_score("Sego_lily_cm-150_nb.jpg")
- print jpeg_quality_score(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement