python 医学图像k-space下采样/傅里叶变换

对图像进行k-sapce下采样

程序仓库:

https://github.com/DejaVuyan/python

from util import save_image,mkdirs,fft_2D,read_image
import argparse
import os

"""
将文件夹中的图片进行k-space下采样后输出
"""

if __name__ == '__main__':
    # 初始化对象
    parser = argparse.ArgumentParser(description='Process some integers.')
    # 添加参数,包括type,default,help等等,注意有个必须的
    # 定义必须要的参数,输入,输出与下采样比例
    parser.add_argument('--input', type=str, required=True, help='the input dir path of images')
    parser.add_argument('--output', type=str, required=True, help='the output dir path of images')
    parser.add_argument('--ratio', type=float, required=True,help='The image is cropped in the middle of the k-space space to preserve the proportion')

    args = parser.parse_args()  # 初始化这个,-h才有显示

    input_dir = args.input  # /data/yuzun/stytr_008_SE10_out_256
    output_dir =args.output # /data/yuzun/stytr_008_SE10_out_256_kspace
    ratio = args.ratio # 0.25

    # 统计所处理的图片的数量
    count = 0
    # 批量读取input所指定的路径中的图片
    for image_name in os.listdir(input_dir):
        image_path = os.path.join(input_dir,image_name)
        img = read_image(image_path,1)  # numpy 灰度图则是(H,W)
        img = fft_2D(img,ratio)   # 返回的是Tensor,归一化后的
        mkdirs(output_dir)
        output_path = os.path.join(output_dir,image_name)
        save_image(img,output_path)
        count=count+1
    print('convert {} images'.format(count))

util中的通用库

def read_image(image_path,channel=3):
    """
    save image by opencv
    @param image_path:
    @param channel: if is 3,save colorful image,if is 1 save gray image
    @return: image,numpy typy,(H,W,C),uint8
    """
    img = cv2.imread(image_path)
    if channel==1:
      img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # convert gray image
    return img

def save_image(image_numpy, image_path):
    """
    save image using opencv
    @param image_numpy:
    @param image_path:
    @param channel:
    @return:
    """
    cv2.imwrite(image_path, image_numpy)
    
def mkdirs(paths):
    """create empty directories if they don't exist
    Parameters:
        paths (str list) -- a list of directory paths
    """
    if isinstance(paths, list) and not isinstance(paths, str):
        for path in paths:
            mkdir(path)
    else:
        mkdir(paths)
        
def fft_2D(input_Tensor,mask_ratio = 0.25):
    """使用2D傅里叶变换,将经过base_dataset,transformer,标准化后的(C,H,W)的Tensor
    转换到频域,然后使用低通滤波器滤波,面积为原图*mask_ratio
    假设input_tensor是个灰度图像,输入是(H,W)
    """
    #print(input_Tensor.shape)
    # img = input_Tensor.squeeze(dim=0)  # (H,W)
    img = input_Tensor
    # 创建一个低通滤波器
    mask = np.zeros(img.shape)
    crow = int((img.shape[0]) / 2)  # 中心位置
    ccol = int((img.shape[1]) / 2)  # 中心位置
    mask_l = int(math.sqrt(img.shape[0] * img.shape[1] * mask_ratio) / 2)
    mask[crow - mask_l:crow + mask_l, ccol - mask_l:ccol + mask_l] = 1

    f = np.fft.fft2(img)  # 2D傅里叶变换
    fshift = np.fft.fftshift(f)  # 低频中心化
    # 滤波
    fshift = fshift * mask

    # 傅里叶逆变换
    ishift = np.fft.ifftshift(fshift)  # 逆中心化
    iimg = np.fft.ifft2(ishift)  # 傅里叶逆变换
    iimg = np.abs(iimg)  # 去掉虚部 此时数据为float64的ndarray
    # iimg = np.expand_dims(iimg, axis=-1)  # 回到了原来的函数
    #print(iimg.shape)
    # 变成Tensor
    # to_tensor = ToTensor()
    # iimg_Tensor = to_tensor(iimg).float()  # 从double float64转换为float 32
    # # 归一化
    # nor = Normalize((0.5,), (0.5,))
    # iimg_Tensor = nor(iimg_Tensor)

    return iimg

你可能感兴趣的:(python脚本,python,opencv,计算机视觉)