【发布时间】: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