快速批量Radon变换的Python实现——使用PyTorch函数affine_grid和grid_sample

Radon变换的旋转过程

Radon变换的旋转过程可以使用ndimage.rotate()对图像进行特定角度的旋转,但这个过程比较耗时,不利于投影角度较多或者需要对批量图像进行Radon变换的情况。而通过利用PyTorch中的仿射变换相关函数affine_grid()和grid_sample()也可以实现旋转的过程,可以在GPU上进行运算,Radon变换所需的时间大幅减少。

快速批量Radon变换的Python实现

from scipy import ndimage
import numpy as np
import matplotlib.pyplot as plt
import imageio
from cv2 import cv2
import torch
import torchvision.transforms as transforms
from torch.nn import functional as F
import math

def DiscreteRadonTransform(image, viewnum, batchSize):
    # image: batchSize*imgSize*imgSize
    channels = len(image[0])
    res = torch.zeros((channels, viewnum))
    res = res.cuda()
    for s in range(viewnum):
    
        angle = -math.pi - 180/viewnum*(s+1) * math.pi / 180
        A = np.array([[np.cos(angle), -np.sin(angle)],
                          [np.sin(angle), np.cos(angle)]])
        theta = np.array([[A[0, 0], A[0, 1], 0], [A[1, 0], A[1, 1], 0]])
        theta = torch.from_numpy(theta).type(torch.FloatTensor)
        theta = theta.unsqueeze(0)
        theta = theta.cuda()
        image_temp = torch.from_numpy(image).type(torch.FloatTensor)
        image_temp = image_temp.unsqueeze(1)
        image_temp = image_temp.cuda()
        theta = theta.repeat(batchSize,1,1)
        grid = F.affine_grid(theta, torch.Size((batchSize,1,512,512)))
        rotation = F.grid_sample(image_temp, grid)
        rotation = torch.squeeze(rotation)
        res[:,s] = torch.sum(rotation,dim=0)
        
    return res
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值