2

I have a 2D python list named image, the array contain only integer in the range from 0-255. For example, the list is like this

image = [[0, 39, 57], [255, 182, 124], [19, 223, 200], [176, 190, 100]]

I have read How to convert a python numpy array to an RGB image with Opencv 2.4?, so i converted the python list to numpy array

image = np.asarray(image)

I could run the following

cv2.imwrite('image.jpg', image)

But how do i load the array as image directly without saving it to file ? something like this

opencvImage = cv2.imread(image, 0)

The above code obviously don't work, so how do i load python list or numpy array directly to opencv without saving it as file? the expected image output is rgb image or greyscale image, and the expected shape is the shape of the list/array itself.

The following code also don't work, i also don't understand why i can save the numpy array as image file but opencv cannot show it directly

cv2.imshow("image", image)

The code above throws assertionfailed error from opencv

LLL
  • 229
  • 2
  • 13
  • 1
    The problem you have is certainly related to typing and shapes but without a clear **example** or much more precise informations about types, we can hardly help you. Using `np.asarray` seems fine to me at first glance. – Jérôme Richard Mar 19 '22 at 13:37
  • i think im already clear enough that my image array is 2d python list with integer from 0 to 255, example is [[0, 39, 57], [255, 182, 124], [19, 223, 200], [176, 190, 100]] – LLL Mar 19 '22 at 14:06
  • 1
    okay i will add example of the image array to the post – LLL Mar 19 '22 at 14:07
  • Ok, thank you, but what is the expected output image? A gray image, RGB, BGR, etc? What about its expected shape? For example, the array can be interpreted as a 2x2 RGB image or a 1x4 RGB or a 4x1 or even a 4x3 gray image. – Jérôme Richard Mar 19 '22 at 14:34
  • oh sorry i forgot to mention, the expected output is either an RGB image or greyscale image – LLL Mar 19 '22 at 14:39
  • the expected shape is the shape of the list/array itself – LLL Mar 19 '22 at 14:45

2 Answers2

2

Based on the provided information, the only possible output is a gray image with the shape (4, 3). OpenCV image are just basic Numpy arrays. You can convert the list using:

lst = [[0, 39, 57], [255, 182, 124], [19, 223, 200], [176, 190, 100]]
image = np.asarray(lst, dtype=np.uint8) # Specifying dtype is critical here

# image = array([[  0,  39,  57],
#                [255, 182, 124],
#                [ 19, 223, 200],
#                [176, 190, 100]])

Then you can show it with cv2.imshow("image", image) but be aware that the image is a bit too small to be easily seen. Note also that a cv2.waitKey() loop may be needed so the window does not freeze and print the image correctly.

Jérôme Richard
  • 41,678
  • 6
  • 29
  • 59
1

You can crease numpy.array then divide by 255.0 then show the image with cv2 like below:

import numpy as np
import cv2

image = [[0, 39, 57], [255, 182, 124], [19, 223, 200], [176, 190, 100]]
image = np.array(image, copy=False) / 255.0

cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Or you can use matplotlib for showing the image like below:

import matplotlib.pyplot as plt
import numpy as np

image = [[0, 39, 57], [255, 182, 124], [19, 223, 200], [176, 190, 100]]
image = np.array(image, copy=False) / 255.0

plt.imshow(image); plt.axis('off')
plt.show()

Output:

enter image description here

I'mahdi
  • 23,382
  • 5
  • 22
  • 30