Ask Your Question

How to create a Scalar with variable number of channels?

asked 2018-03-28 15:41:57 -0500

Blath gravatar image

I want to create a Scalar which has a variable(integer) number of channels, hopefully all the channels being 0.

int channels = 2;
cv::Scalar(...); //Create Scalar with "channels" number of channels, all with the value of 0
edit retag flag offensive close merge delete


Did you try all()?

Der Luftmensch gravatar imageDer Luftmensch ( 2018-03-28 15:57:16 -0500 )edit

From what I've tested myself, it returns a 4 channel scalar with all elements set to a given value (which I would set to 0)

Blath gravatar imageBlath ( 2018-03-28 17:28:03 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-03-29 05:48:35 -0500

rwong gravatar image

cv::Scalar can store a maximum of 4 channels, each containing a double-precision floating point value.

It does not remember how many channels it was initialized with. It will always be 4 channels.

A default-constructed Scalar is already initialized with all-zeroes. This is because it has a user-defined constructor that does not require any arguments, and its body does exactly what you'd expect. Code:

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-03-28 15:41:57 -0500

Seen: 390 times

Last updated: Mar 29 '18