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