Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cv2.imshow() giving black screen

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()
like image 351
Nirvan Anjirbag Avatar asked Dec 18 '17 11:12

Nirvan Anjirbag


People also ask

What is cv2 Imshow ()?

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)

How do I show cv2 Imshow?

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.

Is cv2 waitKey necessary?

4. cv2. waitKey() This function is very important, without this function cv2.

How do I import a cv2 Imshow in Python?

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.


Video Answer


1 Answers

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 ) )
like image 196
s-m-e Avatar answered Nov 15 '22 07:11

s-m-e