Results 1 to 20 of 21

Thread: Problems working with 8bpp and 24bpp images

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Cesar Guest

    Default Re: Problems working with 8bpp and 24bpp images

    I'm glad you managed with that . Anyway I suggest you to use QT api, where it is possible to avoid code duplication.
    And here's my suggestion of the second function. I suppose it's a bit more precise
    First of all, let me explain, why is it so. Pretend QRgb is a point with coordinates (r, g, b). Then imatge.colorTable() is a series of points you have to choose from, when approximating random QRgb.
    Let's pretend you have to approximate point QRgb(R', G', B'). The best point to choose from imatge.colorTable() is the one, which is the most close to (R', G', B'). As you know, the distance betwean two points is calculated this way:
    Qt Code:
    1. double distance(const QRgb &x, const QRgb &y)
    2. {
    3. int dRed = qRed(x) - qRed(y);
    4. int dGreen = qGreen(x) - qGreen(y);
    5. int dBlue = qBlue(x) - qBlue(y);
    6.  
    7. return sqrt(dRed * dRed + dGreen * dGreen + dBlue * dBlue);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Now your task is to find such an i, for which distance(color, imatge.colorTable()[i]) is the least. You present algorythm fits well .
    Qt Code:
    1. int FotoEditorFotos::obtenirIndexColor(const QRgb &colorRgb)
    2. {
    3. QVector<QRgb> taulaColor = imatge.colorTable();
    4. /*
    5. int indexRgb = taulaColor.indexOf(colorRgb);
    6.  
    7. if (indexRgb < 0)
    8. {
    9. */
    10. int indexRgb;
    11. // Search for a similar color in the color table...
    12. int n = taulaColor.count();
    13.  
    14. int verd = qGreen(colorRgb);
    15. int vermell = qRed(colorRgb);
    16. int blau = qBlue(colorRgb);
    17. int alpha = qAlpha(colorRgb);
    18. int diffMin = 3 * 255 * 255 + 1;
    19. int diff;
    20. QRgb rgb;
    21.  
    22. for (int i = 0; i < n; ++i)
    23. {
    24. rgb = taulaColor[i];
    25. int dR = vermell - qRed(rgb);
    26. int dG = verd - qGreen(rgb);
    27. int dB = blau - qBlue(rgb);
    28. diff = dR * dR + dG * dG + dB * dB;
    29.  
    30. if (diff < diffMin)
    31. {
    32. diffMin = diff;
    33. indexRgb = i;
    34. if (0 == diffMin) break;
    35. }
    36. }
    37. // }
    38.  
    39. return indexRgb;
    40. }
    To copy to clipboard, switch view to plain text mode 
    Notes:
    • For performance reasons it's better to compare squares of distances rather then distances themselves.
    • There's no need to do indexRgb = taulaColor.indexOf(colorRgb) Guess why.

  2. #2
    Cesar Guest

    Default Re: Problems working with 8bpp and 24bpp images

    Oops, it seems I've forgotten to take alpha channel in account. I suppose you can do it youself.

  3. #3
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    44
    Thanked 2 Times in 2 Posts

    Default Re: Problems working with 8bpp and 24bpp images

    Another time thanks a lot Cesar, your suggestions are very helpfully It's a great idea to calculate a similar color in the table using the distance between two points, I didn't remember that function . I will try it and I comment the results.

    PD: did you forget to calculate the distance using squares in the function 'obtenirIndexColor'?
    Qt Code:
    1. diff = dR * dR + dG * dG + dB * dB;
    To copy to clipboard, switch view to plain text mode 
    The results of calculating the distance multiplying each term with itself are the same that calculating it as I did with the absolute value, no?
    Last edited by SkripT; 15th February 2006 at 15:36.

  4. #4
    Cesar Guest

    Default Re: Problems working with 8bpp and 24bpp images

    Quote Originally Posted by SkripT
    Another time thanks a lot Cesar, your suggestions are very helpfully It's a great idea to calculate a similar color in the table using the distance between two points, I didn't remember that function . I will try it and I comment the results.
    You are always welcome
    Quote Originally Posted by SkripT
    The results of calculating the distance multiplying each term with itself are the same that calculating it as I did with the absolute value, no?
    No I didn't. Consider the example:
    The point to approximate: P(100, 100, 100).
    Two points to choose betwean: A1(75, 125, 100) and A2(100, 51, 100)
    Qt Code:
    1. diffSkript(P, A1) = |100 - 75| + |100 - 125| + |100 - 100| = 25 + 25 = 50
    2. diffSkript(P, A2) = |100 - 100| + |100 - 51| + |100 - 100| = 49
    3. diffCesar(P, A1) = (100 - 75)*(100 - 75) + (100 - 125)*(100 - 125) + (100 - 100)*(100 - 100) = 625 + 625 = 1250
    4. diffCesar(P, A2) = (100 - 100)*(100 - 100) + (100 - 51)*(100 - 51) + (100 - 100)*(100 - 100) = 49*49 = 2401
    To copy to clipboard, switch view to plain text mode 
    As you can see, the results differ. Which one is the best? You choose

  5. #5
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    44
    Thanked 2 Times in 2 Posts

    Default Re: Problems working with 8bpp and 24bpp images

    You' re right Cesar, the results differ!! I will try some colors and see which one is more aproximated.

  6. #6
    Cesar Guest

    Talking Re: Problems working with 8bpp and 24bpp images

    Go ahead! And please, post the results, 'cos I'm too curious

  7. #7
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    44
    Thanked 2 Times in 2 Posts

    Default Re: Problems working with 8bpp and 24bpp images

    Hi, Cesar I have tested a little both versions. In many colors they give me the same results. But, in other cases, your version give me results that looks similar to the original color. Here are two examples:

    Color to aproximate: r:85, g:0, b:255
    Cesar: r:31, g:82, b:138
    SkripT: r:79, g:110, b:140

    Color to aproximate: r:255, g:85, b:255
    Cesar: r:245, g:207, b:178
    SkripT: r:255, g:255, b:255

    In other cases where they gave me different results, was difficult to say which of one was closer to the original color because is a very subjective fact. Finally I maintain your version because looks more professional calculating the difference with the distance between points

    Cheers.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.