python 图像插值 最近邻、双线性、双三次实例


Posted in Python onJuly 05, 2020

最近邻:

import cv2
import numpy as np
def function(img):
 height,width,channels =img.shape
 emptyImage=np.zeros((2048,2048,channels),np.uint8)
 sh=2048/height
 sw=2048/width
 for i in range(2048):
  for j in range(2048):
   x=int(i/sh)
   y=int(j/sw)
   emptyImage[i,j]=img[x,y]
 return emptyImage
 
img=cv2.imread("e:\\lena.bmp")
zoom=function(img)
cv2.imshow("nearest neighbor",zoom)
cv2.imshow("image",img)
cv2.waitKey(0)

双线性:

import cv2
import numpy as np
import math
def function(img,m,n):
 height,width,channels =img.shape
 emptyImage=np.zeros((m,n,channels),np.uint8)
 value=[0,0,0]
 sh=m/height
 sw=n/width
 for i in range(m):
  for j in range(n):
   x = i/sh
   y = j/sw
   p=(i+0.0)/sh-x
   q=(j+0.0)/sw-y
   x=int(x)-1
   y=int(y)-1
   for k in range(3):
    if x+1<m and y+1<n:
     value[k]=int(img[x,y][k]*(1-p)*(1-q)+img[x,y+1][k]*q*(1-p)+img[x+1,y][k]*(1-q)*p+img[x+1,y+1][k]*p*q)
   emptyImage[i, j] = (value[0], value[1], value[2])
 return emptyImage
 
img=cv2.imread("e:\\lena.bmp")
zoom=function(img,2048,2048)
cv2.imshow("Bilinear Interpolation",zoom)
cv2.imshow("image",img)
cv2.waitKey(0)

双三次:

import cv2
import numpy as np
import math
 
def S(x):
 x = np.abs(x)
 if 0 <= x < 1:
  return 1 - 2 * x * x + x * x * x
 if 1 <= x < 2:
  return 4 - 8 * x + 5 * x * x - x * x * x
 else:
  return 0
def function(img,m,n):
 height,width,channels =img.shape
 emptyImage=np.zeros((m,n,channels),np.uint8)
 sh=m/height
 sw=n/width
 for i in range(m):
  for j in range(n):
   x = i/sh
   y = j/sw
   p=(i+0.0)/sh-x
   q=(j+0.0)/sw-y
   x=int(x)-2
   y=int(y)-2
   A = np.array([
    [S(1 + p), S(p), S(1 - p), S(2 - p)]
   ])
   if x>=m-3:
    m-1
   if y>=n-3:
    n-1
   if x>=1 and x<=(m-3) and y>=1 and y<=(n-3):
    B = np.array([
     [img[x-1, y-1], img[x-1, y],
      img[x-1, y+1],
      img[x-1, y+1]],
     [img[x, y-1], img[x, y],
      img[x, y+1], img[x, y+2]],
     [img[x+1, y-1], img[x+1, y],
      img[x+1, y+1], img[x+1, y+2]],
     [img[x+2, y-1], img[x+2, y],
      img[x+2, y+1], img[x+2, y+1]],
 
     ])
    C = np.array([
     [S(1 + q)],
     [S(q)],
     [S(1 - q)],
     [S(2 - q)]
    ])
    blue = np.dot(np.dot(A, B[:, :, 0]), C)[0, 0]
    green = np.dot(np.dot(A, B[:, :, 1]), C)[0, 0]
    red = np.dot(np.dot(A, B[:, :, 2]), C)[0, 0]
 
    # ajust the value to be in [0,255]
    def adjust(value):
     if value > 255:
      value = 255
     elif value < 0:
      value = 0
     return value
 
    blue = adjust(blue)
    green = adjust(green)
    red = adjust(red)
    emptyImage[i, j] = np.array([blue, green, red], dtype=np.uint8)
 
 return emptyImage
 
img=cv2.imread("e:\\lena.bmp")
zoom=function(img,1024,1024)
cv2.imshow("cubic",zoom)
cv2.imshow("image",img)
cv2.waitKey(0)

补充知识:最邻近插值法(The nearest interpolation)实现图像缩放

也称零阶插值。它输出的像素灰度值就等于距离它映射到的位置最近的输入像素的灰度值。但当图像中包含像素之间灰度级有变化的细微结构时,最邻近算法会在图像中产生人为加工的痕迹。

具体计算方法:对于一个目的坐标,设为 M(x,y),通过向后映射法得到其在原始图像的对应的浮点坐标,设为 m(i+u,j+v),其中 i,j 为正整数,u,v 为大于零小于1的小数(下同),则待求象素灰度的值 f(m)。利用浮点 m 相邻的四个像素求f(m)的值。

