deconv2d.py

 


#  ------------------------------------------------------------------

#  Author: Michael Wang

#  Email: loveu1061@126.com

#  Affiliation: ShenZhen

#  Date: 14 Oct 2020

#  ------------------------------------------------------------------

import torch
import torch.nn as nn
import cv2


class ImageDeconv(nn.Module):
    def __init__(self, in_channel, out_channel):
            super().__init__()
            self.conv = nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=2, padding=0)
            self.deconv = nn.ConvTranspose2d(in_channel, out_channel, kernel_size=3, stride=2, padding=0)
            #self.add = nn.Add()


    def forward(self, x1, x2):
        y1 = self.conv(x1)
        z1 = self.conv(y1)
        y2 = self.conv(x2)
        z2 = self.conv(y2)

        y = (y1 + y2)/2
        z = (z1 + z2)/2

        y = self.deconv(y)
        return y 


# 3 4024 * 3036  Image1.bmp  Image2.bmp
img1 = cv2.imread('Image1.bmp')
img2 = cv2.imread('Image2.bmp')

I1 = img1
I2 = img2

#img1 = img1 / 255   # [0, 1]
#img2 = img2 / 255   # [0, 1]
img1 = (img1 - 127.5) / 128.0    # [-1, 1]
img2 = (img2 - 127.5) / 128.0    # [-1, 1]

#img1 = torch.from_numpy(img1)
#img2 = torch.from_numpy(img2)
img1 = torch.tensor(img1, dtype=torch.float32) #数据转换
img2 = torch.tensor(img2, dtype=torch.float32) #数据转换

img1 = img1.permute(2,1,0)
img2 = img2.permute(2,1,0)
img1 = img1.unsqueeze(0)
img2 = img2.unsqueeze(0)

ImaheDe = ImageDeconv(3, 3)
frame = ImaheDe.forward(img1, img2)

frame = frame.squeeze(0)
frame = frame.permute(2,1,0)
frame = frame.detach().numpy()

frame = frame* 128.0 + 127.5

cv2.imwrite('frame.bmp', frame)

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值