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去除文件中空格、Tab及回车的方法
Apr 12 Python
python2.7的编码问题与解决方法
Oct 04 Python
python访问抓取网页常用命令总结
Apr 11 Python
python中map()函数的使用方法示例
Sep 29 Python
运动检测ViBe算法python实现代码
Jan 09 Python
python使用Tesseract库识别验证
Mar 21 Python
python查找指定文件夹下所有文件并按修改时间倒序排列的方法
Oct 21 Python
python实现一个简单的ping工具方法
Jan 31 Python
python利用多种方式来统计词频(单词个数)
May 27 Python
python 办公自动化——基于pyqt5和openpyxl统计符合要求的名单
May 25 Python
Python GUI编程之tkinter 关于 ttkbootstrap 的使用详解
Mar 03 Python
浅谈Python中对象是如何被调用的
Apr 06 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
Notice: Undefined index: page in E:\PHP\test.php on line 14
2010/11/02 PHP
PHP中func_get_args(),func_get_arg(),func_num_args()的区别
2013/09/30 PHP
PHP面向对象之里氏替换原则简单示例
2018/04/08 PHP
JavaScript基本对象
2007/01/11 Javascript
javascript代码加载优化方法
2011/01/30 Javascript
jQuery ajax serialize()方法的使用以及常见问题解决
2013/01/27 Javascript
jQuery实现用户注册的表单验证示例
2013/08/28 Javascript
jquery使用append(content)方法注意事项分享
2014/01/06 Javascript
jQuery实现鼠标可拖动调整表格列宽度
2014/05/26 Javascript
jquery动态切换背景图片的简单实现方法
2016/05/14 Javascript
Vue.Js中的$watch()方法总结
2017/03/23 Javascript
Javascript实现倒计时时差效果
2017/05/18 Javascript
jQuery实现 RadioButton做必选校验功能
2017/06/15 jQuery
vue.js学习之UI组件开发教程
2017/07/03 Javascript
Vue2.0子同级组件之间数据交互方法
2018/02/28 Javascript
vue.js中ref和$refs的使用及示例讲解
2019/08/14 Javascript
jquery传参及获取方式(两种方式)
2020/02/13 jQuery
[27:02]2014 DOTA2国际邀请赛中国区预选赛 5 23 CIS VS LGD第三场
2014/05/24 DOTA
跟老齐学Python之传说中的函数编写条规
2014/10/11 Python
Python中的左斜杠、右斜杠(正斜杠和反斜杠)
2016/08/30 Python
python3中获取文件当前绝对路径的两种方法
2018/04/26 Python
Python实现读写INI配置文件的方法示例
2018/06/09 Python
caffe binaryproto 与 npy相互转换的实例讲解
2018/07/09 Python
如何在Python中实现goto语句的方法
2019/05/18 Python
python xlwt如何设置单元格的自定义背景颜色
2019/09/03 Python
python ctypes库2_指定参数类型和返回类型详解
2019/11/19 Python
把vgg-face.mat权重迁移到pytorch模型示例
2019/12/27 Python
python如何将两张图片生成为全景图片
2020/03/05 Python
python矩阵运算,转置,逆运算,共轭矩阵实例
2020/05/11 Python
python 调用Google翻译接口的方法
2020/12/09 Python
精灵市场:Pixie Market
2019/06/18 全球购物
什么是类的返射机制
2016/02/06 面试题
酒店营销策划方案
2014/02/07 职场文书
中国在我心中演讲稿
2014/09/13 职场文书
工作失职自我检讨书
2015/05/05 职场文书
MySQL安装后默认自带数据库的作用详解
2021/04/27 MySQL