Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在两个aribtray点上切片图像_Python_Image_Plot_2d_Slice - Fatal编程技术网

Python 在两个aribtray点上切片图像

Python 在两个aribtray点上切片图像,python,image,plot,2d,slice,Python,Image,Plot,2d,Slice,我无法使用坐标作为起始点和停止点来分割图像。到目前为止,我有以下代码 hdulist = fits.open(filename) hdr= hdulist[0].header import numpy as np import scipy import matplotlib.pyplot as plt from matplotlib.pyplot import figure, show from astropy.io import fits from scipy import interpo

我无法使用坐标作为起始点和停止点来分割图像。到目前为止,我有以下代码

hdulist = fits.open(filename)
hdr= hdulist[0].header 

import numpy as np
import scipy
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure, show
from astropy.io import fits
from scipy import interpolate

data=hdulist[0].data

#Make a line with "num" points
D, B = input('Enter the coordinates of the starting point:').split(',')
E, C = input("Enter the coordinates of the stopping point: ").split(',')
x0= float(D)
x1= float(E)
y0= float(B)
y1= float(C)

x = np.arange(data.shape[1])
y = np.arange(data.shape[0])
#length = int((np.hypot(x1-x0, y1-y0))) (can be used instead of num_points)
num_points = 1000
xvalues = np.linspace(x0, x1, num_points)
yvalues = np.linspace(y0, y1, num_points)
f = scipy.interpolate.interp2d(x, y, data) #default is linear


# Extract the values along the line
profile = f(xvalues, yvalues) #this gives me a 2D array, I think it needs to be 1D
#c = profile.flatten()
print(profile.shape)
“轮廓”不是线性的,而是立方的。是否有一种方法可以使轮廓线性化,这样我就可以在起点和终点之间的点上切片图像?我所要做的就是把“轮廓”变成1D而不是2D

我想这样画:

import numpy as np
from numpy import random
from matplotlib.pyplot import figure, show

vels = np.linspace(0, 530, len(profile))
fig = figure()
frame = fig.add_subplot(1,1,1)
frame.plot(vels, profile)
frame.set_ylabel('y-axis')
frame.set_xlabel('x-axis')
frame.grid(True)
show()
print(vels.shape)
print(profile.shape)
print(len(profile))
我的代码不起作用,因为我得到的绘图不是一条直线的一部分,而是一个立方体的一部分

从文档中可以看出,网格似乎是通过插值构建的。凭直觉,我觉得你需要那个网格的形状。对代码进行快速调整:

import numpy as np
import scipy
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure, show
from scipy import interpolate

X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
data = np.sin(R)

x0 = 2
x1 = 23
y0 = 1
y1 = 36

rx0 = 4
rx1 = 2
ry0 = 7
ry1 = 32

x = np.arange(data.shape[1])
y = np.arange(data.shape[0])
num_points = 1000
xvalues = np.linspace(x0, x1, num_points)
yvalues = np.linspace(y0, y1, num_points)
f = scipy.interpolate.interp2d(x, y, data) #default is linear


# Extract the values along the line
profile = f(xvalues, yvalues)

xvalues2 = np.linspace(rx0, rx1, num_points)
yvalues2 = np.linspace(ry0, ry1, num_points)
profile2 = f(xvalues2, yvalues2)

plt.subplot(121)
plt.imshow(data.T, origin="lower", interpolation="nearest")
plt.scatter([x0, x1], [y0, y1])
plt.plot([x0, x1], [y0, y1])
plt.scatter([rx0, rx1], [ry0, ry1], c="r")
plt.plot([rx0, rx1], [ry0, ry1], c="r")
# plt.show()

diag = np.diag(profile)
diag2 = np.diag(profile2)
plt.subplot(122)
plt.plot(np.arange(diag.shape[0]), diag)
plt.plot(np.arange(diag2.shape[0]), diag2, c="r")
plt.show()
这将返回以下内容:

注意:我没有在2D绘图中考虑坐标(这就是为什么两条线看起来大小相同)。

从文档中可以看出,网格似乎是通过插值构建的。凭直觉,我觉得你需要那个网格的形状。对代码进行快速调整:

import numpy as np
import scipy
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure, show
from scipy import interpolate

X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
data = np.sin(R)

x0 = 2
x1 = 23
y0 = 1
y1 = 36

rx0 = 4
rx1 = 2
ry0 = 7
ry1 = 32

x = np.arange(data.shape[1])
y = np.arange(data.shape[0])
num_points = 1000
xvalues = np.linspace(x0, x1, num_points)
yvalues = np.linspace(y0, y1, num_points)
f = scipy.interpolate.interp2d(x, y, data) #default is linear


# Extract the values along the line
profile = f(xvalues, yvalues)

xvalues2 = np.linspace(rx0, rx1, num_points)
yvalues2 = np.linspace(ry0, ry1, num_points)
profile2 = f(xvalues2, yvalues2)

plt.subplot(121)
plt.imshow(data.T, origin="lower", interpolation="nearest")
plt.scatter([x0, x1], [y0, y1])
plt.plot([x0, x1], [y0, y1])
plt.scatter([rx0, rx1], [ry0, ry1], c="r")
plt.plot([rx0, rx1], [ry0, ry1], c="r")
# plt.show()

diag = np.diag(profile)
diag2 = np.diag(profile2)
plt.subplot(122)
plt.plot(np.arange(diag.shape[0]), diag)
plt.plot(np.arange(diag2.shape[0]), diag2, c="r")
plt.show()
这将返回以下内容:


注意:我没有在2D绘图中考虑坐标(这就是为什么两条线看起来大小相同)。

非常感谢,我的代码现在正按照我希望的方式工作!非常感谢,我的代码现在正按照我希望的方式工作!