Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple metric for difference of color in OpenCV?

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?

like image 905
Daniel S. Avatar asked Oct 01 '14 00:10

Daniel S.


People also ask

How do you find the difference in colors?

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).

How is color distance calculated?

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*₂)².

What is identified as the difference between two colors * 1 point?

The difference is called Delta-E.

What does distance mean in color?

The measured distance (colour difference) between two colours.


1 Answers

As suggested by @IwillnotexistIdonotexist, you can use the Vec class and according norm():

cv::Vec4d d = a-b;
double distance = cv::norm(d);
like image 169
herohuyongtao Avatar answered Oct 26 '22 18:10

herohuyongtao