# ------------------------------------------------------------------
# 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)