在缺失值填補(bǔ)上如果用前后的均值填補(bǔ)中間的均值, 比如,0,空,1, 我們希望中間填充0.5;或者0,空,空,1,我們希望中間填充0.33,0.67這樣。

十年的天津網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。網(wǎng)絡(luò)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整天津建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)建站從事“天津網(wǎng)站設(shè)計(jì)”,“天津網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
可以用pandas的函數(shù)進(jìn)行填充,因?yàn)檫@個(gè)就是線性插值法
df..interpolate()
dd=pd.DataFrame(data=[0,np.nan,np.nan,1])
dd.interpolate()
補(bǔ)充知識:線性插值公式簡單推導(dǎo)
以上這篇python線性插值解析就是我分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持。
不知道有沒有,可能python數(shù)學(xué)相關(guān)的庫里會有吧
不過你寫的也不對啊,取3個(gè)值,應(yīng)該是4均分。
def?junfen(start,end,num):
k?=?(end?-?start)/(num?+?1)
return?set([start?+?item?*?k?for?item?in?range(1,num?+?1)])
碼字不易,如果此文對你有所幫助,請幫忙點(diǎn)贊,感謝!
一. 雙線性插值法原理:
? ? ① 何為線性插值?
? ? 插值就是在兩個(gè)數(shù)之間插入一個(gè)數(shù),線性插值原理圖如下:
? ? ② 各種插值法:
? ? 插值法的第一步都是相同的,計(jì)算目標(biāo)圖(dstImage)的坐標(biāo)點(diǎn)對應(yīng)原圖(srcImage)中哪個(gè)坐標(biāo)點(diǎn)來填充,計(jì)算公式為:
? ? srcX = dstX * (srcWidth/dstWidth)
? ? srcY = dstY * (srcHeight/dstHeight)
? ? (dstX,dstY)表示目標(biāo)圖像的某個(gè)坐標(biāo)點(diǎn),(srcX,srcY)表示與之對應(yīng)的原圖像的坐標(biāo)點(diǎn)。srcWidth/dstWidth 和 srcHeight/dstHeight 分別表示寬和高的放縮比。
? ? 那么問題來了,通過這個(gè)公式算出來的 srcX, scrY 有可能是小數(shù),但是原圖像坐標(biāo)點(diǎn)是不存在小數(shù)的,都是整數(shù),得想辦法把它轉(zhuǎn)換成整數(shù)才行。
不同插值法的區(qū)別就體現(xiàn)在 srcX, scrY 是小數(shù)時(shí),怎么將其變成整數(shù)去取原圖像中的像素值。
最近鄰插值(Nearest-neighborInterpolation):看名字就很直白,四舍五入選取最接近的整數(shù)。這樣的做法會導(dǎo)致像素變化不連續(xù),在目標(biāo)圖像中產(chǎn)生鋸齒邊緣。
雙線性插值(Bilinear Interpolation):雙線性就是利用與坐標(biāo)軸平行的兩條直線去把小數(shù)坐標(biāo)分解到相鄰的四個(gè)整數(shù)坐標(biāo)點(diǎn)。權(quán)重與距離成反比。
? ??雙三次插值(Bicubic Interpolation):與雙線性插值類似,只不過用了相鄰的16個(gè)點(diǎn)。但是需要注意的是,前面兩種方法能保證兩個(gè)方向的坐標(biāo)權(quán)重和為1,但是雙三次插值不能保證這點(diǎn),所以可能出現(xiàn)像素值越界的情況,需要截?cái)唷?/p>
? ? ③ 雙線性插值算法原理
假如我們想得到未知函數(shù) f 在點(diǎn) P = (x, y) 的值,假設(shè)我們已知函數(shù) f 在 Q11 = (x1, y1)、Q12 = (x1, y2), Q21 = (x2, y1) 以及 Q22 = (x2, y2) 四個(gè)點(diǎn)的值。最常見的情況,f就是一個(gè)像素點(diǎn)的像素值。首先在 x 方向進(jìn)行線性插值,然后再在 y 方向上進(jìn)行線性插值,最終得到雙線性插值的結(jié)果。
④ 舉例說明
二. python實(shí)現(xiàn)灰度圖像雙線性插值算法:
灰度圖像雙線性插值放大縮小
import numpy as np
import math
import cv2
def double_linear(input_signal, zoom_multiples):
'''
雙線性插值
:param input_signal: 輸入圖像
:param zoom_multiples: 放大倍數(shù)
:return: 雙線性插值后的圖像
'''
input_signal_cp = np.copy(input_signal)? # 輸入圖像的副本
input_row, input_col = input_signal_cp.shape # 輸入圖像的尺寸(行、列)
# 輸出圖像的尺寸
output_row = int(input_row * zoom_multiples)
output_col = int(input_col * zoom_multiples)
output_signal = np.zeros((output_row, output_col)) # 輸出圖片
for i in range(output_row):
? ? for j in range(output_col):
? ? ? ? # 輸出圖片中坐標(biāo) (i,j)對應(yīng)至輸入圖片中的最近的四個(gè)點(diǎn)點(diǎn)(x1,y1)(x2, y2),(x3, y3),(x4,y4)的均值
? ? ? ? temp_x = i / output_row * input_row
? ? ? ? temp_y = j / output_col * input_col
? ? ? ? x1 = int(temp_x)
? ? ? ? y1 = int(temp_y)
? ? ? ? x2 = x1
? ? ? ? y2 = y1 + 1
? ? ? ? x3 = x1 + 1
? ? ? ? y3 = y1
? ? ? ? x4 = x1 + 1
? ? ? ? y4 = y1 + 1
? ? ? ? u = temp_x - x1
? ? ? ? v = temp_y - y1
? ? ? ? # 防止越界
? ? ? ? if x4 = input_row:
? ? ? ? ? ? x4 = input_row - 1
? ? ? ? ? ? x2 = x4
? ? ? ? ? ? x1 = x4 - 1
? ? ? ? ? ? x3 = x4 - 1
? ? ? ? if y4 = input_col:
? ? ? ? ? ? y4 = input_col - 1
? ? ? ? ? ? y3 = y4
? ? ? ? ? ? y1 = y4 - 1
? ? ? ? ? ? y2 = y4 - 1
? ? ? ? # 插值
? ? ? ? output_signal[i, j] = (1-u)*(1-v)*int(input_signal_cp[x1, y1]) + (1-u)*v*int(input_signal_cp[x2, y2]) + u*(1-v)*int(input_signal_cp[x3, y3]) + u*v*int(input_signal_cp[x4, y4])
return output_signal
# Read image
img = cv2.imread("../paojie_g.jpg",0).astype(np.float)
out = double_linear(img,2).astype(np.uint8)
# Save result
cv2.imshow("result", out)
cv2.imwrite("out.jpg", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
三. 灰度圖像雙線性插值實(shí)驗(yàn)結(jié)果:
四. 彩色圖像雙線性插值python實(shí)現(xiàn)
def BiLinear_interpolation(img,dstH,dstW):
scrH,scrW,_=img.shape
img=np.pad(img,((0,1),(0,1),(0,0)),'constant')
retimg=np.zeros((dstH,dstW,3),dtype=np.uint8)
for i in range(dstH-1):
? ? for j in range(dstW-1):
? ? ? ? scrx=(i+1)*(scrH/dstH)
? ? ? ? scry=(j+1)*(scrW/dstW)
? ? ? ? x=math.floor(scrx)
? ? ? ? y=math.floor(scry)
? ? ? ? u=scrx-x
? ? ? ? v=scry-y
? ? ? ? retimg[i,j]=(1-u)*(1-v)*img[x,y]+u*(1-v)*img[x+1,y]+(1-u)*v*img[x,y+1]+u*v*img[x+1,y+1]
return retimg
im_path='../paojie.jpg'
image=np.array(Image.open(im_path))
image2=BiLinear_interpolation(image,image.shape[0]*2,image.shape[1]*2)
image2=Image.fromarray(image2.astype('uint8')).convert('RGB')
image2.save('3.png')
五. 彩色圖像雙線性插值實(shí)驗(yàn)結(jié)果:
六. 最近鄰插值算法和雙三次插值算法可參考:
① 最近鄰插值算法:
???
? ? ② 雙三次插值算法:
七. 參考內(nèi)容:
? ??
???
本文標(biāo)題:python線性插值函數(shù) 線性插值法函數(shù)
標(biāo)題網(wǎng)址:http://chinadenli.net/article38/hijcpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、Google、網(wǎng)頁設(shè)計(jì)公司、營銷型網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)