I have two cv::Scalar
objects and I want to calculate the color difference.
I came up with this code:
cv::Scalar a(255, 128, 255); // color 1
cv::Scalar b(100, 100, 100); // color 2
cv::Scalar d = b - a;
double distance = sqrtl(d[0]*d[0] + d[1]*d[1] + d[2]*d[2]);
This looks rather clumsy. Is there a simpler way to express this or another metric, e.g. a way to express the dot product d*d
, or a way to say directly distance of two cv::Scalar
, or cv::Vec4i
, to which it can be casted afaik?
For two samples in Riemannian colour space, the colour difference can be calculated by integrating the quadratic equation for curvature along the geodesic distance between the points and dividing the result by a constant called least perceptible difference (McDonald 1982; Wyszecki and Stiles, 1982).
In order to measure the difference between two colors, the difference is assigned to a distance within the color space. In an equidistant-method color space, the color difference ∆E can be determined from the distance between the color places: ΔE = √ (L*₁-L*₂)² + (a*₁-a*₂)² + (b*₁-b*₂)².
The difference is called Delta-E.
The measured distance (colour difference) between two colours.
As suggested by @IwillnotexistIdonotexist, you can use the Vec
class and according norm()
:
cv::Vec4d d = a-b;
double distance = cv::norm(d);
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