function re_im = nearest(im, p, q)
%最邻近插值法,输入目标图像和行缩放、纵缩放倍数
%ziheng 2016.3.27
[m,n] = size(im);
im_R = im(:,:,1);
im_G = im(:,:,2);
im_B = im(:,:,3);
l = round(m*p);
h = round(n*q)/3;
re_R = uint8(zeros(l,h));
re_G = uint8(zeros(l,h));
re_B = uint8(zeros(l,h));
for dstx = 1:l
 for dsty = 1:h
   srcx = max(1,min(m,round(dstx/p)));
   srcy = max(1,min(n/3,round(dsty/q)));
   re_R(dstx,dsty) = im_R(srcx,srcy);
   re_G(dstx,dsty) = im_G(srcx,srcy);
   re_B(dstx,dsty) = im_B(srcx,srcy);
 end
end
re_im = cat(3,re_R,re_G,re_B);
figure,imshow(re_im);

以上这篇python 图像插值 最近邻、双线性、双三次实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
在Python中利用Into包整洁地进行数据迁移的教程
Mar 30 Python
Python的自动化部署模块Fabric的安装及使用指南
Jan 19 Python
使用Python读写文本文件及编写简单的文本编辑器
Mar 11 Python
web.py 十分钟创建简易博客实现代码
Apr 22 Python
Python操作Redis之设置key的过期时间实例代码
Jan 25 Python
使用Python从零开始撸一个区块链
Mar 14 Python
Python使用requests提交HTTP表单的方法
Dec 26 Python
python文件写入write()的操作
May 14 Python
Python3获取cookie常用三种方案
Oct 05 Python
Django配置Bootstrap, js实现过程详解
Oct 13 Python
Pytest之测试命名规则的使用
Apr 16 Python
Python中的程序流程控制语句
Feb 24 Python
python cv2.resize函数high和width注意事项说明
Jul 05 #Python
Python中flatten( ),matrix.A用法说明
Jul 05 #Python
python线性插值解析
Jul 05 #Python
使用keras实现非线性回归(两种加激活函数的方式)
Jul 05 #Python
Keras 中Leaky ReLU等高级激活函数的用法
Jul 05 #Python
Django --Xadmin 判断登录者身份实例
Jul 03 #Python
详解Python多线程下的list
Jul 03 #Python
You might like
php $_ENV为空的原因分析
2009/06/01 PHP
php _autoload自动加载类与机制分析
2012/02/10 PHP
php开发工具有哪五款
2015/11/09 PHP
PHP扩展框架之Yaf框架的安装与使用
2016/05/18 PHP
laravel框架的安装与路由实例分析
2019/10/11 PHP
Vagrant(WSL)+PHPStorm+Xdebu 断点调试环境搭建
2019/12/13 PHP
JQuery 浮动导航栏实现代码
2009/08/27 Javascript
JS动态修改iframe内嵌网页地址的方法
2015/04/01 Javascript
Highcharts使用简例及异步动态读取数据
2015/12/30 Javascript
jQuery 利用$.ajax 时获取原生XMLHttpRequest 对象的方法
2016/08/25 Javascript
React Router基础使用
2017/01/17 Javascript
简单实现js选项卡切换效果
2017/02/09 Javascript
Bootstrap table 定制提示语的加载过程
2017/02/20 Javascript
详解nodeJS中读写文件方法的区别
2017/03/06 NodeJs
详解Angular 4.x Injector
2017/05/04 Javascript
JavaScript常用数学函数用法示例
2018/05/14 Javascript
vue项目打包后打开页面空白解决办法
2018/06/29 Javascript
JS数组reduce()方法原理及使用技巧解析
2020/07/14 Javascript
[51:00]Secret vs VGJ.S 2018国际邀请赛淘汰赛BO3 第一场 8.24
2018/08/25 DOTA
python中使用OpenCV进行人脸检测的例子
2014/04/18 Python
PYQT5实现控制台显示功能的方法
2019/06/25 Python
python实现键盘输入的实操方法
2019/07/16 Python
决策树剪枝算法的python实现方法详解
2019/09/18 Python
python GUI库图形界面开发之PyQt5动态(可拖动控件大小)布局控件QSplitter详细使用方法与实例
2020/03/06 Python
林清轩官方网站:山茶花润肤油开创者
2016/10/26 全球购物
美国亚洲时尚和美容产品的一站式网上商店:Stylevana
2019/09/05 全球购物
最新的咖啡店创业计划书
2013/12/30 职场文书
优秀党员主要事迹
2014/01/19 职场文书
家长对孩子的感言
2014/03/10 职场文书
股份合作协议书
2014/04/12 职场文书
2014民事授权委托书范本
2014/09/29 职场文书
基层党支部承诺书
2015/04/30 职场文书
2016教师校本培训心得体会
2016/01/08 职场文书
转变工作作风心得体会
2016/01/23 职场文书
导游词之吉林花园山
2019/10/17 职场文书
win10忘记pin密码登录不了怎么办?win10忘记pin密码登不进去的解决方法
2022/07/07 数码科技