Ask Your Question
0

How does np.zeros() create a black background?

asked 2015-10-28 08:28:10 -0600

arp1561 gravatar image

updated 2015-10-28 08:29:31 -0600

I am creating a very simple line drawing program. The problem is that i am still in the learning stage and I jsut stumbled across a problem.

import numpy as np import cv2

img = np.zeros((512,512,3), np.uint8)

img = cv2.line(img,(0,0),(511,511),(255,0,0),5)

how does np.zeros((512,512,3), np.uint8) create a black back ground? What if i want to create a red or a green background? And also, what does these 2 parameters mean?

I hope this answers my problem! Thanks!! :)

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
1

answered 2015-10-28 09:15:13 -0600

Since you are using np.zeros, you are explicitly telling it to fill the complete image with zeros. Adding (0,0,0) at every position in the image results in a black image. Setting it all to (255,255,255) would be white, equalling np.ones() function. If you want color then you should assign values to the image positions.

edit flag offensive delete link more

Comments

I tried using np.ones() but that is also giving me a black image. What should I do to get a white image?

meghasoni gravatar imagemeghasoni ( 2017-03-03 06:38:17 -0600 )edit

Can you try np.ones() * 255? It might be that your range is not correctly detected. If not, please open a new issue with more details.

StevenPuttemans gravatar imageStevenPuttemans ( 2017-03-03 07:10:01 -0600 )edit

but what is the significane of 3 in (255,255,3 )

supr8sung gravatar imagesupr8sung ( 2017-04-21 17:56:42 -0600 )edit

its a 255x255 image with 3 channels

StevenPuttemans gravatar imageStevenPuttemans ( 2017-04-24 06:04:14 -0600 )edit
0

answered 2018-09-17 07:06:31 -0600

I've also been facing the same issue, try doing this (it solved the issue for me):

img = np.ones((512,512,3)) #img is the background image which appears when you run the program

For getting a black background you can use the np.zeros() function

img = np.zeros((512,512,3))

And if you want any other color then try filling the matrix with your choice of numbers corresponding to RGB values of your choice as this is (512 X 512 X 3) matrix.

edit flag offensive delete link more
0

answered 2020-05-03 08:27:18 -0600

The following python code worked for me for white background. Thanks to Steven P.

DEFAULT_SIZE_WHITE_CHANNEL = (640, 480, 1)
white_channel_image = np.ones(DEFAULT_SIZE_WHITE_CHANNEL, dtype = "uint8") * 255

The image produced will have a single channel of size 640 x 480. You will get a white image. Merge it with different channels using cv2.merge() function to get a RGB image.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-10-28 08:28:10 -0600

Seen: 51,325 times

Last updated: May 03 '20