I'm converting a image (numpy array) into a string. Then I'm converting this string back to a numpy array of the original dimensions. Hence both the numpy arrays are equal- infact numpy.array_equals()
also returns True
for the arrays being equal.
When I call cv2.imshow()
on the original numpy array, it prints the image. But when I call cv2.imshow()
on the new numpy array, I get only a black screen.
Why is this happening? Both the numpy arrays are equal, so I should get the same output right?
import numpy as np
import cv2
frame = cv2.imread( '/home/nirvan/img_two.png' , cv2.IMREAD_GRAYSCALE)
string = ' '.join(map(str,frame.flatten().tolist()))
frameCopy = frame.copy()
x = frame.shape[0]
y = frame.shape[1]
frame = string.strip()
temp = [ int(t) for t in frame.split(' ')]
temp = np.array(temp)
temp = temp.reshape( (x,y) )
print( np.array_equal(frameCopy , temp) )
#gives black screen
cv2.imshow('l' , np.array(temp) )
#gives proper image
#cv2.imshow('l' , np.array(frameCopy) )
cv2.waitKey()
cv2. imshow() method is used to display an image in a window. The window automatically fits to the image size. Syntax: cv2.imshow(window_name, image)
To display the image, we read with an image with an imread() function, and then we call the imshow() method of the cv2 module. The imshow() function will display the image in a window, and it receives as input the name of the window and the image.
4. cv2. waitKey() This function is very important, without this function cv2.
So open the command prompt or terminal as per the operating system you are using. In that type, “Python,” it will show you the python version you are using. Next, in that use command “pip install OpenCV-python,” it will install this for you. Along with that, it will also install you the NumPy library.
Your arrays i.e. your frames are equal, but the data types are not. Your temp
array is of type int64
while imshow
expects uint8
. The following will fix your script:
cv2.imshow('l' , np.array(temp, dtype = np.uint8 ) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With