Ask Your Question

Scalar Use

asked 2013-02-21 07:40:22 -0500

izzo gravatar image

Hey there, please someone tell me what's the Scalar do in the following code ? and what's the use of it in general ?

Mat M(2,2, CV_8UC3, Scalar(0,0,255));
cout << "M = " << endl << " " << M << endl << endl;


edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-02-21 08:05:48 -0500

SR gravatar image

It initializes all pixels within the 2x2 image M with the color red as the Scalar(0,0,255) describes red in BGR ordering.

edit flag offensive delete link more


oh, right again ;)

berak gravatar imageberak ( 2013-02-21 08:07:40 -0500 )edit

alright, one more question, when the data type is (CV_8UC3), it's always BGR ?, and what if i want it in reverse (RGB) how could it be possible ?

izzo gravatar imageizzo ( 2013-02-21 08:23:17 -0500 )edit

By default yes. If you want to interchange channels, you need to 'convert' the image: cvtColor(src, dst, CV_BGR2RGB);.

SR gravatar imageSR ( 2013-02-21 08:27:22 -0500 )edit

Thanks a lot SR :)

izzo gravatar imageizzo ( 2013-02-21 08:30:24 -0500 )edit

answered 2013-02-21 08:03:04 -0500

berak gravatar image

updated 2013-02-21 08:12:51 -0500

Scalar is a typedef for Scalar_<double>, which inherits from template<class _Tp> Vec<4,_Tp> ...

lol, now seriously: think of it as a Vec with variable size(up to 4 values), there can be Scalar(0), Scala(3,3,3), or Scalar(31,12,3,14);

it's mostly used to pass Color values, ranges, or similar in opencv

in your example above, a 2x2 rgb Mat is created and all pixels are set to Scalar(0,0,255) (blue)

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2013-02-21 07:40:22 -0500

Seen: 555 times

Last updated: Feb 21 '13