0

I'm trying to write an efficient pair matching algorithm for a color matching context

Let's say we have an object (representing a CIE-LAB colour):

public class labColour {

      double l;
      double a;
      double b;

}

Now let's say we have two lists of LabColour:

List<LabColour> list1;
List<LabColour> list2;

Each list is of size n. Finally let's say we have a complex function which can calculate the similarity/distance between two colours:

    public double cie94ColourDistance(LabColour labColour1, LabColour labColour2) {

         final double L1 = labColour1.getL();
         final double a1 = labColour1.getA();
         final double b1 = labColour1.getB();
         final double L2 = labColour2.getL();
         final double a2 = labColour2.getL();
         final double b2 = labColour2.getL();

         double c1 = Math.sqrt(a1 * a1 + b1 * b1);
         double deltaC = c1 - Math.sqrt(a2 * a2 + b2 * b2);
         double deltaA = a1 - a2;
         double deltaB = b1 - b2;
         double deltaH = Math.sqrt(Math.max(0.0, deltaA * deltaA + deltaB * deltaB - deltaC * deltaC));

         return Math.sqrt(Math.max(0.0, Math.pow((L1 - L2) / (kL * sl), 2) + Math.pow(deltaC / (kc * (1 + K1 * c1)), 2) + Math.pow(deltaH / (kh * (1 + K2 * c1)), 2.0))); 
      }

I want to find the best match between each of the n elements in the two lists. The naive approach would be to use a nested for loop to iterate through list1 and list2, and use cie94ColourDistance to calculate the best match for each. This is computationally expensive at O(n^2) complexity.

Can I do this faster O(n^2)? If so, how?

Also, is there a way (e.g. multithreading, a certain library, caching etc.) to speed the whole thing up in Java?

Thanks in advance!

Ben123
  • 335
  • 2
  • 11
  • Why is your distance function so complicated? CIELAB was intended to make perceptual difference = Euclidean distance. – Matt Timmermans Dec 02 '21 at 14:33
  • https://en.wikipedia.org/wiki/Color_difference#CIE94 That was the intention but not the outcome. The distance function has been adjusted various times to address perceptual non-uniformities. – Ben123 Dec 02 '21 at 14:44
  • If the colourDistance positions the colors in order, I would go with a sortedlist (sortedSet in Java I believe) where the elements are sorted by the algorithm. This allows for binary searches. – Aldert Dec 02 '21 at 17:40
  • OIC. Well, it's a shame that it doesn't seem possible to map that into a space where Euclidean distance works. If you put points into a K-D tree using their LAB coordinates, though, then you can use this procedure to do a closest-point search using your expensive metric: https://stackoverflow.com/questions/50699240/query-points-in-circle-areas/50705109#50705109 – Matt Timmermans Dec 02 '21 at 18:43
  • @Aldert it's three dimensional space so there is no linear 'order'/sequence of colours unfortunately. – Ben123 Dec 03 '21 at 09:35
  • @MattTimmermans Thanks very much for this. Will look at implementing it. The other option is just to use Euclidean I guess (and sacrifice some accuracy). Do you know if doing this would open up any other possible algorithms? – Ben123 Dec 03 '21 at 09:36
  • Not sure. I would probably do it the same way, but the part where you have to find a lower bound on the distance between the search point and a box is going to be simpler and more accurate, leading to a faster search. – Matt Timmermans Dec 03 '21 at 13:07

0 Answers0