I have to find the sum of the elements in a Mat, the sum function of OpenCV returns a cv::Scalar but how should I interpret it ?
Scalar Represents a 4-element vector. The type Scalar is widely used in OpenCV for passing pixel values. In this tutorial, we will use it extensively to represent BGR color values (3 parameters). It is not necessary to define the last argument if it is not going to be used.
Data objects in C++ can be categorized as either scalar (e.g. integers and pointers) or non- scalar (e.g. arrays and classes), where scalars are primitive objects which contain a single value and are not composed of other C++ objects.
Vec3b is the abbreviation for "vector with 3 byte entries" Here those byte entries are unsigned char values to represent values between 0 .. 255. Each byte typically represents the intensity of a single color channel, so on default, Vec3b is a single RGB (or better BGR) pixel.
cv::Scalar
is used because the image can be multi-channel.
For this reason the the white color is represented as:
cv::Scalar(255,255,255);
For access a particular element you can simply use [] operator
:
cv::Scalar myWhite(255,255,255);
cout << myWhite[0] << endl;
For the sum, each channel will represent the sum of that particular channel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With