当前位置:网站首页>pytorch bilinear interpolation
pytorch bilinear interpolation
2022-07-31 00:14:00 【fksfdh】
1、单线性插值
化简得:
重要公式
将yTreated as a function of pixel values;
2、双线性插值
问题:求P点的像素值?
According to the unilinear interpolation formula:
1、得到R1和R2点的像素值:
2、然后通过R1和R2线性插值得到P点的像素值:
所以,A total of cubic unilinear interpolation is used,final pixel value.
另外,Which is due to the difference between adjacent pixels1,所以y2 - y1 = 1 ,和x2-x1 = 1,So the denominator is 1.
最终得到的计算公式为:
3、最近邻法
使用下面公式,Find the nearest pixel value
其中:
存在问题:右偏移
The original formula is offset right,The new formula centers on it.
Because it is offset right in the original formula,So use the center point coincidence to eliminate it.
Below is the optimization formula:
4、Simple implementation of bilinear interpolation
Find by nearest neighborP点,Then you need to find four adjacent pixels.
通过floorThe function finds the lower bound,floor +1 find the upper limit,But to prevent exceeding the pixel coordinate value of the image
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
def Bilinear(dst,des_w,des_h,src_w,src_h):
for c in range(3):
for dst_x in range(des_w):
for dst_y in range(des_h):
src_x = (dst_x + 0.5)*src_w/des_w - 0.5
src_y = (dst_y + 0.5)*src_h/des_h - 0.5
#Four proximity points
src_x_1 = int(np.floor(src_x))
src_y_1 = int(np.floor(src_y))
src_x_2 = min(src_x_1 + 1,src_w -1)
src_y_2 = min(src_y_1 + 1,src_h -1)
R1 = (src_x_2 - src_x) * src[src_y_1,src_x_1,c] + (src_x - src_x_1) * src[src_y_1,src_x_2,c]
R2 = (src_x_2 - src_x) * src[src_y_2,src_x_1,c] + (src_x - src_x_1) * src[src_y_2,src_x_2,c]
P = int((src_y_2 - src_y) * R1 + (src_y - src_y_1) * R2)
dst[dst_y, dst_x, c] = P
return dst
def show_img(dst):
dst = dst.astype(np.uint8)
plt.figure()
plt.subplot(121)
plt.imshow(src)
plt.subplot(122)
plt.imshow(dst)
# plt.imsave("./img.png",dst)
plt.show()
if __name__ == '__main__':
src = Image.open("./img_1.png")
src_w = src.width
src_h = src.height
src = np.array(src)
dst = np.ones((960, 1280, 3))
des_w = dst.shape[1]
des_h = dst.shape[0]
# print(des_w,des_h)
dst = Bilinear(dst,des_w,des_h,src_w,src_h)
show_img(dst)
5、pytorch中双线性插值
import torch
from torch.nn import functional as F
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
img = Image.open("./img.png")
img = np.array(img,dtype=float)
print(img.shape)
img = torch.from_numpy(img)
print(img.shape)
img = img.unsqueeze(0).permute(0,3,1,2) #[b,c,h,w]
img = F.interpolate(img,scale_factor=(2,2),mode='bilinear')
# print(img.shape)
img = img.squeeze(0).permute(1,2,0)
print(img.shape)
a = torch.tensor(img, dtype=torch.uint8)
print(a.shape)
plt.figure()
plt.imshow(a)
plt.show()
边栏推荐
- .NET Cross-Platform Application Development Hands-on Tutorial | Build a Kanban-style Todo App with Uno Platform
- 2D Transform Module && Media Queries
- leetcode 406. Queue Reconstruction by Height 根据身高重建队列(中等)
- 【VisDrone数据集】YOLOV4训练VisDrone数据集步骤与结果
- 游戏商城表建立
- 状态机动态规划之股票问题总结
- firewalld
- 从笔试包装类型的11个常见判断是否相等的例子理解:包装类型、自动装箱与拆箱的原理、装箱拆箱的发生时机、包装类型的常量池技术
- HCIP第十五天笔记
- A Brief Talk About MPI
猜你喜欢
Flex布局使用
How to adjust Chinese in joiplay simulator
image里的mode属性
2022 China Logistics Industry Conference and Entrepreneur Summit Forum will be held in Hangzhou!
雪佛兰开拓者,安全保障温暖你的家庭出行的第一选择
joiplay模拟器不支持此游戏类型怎么解决
从两个易错的笔试题深入理解自增运算符
Chevrolet Trailblazer, the first choice for safety and warmth for your family travel
[0x800706D9] solution appears in Microsoft Store
xss绕过:prompt(1)
随机推荐
常用的正则表达式
一款好用的接口测试工具——Postman
How to solve the error of joiplay simulator
Soft Exam Study Plan
Dry goods | 4 tips for MySQL performance optimization
How to ensure the consistency of database and cache data?
joiplay模拟器如何导入游戏存档
数据清洗-使用es的ingest
Shell script if statement
状态机动态规划之股票问题总结
C# VSCode & Rider引用命名空间快捷键
"Wei cup" school more than 2022 cattle summer camp 4 Nancy (polocy) pelosi article variance law of Arts
ctfshow 文件包含
software development design process
实验8(vlan实验)
游戏商城表建立
pytorch的安装注意事项
DATA AI Summit 2022提及到的对 aggregate 的优化
(五)fastai应用
uni-ui installation