First time here? Check out the FAQ!

Ask Your Question
1

Scalar Use

asked Feb 21 '13

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;

Thanks

Preview: (hide)

2 answers

Sort by » oldest newest most voted
2

answered Feb 21 '13

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.

Preview: (hide)

Comments

oh, right again ;)

berak gravatar imageberak (Feb 21 '13)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 (Feb 21 '13)edit
1

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

SR gravatar imageSR (Feb 21 '13)edit

Thanks a lot SR :)

izzo gravatar imageizzo (Feb 21 '13)edit
2

answered Feb 21 '13

berak gravatar image

updated Feb 21 '13

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)

Preview: (hide)

Question Tools

Stats

Asked: Feb 21 '13

Seen: 1,650 times

Last updated: Feb 21 '13