0

Ok, I asked a similar question that revolved around vrep but it was a little specific when in fact a simpler python-based question would be more useful. I will, however, leave the question there should anyone be able to provide useful information.

Here is the question; How does one take a 1 dimensional list containing image data, convert it into a numpy array and then display it?

This is what I have so far:

im = np.array(image, dtype=np.uint8)
im.resize(128,128,3) #reshape this array into an image form (e.g. rather than 49152)
mlp.imshow(im)
pylab.show(im)

Here, image is returned from a simxGetVisionSensorImage (not important if you don't know vrep stuff) and is a list. I then try to create a numpy array and read the data in, turning it from a signed 8 bit integer into an unsigned 8 bit integer. I then resize it (it is a 49152 length list corresponding to a resolution of 128x128) and attempt to display it using either matplotlib or pylab.

Here are the includes should you need them:

import numpy as np
import matplotlib.pyplot as mlp
import pylab

the matplotlib.show command does not even show a window for the image. the pylab.show command throws this error:

Traceback (most recent call last):
File "vrep_epuck.py", line 59, in <module>
pylab.show(im)
File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 244, in show
return _show(*args, **kw)
File "/usr/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 165, in __call__
if block:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Here is the link to the original vrep question should you want to see the whole code or see the vrep stuff:

vrep question

2
  • Could you try printing the dimension of the im array before and after resizing? Commented Jun 24, 2017 at 12:16
  • Sure. I used im.shape to give me the dimensions after im = np.array(image, dtype=np.uint8) which printed (49152,) and i used im.shape again after im.resize(128,128,3) which printed (128, 128, 3) I'm not sure why there is a comma and then no number in the printout initially. Commented Jun 24, 2017 at 12:49

1 Answer 1

3

It would help to stay within the usual naming conventions.
Because then it would be more obvious that pyplot.show() does not take an image as argument. Thus, don't use pyplot.show(some_image_as_argument) but simply pyplot.show().

import matplotlib.pyplot as plt

image = ...
im = np.array(image, dtype=np.uint8)
im.resize(128,128,3) 

plt.imshow(im)
plt.show()
Sign up to request clarification or add additional context in comments.

4 Comments

Apologies, I followed a tutorial and the guy used "mlp". So pyplot.show() takes a numpy array as an argument, and then pyplot.show() displays it? I tried your code and it still does not show the image. There is no error message, the code still runs but no image is displayed. I should mention that this code is implemented inside a while loop. (The full code can be viewed on the link provided in the question but is mostly vrep stuff).
Can you try the code above outside the for loop? The code above is very standard and it works for me with the sample 128x128 RGB downloaded from web.
Reasons for the image not showing up in the original code (assuming that you have removed pylab and used the code above) are still numerous. If the connection doesn't return anything or vrep.simxGetConnectionId(clientID)==-1 or error!=vrep.simx_return_ok will all lead to the image not showing up. You need to debug the code yourself (nobody else can do this for you) and find out which command is actually executed.
@omdv yes I have tried the code outside the loop and it still does not seem to appear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.