【问题标题】:x and y are changed in the picture. Where does it change in the code?图中的 x 和 y 发生了变化。它在代码中的哪些地方发生了变化?
【发布时间】:2018-08-07 07:08:21
【问题描述】:

我正在上传一张图片,然后输出的图片突然垂直翻转。我不知道它发生在代码的什么地方。

from scipy.interpolate.rbf import Rbf  # radial basis functions
import cv2
import matplotlib.pyplot as plt
import numpy as np
from shrink import shrink2, shrink3

# Parameter
res = 5         # to add sequence of black points
smoo = -200       # to smooth rbf
eps = 2.8       # epsilon of rbf
shrinkEps = 4.

# import data
input = "testProbe3.jpg"
image = cv2.imread(input)   # load the image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)  # convert it to grayscale

直到这里我认为什么都没有发生。也许它必须发生在这里的某个地方:

#Reduce pixels
shrinked = shrink3(gray, int(gray.shape[0]/3), int(gray.shape[1]/3))

先y后x对吗?

# # Add white Points
y, x = np.where(shrinked >= 0)

# Set z values to summed values
z=[]

这里也是:首先循环 y(行)然后循环 x(列),对吗?

for y1 in range(0, shrinked.shape[0]):
    for x1 in range(0, shrinked.shape[1]):
        z = np.append(z, shrinked.item(y1,x1))

然后用 xx 和 yy 绘制一个网格法线?

# meshgrid
x_min, x_max = x.min() - 1, x.max() + 1
y_min, y_max = y.min() - 1, y.max() + 1
ti = np.linspace(x_min, x_max, (x_max-x_min+1)*3)
tii = np.linspace(y_min, y_max, (y_max+y_min+1)*3)
xx, yy = np.meshgrid(ti, tii)

和 rbf 插值法线与 x, y, z?

# rbf interpolation
rbf = Rbf(x, y, z, function='gaussian', epsilon=eps/shrinkEps, smooth=smoo)
zz = rbf(xx, yy)
jet = cm = plt.get_cmap('jet')
plt.pcolor(xx, yy, zz, cmap=jet)
plt.colorbar()

我认为这不可能是错误:

# Plotting the original points.
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.title('epsilon = %e/%s, smooth=%s, res=%r, Auflösung 1x gedrittelt, 1x halbiert, testProbe3.jpg'%(eps, shrinkEps, smoo, res))
plt.gcf().set_size_inches(9, 7)

plt.show()

这是我定义的模块收缩:

def shrink3(data, rows, cols):
    if data.shape[0] % 3:                       # Wenn Zeilen nicht durch 3 teilbar
        if (data.shape[0] - 1) % 3 == 0:        # dann reduziere um 1
            data = data[0:data.shape[0] - 1, :]
        elif (data.shape[0] - 2) % 3 == 0:      # sonst reduziere um 2
            data = data[0:data.shape[0] - 2, :]
    if data.shape[1] % 3:                       # Wenn Spalten nicht durch 3 teilbar
        if (data.shape[1] - 1) % 3 == 0:        # dann reduziere um 1
            data = data[:, 0:data.shape[1] - 1]
        elif (data.shape[1] - 2) % 3 == 0:      # sonst reduziere um 2
            data = data[:, 0:data.shape[1] - 2]
    return data.reshape(rows, int(data.shape[0]/rows), cols, int(data.shape[1]/cols)).sum(axis=1).sum(axis=2)

有什么想法吗?

【问题讨论】:

  • 您可以尝试在代码中的不同位置显示图像。我将首先在shrinked 之后显示图像,因为我不确定该函数的作用。
  • @Christoffer 谢谢你的回答。我在代码描述中添加了收缩功能。绘制shrinked 对我没有帮助,因为图像没有意义。
  • 那是因为imread总是用the origin on the upper left corner读取图像?
  • @jadelord。是的,我认为这是问题所在。我颠倒了y并解决了我的问题。

标签: python python-3.x numpy matplotlib scipy


【解决方案1】:

在您的代码中,查看标有!!!! 的行:

#Reduce pixels
shrinked = shrink3(gray, int(gray.shape[0]/3), int(gray.shape[1]/3))
# # Add white Points
!!!! y, x = np.where(shrinked >= 0)

# Set z values to summed values
z=[]
for y1 in range(0, shrinked.shape[0]):
    for x1 in range(0, shrinked.shape[1]):
        z = np.append(z, shrinked.item(y1,x1))
# meshgrid
x_min, x_max = x.min() - 1, x.max() + 1
y_min, y_max = y.min() - 1, y.max() + 1
ti = np.linspace(x_min, x_max, (x_max-x_min+1)*3)
tii = np.linspace(y_min, y_max, (y_max+y_min+1)*3)
!!!! xx, yy = np.meshgrid(ti, tii)

# rbf interpolation
rbf = Rbf(x, y, z, function='gaussian', epsilon=eps/shrinkEps, smooth=smoo)
zz = rbf(xx, yy)
jet = cm = plt.get_cmap('jet')
plt.pcolor(xx, yy, zz, cmap=jet)
plt.colorbar()

您切换xy。也许你可以这样做:

yy, xx = np.meshgrid(tii, ti)

【讨论】:

  • 感谢您的回答@BrunoGL。我已经试过了,没有任何改变
【解决方案2】:

完全是猜测。这里有什么问题吗?

rbf = Rbf(x, y, z, function='gaussian', epsilon=eps/shrinkEps, smooth=smoo)

切换xy会有帮助吗

rbf = Rbf(y, x, z, function='gaussian', epsilon=eps/shrinkEps, smooth=smoo)

【讨论】:

  • 不,然后图片转90度
【解决方案3】:

好的,看起来plt.pcolor 反转了我的数组 y。于是用下面的代码改正:

# inverse y
y = y[::-1]

现在我的图像看起来不错。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多