我有一个图像,我想使用pcolormesh
显示,但我并不真正理解这应该是如何工作的。我有某一种颜色的X和相应的Y,但是如果我在pcolormesh
中输入一个正常的数组作为C,我会得到一个错误。
我的代码:
# load image
img = cv2.imread('Distorted_resized_50.jpg')
img_array = np.asarray(img)
height, width, channels = img.shape
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# create vector matrix
U, V = np.meshgrid(range(gray_img.shape[1]),
range(gray_img.shape[0]))
UV = np.vstack((U.flatten(),
V.flatten())).T
H, mask = cv2.findHomography(UV_cp, XYZ_gcp)
UV_warped = cv2.perspectiveTransform(np.array([UV]).astype(np.float32), H)
UV_warped = UV_warped[0]
UV_warped = UV_warped.astype(np.int)
X_warped = UV_warped[:,0].reshape((height, width))
Y_warped = UV_warped[:,1].reshape((height, width))
fig, axs = plt.subplots(figsize=(15,10))
axs.pcolormesh(X_warped, Y_warped, img_array)
有谁能帮我吗?网站上的解释对我来说不是很清楚。这一切都很好,直到fig, axs = plt.subplots(figsize=(15,10))
回溯:
Traceback (most recent call last):
File "C:\Users\Yorian\Desktop\TU\Stage Shore\python_files\Rectificatie dmv foto thuis\rectify.py", line 53, in <module>
ax.pcolormesh(X_warped, Y_warped, img_array)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 7734, in pcolormesh
X, Y, C = self._pcolorargs('pcolormesh', *args, allmatch=allmatch)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 7350, in _pcolorargs
numRows, numCols = C.shape
ValueError: too many values to unpack
发布于 2018-11-11 15:28:30
我最近遇到了一个类似的问题,在不了解更多的情况下,我猜测问题是您有一个xdim,通过ydim×3数组,plt.pcolormesh
需要一个由标量值组成的2d数组,其中您有r、g和b的值(介于0和255之间)。
如上所述,您可以做一些事情:
将图像显示为灰度,首先通过skimage.color.rgb2grey(image)
转换图像,然后使用pcolormesh和cmap='binary'
绘图
或者使用plt.imshow
绘制,并使用本文中建议的kwarg=extent
。Matplotlib: how to make imshow read x,y coordinates from other numpy arrays?
https://stackoverflow.com/questions/22073617
复制相似问题