Reputation: 13
I am new to opencv and I was just wondering if I can make a image using numpy array. First I just made a simple black image using np.zeroes ->
import numpy as np
img = np.zeros([512,512,3])
cv.imwrite('color_img.jpg', img)
cv.imshow("image", img)
cv.waitKey(0)
This displays a black image.
So I wanted to try out this with some different values using np.full()
Code->
import numpy as np
img = np.full([512, 512, 3], 0)
cv.imwrite('color_img.jpg', img)
cv.imshow("image", img)
cv.waitKey(0)
I first tried np.full()
with other values than zero but it wasn't working then I tried the
same with 0
, but it still doesn't work.
This shows a window which instantly disappears, why?
Both are same arrays so why one shows and other not?
I checked the documentation of both np.zeros()
and np.full()
, if they
both return the same thing, then what is the error here.
Any explanation will be helpful.
I am trying to display a numpy array as image.
I was expecting np.zeros()
and np.full()
to work same but they didn't.
Upvotes: 0
Views: 341
Reputation: 13
The problem was in the data type of both the arrays. (As pointed out in comments.)
The dtype of np.zeros() is float64 while the dtype of np.full() is int32. So, it's better to specify dtype while doing such things.
eg-> (array of zeros using np.full())
import numpy as np
img = np.full([512,512,3], 0, dtype=np.float64)
cv.imshow("image", img)
cv.waitKey(0)
Upvotes: 1