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

所属分类: 脚本专栏 / python 阅读数: 592
收藏 0 赞 0 分享

最近邻:

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 图像插值 最近邻、双线性、双三次实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

pandas的qcut()方法详解

这篇文章主要介绍了pandas的qcut()方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

从列表或字典创建Pandas的DataFrame对象的方法

这篇文章主要介绍了从列表或字典创建Pandas的DataFrame对象的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas.DataFrame的pivot()和unstack()实现行转列

这篇文章主要介绍了pandas.DataFrame的pivot()和unstack()实现行转列,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

pandas中的series数据类型详解

这篇文章主要介绍了pandas中的series数据类型详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas 时间格式转换的实现

这篇文章主要介绍了pandas 时间格式转换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python中时间、日期、时间戳的转换的实现方法

这篇文章主要介绍了python中时间、日期、时间戳的转换的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas进行时间数据的转换和计算时间差并提取年月日

这篇文章主要介绍了pandas进行时间数据的转换和计算时间差并提取年月日,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法

这篇文章主要介绍了详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python和c语言的主要区别总结

在本篇文章里小编给各位整理了关于python和c语言的主要区别的相关知识帖内容,有需要的朋友们学习阅读下。
收藏 0 赞 0 分享

选择Python写网络爬虫的优势和理由

在本篇文章里小编给各位整理了一篇关于选择Python写网络爬虫的优势和理由以及相关代码实例,有兴趣的朋友们阅读下吧。
收藏 0 赞 0 分享
查看更多