pytorch实现图像投影变换

python 复制代码
import cv2
import torch


def cpu_remap(numpy_img,mapx,mapy):
    return cv2.remap(numpy_img,mapx,mapy,cv2.INTER_LINEAR)

def gpu_remap(numpy_img,map_tensor):
    '''
    numpy_img:原始图像格式为ndarray
    map_tensor:[N,H,W,C]用于grid_sample的map参数,需要规制到-1到1
    '''
    # 准备图像数据
    img_tensor = torch.from_numpy(numpy_img).contiguous().cuda(non_blocking=True)
    img_tensor = img_tensor.permute(2,0,1).unsqueeze(0).cuda().float()
    res = torch.nn.functional.grid_sample(img_tensor,map_tensor,
                                          mode='bilinear',
                                          padding_mode='zeros',
                                          align_corners=None)
    res = res.char()
    res = res[0].permute(1,2,0)
    res = res.cpu()
    res = res.numpy()
    res = np.uint8(res)
    return res
@profile
def main():
   #已知单应矩阵(投影矩阵M)
   M=np.asarray([[-5.23249213e+00, -2.83428439e+01,  2.74163372e+03],
       [-2.71994329e+00, -2.52929752e+01,  1.13544900e+04],
       [-1.38531350e-04, -1.40379841e-02,  1.00000000e+00]])
   
   
   img_revise = cv2.imread('206_revise.jpg') # 无畸变的图像
   img_perspect = cv2.warpPerspective(img_revise, M, (2800, 1500), borderValue=0)
   cv2.imwrite('warpPerspective.jpg',img_perspect)

   x, y = np.meshgrid(np.arange(2800), np.arange(1500))
   grid = np.vstack([x.flatten(),y.flatten(),np.ones(y.flatten().shape)])
   grid_trans = np.linalg.inv(M)@grid
   grid_trans = grid_trans/grid_trans[2]
   grid_trans = grid_trans[:2]
   mapx = np.float32(np.reshape(grid_trans[0],[1500,2800]))#/1920*2-1
   mapy = np.float32(np.reshape(grid_trans[1],[1500,2800]))#/1080*2-1
   print(mapx.shape)
   img_perspcet = cpu_remap(img_revise,mapx,mapy)
   cv2.imwrite('cpu_remap.jpg',img_perspect)

   grid_trans_x = torch.from_numpy(mapx).unsqueeze(2)/1920*2-1
   grid_trans_y = torch.from_numpy(mapy).unsqueeze(2)/1080*2-1
   map_tensor = torch.cat([grid_trans_x,grid_trans_y],2).unsqueeze(0).float().cuda()
   img_perspect = gpu_remap(img_revise,map_tensor)
   cv2.imwrite('gpu_remap.jpg',img_perspect)
if __name__=='__main__':
    main()

还是那个问题,数据在cpu->GPU拷贝时会慢,单GPU的计算耗时很短

相关推荐
Good kid.7 小时前
不用自建代理,国内直连 Gemini API:Aisoui 接入指南与定价说明
人工智能·ai·gemini
AmberSoulAnswersAI占星7 小时前
# 为什么AI本命盘报告很难做好——Soul Answers的解题思路
人工智能
jkyy20147 小时前
AI赋能业态革新,助力大健康新零售开启智能化新赛道
人工智能·零售
阿瑞IT7 小时前
AI Agent 从 Demo 到生产:被低估的四个工程问题
人工智能
小锋java12347 小时前
【技术专题】LangChain4j 开发Java Agent智能体 - 整合SpringBoot4
java·人工智能
MemOS7 小时前
产品更新 |MemOS CLI 上线:让能跑命令行的 Agent 更轻量接入长期记忆
人工智能
独秀不如众秀7 小时前
AI Coding 的最大问题不是写错代码,而是反复犯同一个错
人工智能
CV实验室7 小时前
Remote Sensing 29个SITS基准数据集综述:多模态遥感分类的新起点
人工智能·深度学习·计算机视觉·音视频
m0_466525298 小时前
锚定场景深耕数据 东软探索城市全域数字化新路径
大数据·人工智能
Data-Miner8 小时前
智慧城市数据中台建设方案深度解析PPT解读
人工智能·智慧城市