python 圖像插值 最近鄰、雙線性、雙三次實(shí)例
最近鄰:
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)
補(bǔ)充知識:最鄰近插值法(The nearest interpolation)實(shí)現(xiàn)圖像縮放
也稱零階插值。它輸出的像素灰度值就等于距離它映射到的位置最近的輸入像素的灰度值。但當(dāng)圖像中包含像素之間灰度級有變化的細(xì)微結(jié)構(gòu)時(shí),最鄰近算法會(huì)在圖像中產(chǎn)生人為加工的痕跡。
具體計(jì)算方法:對于一個(gè)目的坐標(biāo),設(shè)為 M(x,y),通過向后映射法得到其在原始圖像的對應(yīng)的浮點(diǎn)坐標(biāo),設(shè)為 m(i+u,j+v),其中 i,j 為正整數(shù),u,v 為大于零小于1的小數(shù)(下同),則待求象素灰度的值 f(m)。利用浮點(diǎn) m 相鄰的四個(gè)像素求f(m)的值。
function re_im = nearest(im, p, q) %最鄰近插值法,輸入目標(biāo)圖像和行縮放、縱縮放倍數(shù) %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 圖像插值 最近鄰、雙線性、雙三次實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python 類變量和實(shí)例變量的實(shí)現(xiàn)與區(qū)別(附示例)
本文主要介紹了Python 類變量和實(shí)例變量的實(shí)現(xiàn)與區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03
Flask框架運(yùn)用Ajax實(shí)現(xiàn)數(shù)據(jù)交互的示例代碼
使用Ajax技術(shù)網(wǎng)頁應(yīng)用能夠快速地將增量更新呈現(xiàn)在用戶界面上,而不需要重載刷新整個(gè)頁面,這使得程序能夠更快地回應(yīng)用戶的操作,本文將簡單介紹使用AJAX如何實(shí)現(xiàn)前后端數(shù)據(jù)通信2022-11-11
Python代碼統(tǒng)計(jì)耗時(shí)的方法詳解
在現(xiàn)代軟件開發(fā)中,性能優(yōu)化是一個(gè)至關(guān)重要的環(huán)節(jié),無論是開發(fā)大型系統(tǒng)還是小型工具,開發(fā)者都需要對代碼的執(zhí)行時(shí)間進(jìn)行精確測量,以便找出瓶頸并優(yōu)化性能,本文給大家介紹了Python代碼統(tǒng)計(jì)耗時(shí)的方法,需要的朋友可以參考下2025-02-02
pyqt5 tablewidget 利用線程動(dòng)態(tài)刷新數(shù)據(jù)的方法
今天小編就為大家分享一篇pyqt5 tablewidget 利用線程動(dòng)態(tài)刷新數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
python 刪除excel表格重復(fù)行,數(shù)據(jù)預(yù)處理操作
這篇文章主要介紹了python 刪除excel表格重復(fù)行,數(shù)據(jù)預(yù)處理操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Python 解決中文寫入Excel時(shí)拋異常的問題
下面小編就為大家分享一篇Python 解決中文寫入Excel時(shí)拋異常的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Python基于OpenCV庫Adaboost實(shí)現(xiàn)人臉識別功能詳解
這篇文章主要介紹了Python基于OpenCV庫Adaboost實(shí)現(xiàn)人臉識別功能,結(jié)合實(shí)例形式分析了Python下載與安裝OpenCV庫及相關(guān)人臉識別操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-08-08

