0

How can I sort a RGB struct vector v[i].R,v[i].G,v[i].B in C++ in order to later do a binary search on it to find the closest value for another image using the formula dist = abs(v[i].R-img.R)+abs(v[i].G-img.G)+abs(v[i].B-img.B).

I managed to do this for grayscale images given that for an image there is only one value but I can't figure out how to do it when there are 3 RGB values. More precisely, what should be the criteria for sorting the vector to get an accurate result? Many thanks.

  • An [octree](https://en.wikipedia.org/wiki/Octree) is commonly used for this task. – 500 - Internal Server Error Mar 02 '21 at 19:10
  • Any comparator implementing [strict weak order](https://en.cppreference.com/w/cpp/concepts/strict_weak_order) would work for sorting and binary-searching for a specific value, but I don't think you can binary-search for a closest value according to the formula you provided. – HolyBlackCat Mar 02 '21 at 19:18
  • @500-InternalServerError could you please describe how would I sort the elements in the vector? –  Mar 02 '21 at 19:23
  • @HolyBlackCat the formula works for the program I currently have but I/m trying to optimize it as I have a lot of images but I don't know on which criteria to do the sorting so that it'd be easier to compare the images afterwards based on the formula –  Mar 02 '21 at 19:26

0 Answers0