Simple metric for color difference in OpenCV?

I have two objects cv::Scalar, 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]);

It looks pretty awkward. Is there an easier way to express this or another metric, for example. a way to express a point product d*d, or a way of saying a direct distance from two cv::Scalaror cv::Vec4ito which it can be added afaik?

+4
source share
1 answer

As suggested by @IwillnotexistIdonotexist, you can use the class Vecand as per norm():

cv::Vec4d d = a-b;
double distance = cv::norm(d);
+7
source

All Articles