Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opencv draws numpy.zeros as a gray image

I'm struggling with understanding how opencv interprets numpy arrays.

import cv2
import numpy as np

if __name__ == '__main__':
    size = (w, h, channels) = (100, 100, 1)
    img = np.zeros(size, np.int8)
    cv2.imshow('result', img), cv2.waitKey(0)
    cv2.destroyAllWindows()

Grayscale black 100x100 image, right? No, it's showing me gray! Why's that?

like image 526
Vanuan Avatar asked Sep 22 '13 15:09

Vanuan


Video Answer


2 Answers

Ok, the crucial part is dtype. I've chosen np.int8. When I use np.uint8, it is black.

Suprisingly, when dtype=np.int8, zeros are interpreted as 127(or 128)!

I expected that zero is still zero, no matter if it is signed or unsigned.

like image 57
Vanuan Avatar answered Sep 24 '22 17:09

Vanuan


For a BGR image,

img = np.zeros([height, width, 3], dtype=np.uint8)
like image 30
Mateen Ulhaq Avatar answered Sep 21 '22 17:09

Mateen Ulhaq