1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python script for MTF

Discussão em 'Python' iniciado por Stack, Março 30, 2025.

  1. Stack

    Stack Membro Participativo

    I want to implement a MTF(Modulation Transfer function) script in python. To this script I want to pass an image as an argument and get the MTF value as result.

    This MTF value will be used to quantify sharpness of an image in the project. here are some of the link that I used as reference to develop the script - [currently i am using this link to develop the scrip]https://github.com/weiliu4/py_mtf/blob/master/mtf.py https://github.com/habi/GlobalDiagnostiX/blob/master/MTF.py

    Definition of MTF: the MTF is defined as the FFT of the line spread function.

    The line spread function is defined as the derivative of the edge spread function. The edge spread function are the values along an edge, ideally a knife-edge test target.

    def ESF(path):
    img=cv2.imread(path)
    img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    #m_out=0.299*img[:,:,0]+0.589*img[:,:,1]+0.114*img[:,:,2]
    #x=m_out[100,:]
    x=img
    mu=np.sum(x)/(x.shape[0])
    tmp=(x[:]-mu)**2
    sigma=np.sqrt(np.sum(tmp)/(x.shape[0]))
    edge_function=(x[:]-mu)/sigma
    #print(edge_function)
    edge_function=edge_function[::3]
    #lsf=edge_function[:-2]-edge_function[2:]
    lsf=np.abs(np.diff(edge_function))
    mtf=abs(np.fft.fft(lsf))
    print(mtf)
    #print(np.max(mtf))
    mtf=mtf[:]/np.max(mtf)
    mtf=mtf[:len(mtf)//2]
    mtf=np.arange(mtf.shape[0]*mtf.shape[1]).reshape(mtf.shape[0],mtf.shape[1])
    ix=np.arange(mtf.shape[0])/(mtf.shape[0])
    mtf_poly=np.polyfit(ix, mtf,6)
    #print(mtf_poly)
    #mtf_poly=np.squeeze(mtf_poly)
    #print(mtf.shape[0])
    #print(mtf.shape[1])
    #poly=np.poly1d(mtf_poly)
    #print(poly)
    plt.figure()
    plt.title("MTF")
    plt.xlabel(r'Frecuency $[cycles/pixel]$') ; plt.ylabel('mtf')
    p= plt.plot(mtf,'-or')
    #ll = plt.plot(poly(ix))
    #plt.legend([p,ll],["MTF values","polynomial fit"])
    plt.grid()
    #plt.show()


    Currently I am getting error while evaluating the line

    poly=np.poly1d(mtf_poly)


    Any help to what I am doing wrong. Also can someone please let me know if I am headed in the correct direction to find MTF.

    TIA

    Continue reading...

Compartilhe esta Página