def main(file_to_denoise, param, output_file_name) :

    path, file, ext = sct.extract_fname(file_to_denoise)

    img = nib.load(file_to_denoise)
    hdr_0 = img.get_header()

    data = img.get_data()
    aff = img.get_affine()


    # Process for manual detecting of background
    mask = data[:, :, :] > noise_threshold

    data = data[:, :, :]

    if '-std' in arguments:
        sigma = std_noise
        # Application of NLM filter to the image
        print 'Applying Non-local mean filter...'
        if param.parameter == 'Rician':
            den = nlmeans(data, sigma=sigma, mask=None, rician=True)
        else : den = nlmeans(data, sigma=sigma, mask=None, rician=False)
    else:
        # # Process for manual detecting of background
        # mask = data[:, :, :] > noise_threshold
        # data = data[:, :, :]
        sigma = np.std(data[~mask])

        print("vol size", data.shape)


        # Application of NLM filter to the image
        print 'Applying Non-local mean filter...'
        if param.parameter == 'Rician':
            den = nlmeans(data, sigma=sigma, mask=mask, rician=True)
        else : den = nlmeans(data, sigma=sigma, mask=mask, rician=False)

    t = time()
    print("total time", time() - t)
    print("vol size", den.shape)


    axial_middle = data.shape[2] / 2

    before = data[:, :, axial_middle].T
    after = den[:, :, axial_middle].T

    diff_3d = np.absolute(den.astype('f8') - data.astype('f8'))
    difference = np.absolute(after.astype('f8') - before.astype('f8'))
    if '-std' not in arguments:
        difference[~mask[:, :, axial_middle].T] = 0

    if param.verbose == 2 :
        fig, ax = plt.subplots(1, 3)
        ax[0].imshow(before, cmap='gray', origin='lower')
        ax[0].set_title('before')
        ax[1].imshow(after, cmap='gray', origin='lower')
        ax[1].set_title('after')
        ax[2].imshow(difference, cmap='gray', origin='lower')
        ax[2].set_title('difference')
        for i in range(3):
            ax[i].set_axis_off()

        plt.show()
        plt.savefig('denoised_S0.png', bbox_inches='tight')

    #Save files
    img_denoise = nib.Nifti1Image(den, None, hdr_0)
    img_diff = nib.Nifti1Image(diff_3d, None, hdr_0)
    if output_file_name != None :
        output_file_name =output_file_name
    else: output_file_name = file + '_denoised' + ext
    nib.save(img_denoise,output_file_name)
    nib.save(img_diff, file + '_difference' +ext)



#=======================================================================================================================
# Start program
#=======================================================================================================================
dataset/ETHPy150Open neuropoly/spinalcordtoolbox/dev/tamag/old/sct_denoising_nlm.py/main/original