0

I have an image that I want to show using pcolormesh but I do not really understand how this should work exactly. I have the X and corresponding Y for a certain color but if I enter a normal array as C in pcolormesh I get an error.

My code:

# 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)

Anybody that can help me? The explanation on the website isn't very clear to me. It all works fine until fig, axs = plt.subplots(figsize=(15,10))

Traceback:

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
Georgy
  • 12,464
  • 7
  • 65
  • 73
Yorian
  • 2,002
  • 5
  • 34
  • 60

1 Answers1

1

I recently ran into a similar problem, and without knowing more, I'm guessing that the problem is that you have a xdim, by ydim by 3 array, plt.pcolormesh expects a 2d array of scalar values where you have values for r,g, and b (between 0 and 255).

With that said, you can do a few things:

display the image as greyscale first converting the image via skimage.color.rgb2grey(image) and plot with pcolormesh and cmap='binary'

OR plot with plt.imshow and use the kwarg=extent as suggested in this post here Matplotlib: how to make imshow read x,y coordinates from other numpy arrays?

SBFRF
  • 167
  • 2
  • 16