1

I am trying to implement the S1 measure (Spectral Measure of Sharpness - Section III-A) from this paper. Here we have to calculate slope (alpha) of the magnitude spectrum for an image in order to measure sharpness. I am able to write the other part of the algorithm, but unable to calculate the slope. Here is my code. Function 'alpha' is where I calculate the magnitude_spectrum and I think using this we can calculate the slope but am not sure how to do that -

def aplha(image_block):
    img_float32 = np.float32(image_block)

    dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT)
    dft_shift = np.fft.fftshift(dft)
    magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1]))

    return output (??)

Rest of the code:

def S1_calc(alpha):
    tou1 = -3
    tou2 = 2
    output = 1 - (1 / (1 + np.exp(tou1 * (alpha - tou2))))
    return output

def lx(image_block):
    b = 0.7656
    k = 0.0364
    y = 2.2
    return np.power((b + k * image_block), y)

def contrast(lx_val):
    T1 = 5
    T2 = 2
    max_val = np.max(lx_val)
    min_val = np.min(lx_val)
    mean_val = np.mean(lx_val)
    return (((max_val - min_val) < T1) or (mean_val < T2))

def image_gray(image_RGB):
    output = (0.2989 * image_RGB[:,:,0] +
              0.5870 * image_RGB[:,:,1] +
              0.1140 * image_RGB[:,:,2])

    return output

def S1(gray_image, m = 32, d = 24):
    ### SPECTRAL MEASURE OF SHARPNESS ###

    # m = each block size
    # d = overlapping pixels of neighbouring blocks
    h,w = gray_image.shape
    output = gray_image.copy()

    row = 0
    while (row < h):
        col = 0
        while (col < w):

            top = row
            bottom = min(row + m, h)
            left = col
            right = min(col + m, w)

            image_block = gray_image[top : bottom, left : right]

            lx_val = lx(image_block)
            contrast_bool = contrast(lx_val)
            if contrast_bool==True:
                output[top : bottom, left : right] = 0
            else:
                alpha_val = aplha(image_block)
                output[top : bottom, left : right] = S1_calc(alpha_val)

            col = col + m - d
        row = row + m - d

    return output

Am using jupyter notebook, python 3.6

Malgo
  • 1,871
  • 1
  • 17
  • 30
  • Using the right tags should bring your post to the attention of people that can actually help you. [Signal Processing SE](https://dsp.stackexchange.com) might be a better place to ask. But, most importantly, make sure you actually ask a question. Right now you say what you want to do, and show what you’ve tried, but don’t ask an actual question. – Cris Luengo Feb 23 '19 at 15:03
  • Oh.. thank you for your comment and edits. Am new to the forum so still getting to know the workings. My actual question is the second part of the heading, i.e., how to calculate the slope of a magnitude spectrum of an image? – Malgo Feb 25 '19 at 05:05
  • (If you include question in the original post, it might be better in attracting the right people who can answer.) – zabop Feb 25 '19 at 15:46

1 Answers1

0

You could check this MATLAB code. See also another MATLAB code.

According to the latter one, we need to know freq and power value, and then we could fit these two var with a linear function, the slope of the line is what we need. We could get the slope with np.polyfit.

Now, our question is how to get the freq of a image, you could do this:

from skimage.data import camera
import numpy as np

image = camera()
height, width = image.shape
u, v = np.meshgrid(np.arange(height // 2), np.arange(width // 2))
freq = np.round(np.sqrt(u**2 + v**2)).astype(np.int64)

Now freq should be the same shape as fft transform of the input image. You need to sum all value of the magnitude_spectrum where they have the same freq, like this:

freq_uniq = np.unique(freq.flatten())
y = []
for value in f_uniq:
  y.append(magnitude_spectrum[f == value].sum())
y = np.array(y)

Finally, you could just fit freq_uniq and y and get the slope. You might need to scale them with np.log first.

Darcy
  • 13
  • 4