Color Matching Algorithm Needed
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Color Matching Algorithm Needed

  1. #1
    Join Date
    Aug 2001
    Posts
    77

    Color Matching Algorithm Needed

    Does anyone have (or know of) a good algorithm for finding the closest color available in a palette? I am using a 256-color palette, with 8 bits each for the r,g, and b components. Allegro uses a weighted squares method, which works OK, but it does not seem to be as accurate as the CPalette::GetNearestColorIndex() function in VC++. I am writing an app for DOS, so I can't use the VC++ function. Any help would really be appreciated.



    Jesus is Lord!

  2. #2
    Join Date
    Mar 2001
    Location
    Colorado
    Posts
    241

    Re: Color Matching Algorithm Needed

    Hello, These two functions together work really well. We have a math wiz on our team who gave me this to use once, and it is great.


    double CYourClass::ComputeColorDistance(double r1, double g1, double b1, double r2, double g2, double b2)
    {
    /*double rd = 77*(r1-r2)/255;
    double gd = 150*(g1-g2)/255;
    double bd = 29*(b1-b2)/255;
    double d = sqrt(rd*rd+gd*gd+bd*bd);

    return d;*/

    double x1 = .412453*r1/255+.357580*g1/255+.180423*b1/255;
    double y1 = .212671*r1/255+.715160*g1/255+.072169*b1/255;
    double z1 = .019334*r1/255+.119193*g1/255+.950227*b1/255;
    double x2 = .412453*r2/255+.357580*g2/255+.180423*b2/255;
    double y2 = .212671*r2/255+.715160*g2/255+.072169*b2/255;
    double z2 = .019334*r2/255+.119193*g2/255+.950227*b2/255;

    double cl1 = 116*pow(y1,1.0/3)-16;
    if (y1 <= .008856) cl1 = 903.3*y1;
    double ca1 = 500*(Fcn(x1)-Fcn(y1));
    double cb1 = 200*(Fcn(y1)-Fcn(z1));
    double cl2 = 116*pow(y2,1.0/3)-16;
    if (y2 <= .008856) cl2 = 903.3*y2;
    double ca2 = 500*(Fcn(x2)-Fcn(y2));
    double cb2 = 200*(Fcn(y2)-Fcn(z2));

    double ld = (cl1-cl2);
    double ad = (ca1-ca2);
    double bd = (cb1-cb2);

    double dist = ld*ld+ad*ad+bd*bd;
    dist = dist / 10100; //4 .05

    return dist;
    }

    double CYourClass::Fcn(double x)
    {
    if (x > .008856)
    return pow(x,1.0/3);
    else
    return 7.787*x+16.0/116;
    }





    Good luck!

    Wade

  3. #3
    Join Date
    Aug 2001
    Posts
    77

    Re: Color Matching Algorithm Needed

    Thanks, that is a lot better than the standard weighted squares method (which I noticed you had commented out in your code). This is good enough for me to use, but STILL not quite as good as the CPalette::GetNearestPaletteIndex() function.

    Thanks for your help.


    Jesus is Lord!

  4. #4
    Join Date
    May 2012
    Posts
    2

    Re: Color Matching Algorithm Needed

    Quote Originally Posted by Wade View Post
    Hello, These two functions together work really well. We have a math wiz on our team who gave me this to use once, and it is great.


    double CYourClass::ComputeColorDistance(double r1, double g1, double b1, double r2, double g2, double b2)
    {
    /*double rd = 77*(r1-r2)/255;
    double gd = 150*(g1-g2)/255;
    double bd = 29*(b1-b2)/255;
    double d = sqrt(rd*rd+gd*gd+bd*bd);

    return d;*/

    double x1 = .412453*r1/255+.357580*g1/255+.180423*b1/255;
    double y1 = .212671*r1/255+.715160*g1/255+.072169*b1/255;
    double z1 = .019334*r1/255+.119193*g1/255+.950227*b1/255;
    double x2 = .412453*r2/255+.357580*g2/255+.180423*b2/255;
    double y2 = .212671*r2/255+.715160*g2/255+.072169*b2/255;
    double z2 = .019334*r2/255+.119193*g2/255+.950227*b2/255;

    double cl1 = 116*pow(y1,1.0/3)-16;
    if (y1 <= .008856) cl1 = 903.3*y1;
    double ca1 = 500*(Fcn(x1)-Fcn(y1));
    double cb1 = 200*(Fcn(y1)-Fcn(z1));
    double cl2 = 116*pow(y2,1.0/3)-16;
    if (y2 <= .008856) cl2 = 903.3*y2;
    double ca2 = 500*(Fcn(x2)-Fcn(y2));
    double cb2 = 200*(Fcn(y2)-Fcn(z2));

    double ld = (cl1-cl2);
    double ad = (ca1-ca2);
    double bd = (cb1-cb2);

    double dist = ld*ld+ad*ad+bd*bd;
    dist = dist / 10100; //4 .05

    return dist;
    }

    double CYourClass::Fcn(double x)
    {
    if (x > .008856)
    return pow(x,1.0/3);
    else
    return 7.787*x+16.0/116;
    }


    Good luck!

    Wade
    Hello Wade,

    you computed color difference in this function .
    Well this is actually color matching or similarity . but my question is that what the difference mean actually regarding matching .
    I want to show color similarity image .do't want to compute the color differences.

    look : http://i1096.photobucket.com/albums/...similarity.jpg
    This is color difference formula that's what you did in your function and this is actually color matching.
    Just tell what this difference mean ?
    Also how can i show color similarity image ?

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Color Matching Algorithm Needed

    This thread is 10 years old.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,418

    Re: Color Matching Algorithm Needed

    Quote Originally Posted by mili123 View Post
    Hello Wade,
    How do you know that Wade is even still alive after 10 years?

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center