- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试对一些成像数据进行二维表面拟合。我附上了此类数据的示例,它基本上是一个 1014 x 1014 数组,具有大量噪声。 Example_image 。该数组的一些补丁是无效数据,我将其屏蔽并设置为 NaN 值,如示例图像中的黄色所示。正如您在图像中看到的,有一个从左(较亮)到右(较暗)的背景渐变,我正在尝试将其删除。多项式无法很好地拟合梯度,因此我的目标是进行二维表面二元样条拟合,并减去梯度。
我在 scipy 中尝试了许多任务,但大多数都没有返回理想的结果。
首先,我尝试了 [RectBivariateSpline] Bivariate structured interpolation of large array with NaN values or mask ),但由于我的图像中含有 NaN,因此运行 RectBivariateSpline 仅给出 NaN 的输出。
我也尝试过SmoothBivariateSpline ,这是任务的不规则网格版本。我省略了那些具有 NaN 值的像素,并将其余像素转换为一维数组作为输入。但由于数组大小太大而失败。然后我尝试切碎我的数组以尝试在较小的 block 上运行它,但它给出了以下错误并因段错误而退出,我不知道这意味着什么。
fitpack2.py:1044: 用户警告:输入错误,未返回近似值。满足以下条件必须持有:xb<=x[i]<=xe, yb<=y[i]<=ye, w[i]>0, i=0..m-1如果 iopt==-1,那么xb
然后,我尝试首先使用带有线性插值的 griddata 用值填充图像中的 NaN 补丁。由于补丁很大,插值并不理想,但至少它给了我一个没有 NaN 的数组。然后,我使用该数组再次运行 RectBivariateSpline。但输出数组仍然是 NaN。
我怀疑图像中的噪声破坏了这两个任务的行为,因此我还尝试首先在图像上运行高斯核以使其平滑,然后用网格数据填充 NaN 补丁,然后运行 RectBivariateSpline 或 SmoothBivariateSpline,但它们仍然为我提供带有 NaN 值的数组作为输出。
我不确定我是否正确理解了这两个任务的手册,因此我附上了以下脚本:
#!/usr/bin/python
import matplotlib
matplotlib.use('qt5agg')
#matplotlib.rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
#matplotlib.rc('text.latex', preamble=r'\usepackage{cmbright}')
#matplotlib.rc('text.latex', preamble=r'\usepackage[scaled]{helvet} \renewcommand\familydefault{\sfdefault} \usepackage[T1]{fontenc}')
#matplotlib.rc('text', usetex=True)
import matplotlib.pyplot as plt
import numpy as np
import astropy.io.fits as pyfits
import scipy.interpolate as sp
from astropy.convolution import convolve
from astropy.convolution import Gaussian2DKernel
#------------------------------------------------------------
#Read in the arrays
hdulistorg = pyfits.open('icmj01jrq_flt.fits')
hdulistorg.info()
errarrorg = np.swapaxes(hdulistorg[1].data, 0,1)
hdulist = pyfits.open('jrq_sci_nan_deep.fits')
hdulist.info()
dataarrorg = np.swapaxes(hdulist[0].data, 0,1) #image array
errarrorg = np.swapaxes(hdulistorg[1].data, 0,1) #error array
#Flag some of the problematic values, turn NaNs into 0 for easier handling
dataarr = np.copy(dataarrorg)
w=np.isnan(dataarr)
ww=np.where(dataarr == 0)
www=np.where(dataarr > 100)
wwww=np.where(dataarr < 0)
errarr = 1.0 / (np.copy(errarrorg)+1e-5) # Try to use 1/error as the estimate for weight below
errarr[w] = 0
errarr[ww] = 0
errarr[www] = 0
errarr[wwww]=0
dataarr[w]= 0
dataarr[ww]= 0
dataarr[www]=0
dataarr[wwww]=0
#Make a gaussian kernel smoothed data
maskarr = np.copy(errarr) #For masking the nan regions so they dun get smoothed
maskarr[:]=0
maskarr[w]=1
maskarr[ww]=1
maskarr[www]=1
maskarr[wwww]=1
gauss = Gaussian2DKernel(stddev=5)
condataarr = convolve(dataarr,gauss,normalize_kernel=True,boundary='extend',mask=maskarr)
condataarr[w]=0
conerrarr = np.copy(errarr)
#Setting x,y arrays for the Spline functions
nx, ny = (1014,1014)
x = np.linspace(0, 1013, nx)
y = np.linspace(0, 1013, ny)
xv, yv = np.meshgrid(x, y)
#Make an 1D version of these 2D arrays
dataarrflat = np.ravel(condataarr[0:200,0:200]) #Try only a small chunk!
xvflat = np.ravel(xv[0:200,0:200])
yvflat = np.ravel(yv[0:200,0:200])
errarrflat = np.ravel(conerrarr[0:200,0:200])
notnanloc = np.where(dataarrflat != 0) #Not NaNs
#SmoothBivariateSpline!
rect_S_spline = sp.SmoothBivariateSpline(xvflat[notnanloc], yvflat[notnanloc], dataarrflat[notnanloc],w=errarrflat[notnanloc], kx=3, ky=3)
#Also try using grid data to fix the grid?
gddataarr = np.copy(condataarr)
gddataarrflat = np.ravel(gddataarr)
gdloc = np.where(gddataarrflat != 0) #Not NaNs
gdxvflat = np.ravel(xv)
gdyvflat = np.ravel(yv)
xyarr = np.c_[gdxvflat[gdloc],gdyvflat[gdloc]]
x_grid, y_grid = np.mgrid[0:1013:1014j,0:1013:1014j]
grid_z2 = sp.griddata(xyarr, gddataarrflat[gdloc], (x_grid, y_grid), method='linear')
plt.imshow(grid_z2.T)
#plt.show()
#RectBivariatSpline
rect_B_spline = sp.RectBivariateSpline(x, y, grid_z2.T)
#Result grid (same as input for now)
xnew = np.arange(0, 1013, 1)
ynew = np.arange(0, 1013, 1)
znewS = rect_S_spline(xnew, ynew)
znewB = rect_B_spline(xnew, ynew)
print 'znewS', znewS
print 'znewB', znewB
#Write FITS files
condataarr = np.swapaxes(condataarr, 0, 1)
hdu2 = pyfits.PrimaryHDU(condataarr)
hdulist2 = pyfits.HDUList([hdu2])
hdulist2.writeto('contest.fits',overwrite=True)
hdulist2.close()
hdu3 = pyfits.PrimaryHDU(znewS)
hdulist3 = pyfits.HDUList([hdu3])
hdulist3.writeto('Stest.fits',overwrite=True)
hdulist3.close()
最佳答案
我不能完全解决你的问题,但我有一些代码将 FORTRAN 插值例程与 python 连接起来。您可以直接从 python 调用例程,不需要 fortran。
您可以在此 github 页面找到代码及其描述 https://github.com/haakoan/inter
关于python - 使用 scipy 的 RectBivariateSpline 和 SmoothBivariateSpline 对噪声数据进行样条曲面拟合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42572743/
在航空工程中,跑道平面以上的高度 (Z) 与: 从起飞开始的下降距离 (X) 满载飞机使用的跑道长度 (Y)。 我们希望从已知 x 和 y 值的观察点 z 的数组中找到 z=f(x,y)。 z 在 x
我正在尝试为图形设置表面 View ,但是仍然出现错误,表明我已经必须导入SView了。我尝试的一切似乎都无法正常工作。以下代码中缺少任何内容吗?还是我忘了添加一些东西。 Firstclass.jav
我有一个由以下内容生成的数据结构(其中 [a] func1 和 func2 是占位符函数,[b] i1 等是占位符变量值): def func1(x,y,z): return x+y/z de
我有一个 NURBS surface它有 4 个弯曲的边缘。 (我有曲线的 4 个贝塞尔点) 我想用轴上的切片平面(不如 3DSMAX 先进!)对 NURBS 曲面进行切片,并计算切片平面和 NURB
我正在实现 NURBS 曲面。我想要的只是在每次鼠标单击时 Y 轴上都有递减,所以它看起来像是有太阳或其他行星的重量。 #include #include int PI = 3.145; int
我在 Abaqus 中编写脚本,在那里我用切圆(如奶酪)粉碎圆形和正方形。我需要在部件之间放置 Contact,所以我需要 Surface。 宏管理器生成: s1 = a.instances['kol
我正在尝试使用 Python 和 Matplotlib 渲染由 给出的多面体的 3D 表面 但是我的代码(如下所示)似乎没有正确绘制它。应该怎么做呢? 尝试失败: %matplotlib inline
我想绘制一个 3d 曲面,其参数形式为: x = (-2 * (s^2 - t)^ 3) y = log(t*(t-2*s^2)+2*s^2) z = 1/(s^2-t) 其中 s、t 是参数。我使
关于从Python中的点云进行3D网格重建的问题,有几个主题。通常,当X,Y,Z坐标可用并且不存在体积表示时,通过激光扫描获取。我已经尝试了许多库: matplotlib, mayavi, open3
我必须编写一个 C++ 程序,根据未组织的点列表,计算这些点所在的近似曲面并生成相应的 NURBS。我已经看过 OpenNurbs,但没有这方面的功能,而 libNURBS 但包含的功能被标记为“研究
我有以下问题: 下面是我在屏幕上绘制立方体的方法: void drawCube() { //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
我一直在研究地形 LOD 算法,但主要逻辑在 CPU 上: 我尝试将大部分逻辑转换为 opengl 管道的曲面 segmentation 控制和评估阶段,但没有任何显示: 我将代码缩减为基本的“hel
我正在学习 OpenGL,我想要一个中间有轻微隆起的表面。我目前正在使用这段代码,但我不确定如何调整 ctrl 点以使其成为我想要的方式。它目前像 我希望它是这样的: 我不完全确定我应该使用哪些控制点
我在这里使用标准的 matplotlib surfaceplot 作为示例。 from mpl_toolkits.mplot3d import axes3d import matplotlib.pyp
我想使用 R 包 rgl 绘制相交曲面。我有一个 3D 数据数组,在恒定“z”(数组 [,,n])处绘制曲面没有问题。但是,我不知道如何在同一个 rgl 图上绘制垂直面 (array[n,,])。 示
我正在创建一个建模软件。我的模型都是由平面多边形组成的,它们只是我用 OpenGL 显示的一组有序顶点。我已经进行了大量搜索,但令我惊讶的是,我没有找到与我正在寻找的应用程序相关的太多信息。 我正在尝
我使用了示例 here将我的镶嵌回调移动到不同的类。 代码编译,但回调代码永远不会执行。 回调类: template class SingularCallBack { public: typ
我有兴趣寻找软件来计算 3D 多面体的精确中轴(或您选择的术语!)。我想输出将是一个三角形面的列表,可能是边和顶点,代表内侧表面的连接点、薄片和接缝。理想情况下,我想要可以处理所有多面体的东西,包括凹
我想在 linux (fedora 12) 下使用 GTK 3 创建一个窗口,并使用 cairo-gl 后端在其上绘制一个简单的矩形,为此我想创建一个 cairo-gl 表面。我该怎么做,任何人都可以
我有 6 个点,它们都位于球体的表面,并且是八面体的顶点。我怎样才能在 3d 轴上探索球体内的这个八面体的表面? 我有以下代码,但它没有达到我的预期: from mpl_toolkits.mplot3
我是一名优秀的程序员,十分优秀!