Recent

Author Topic: How to Optimeze use of BGRAColorQuantizer??  (Read 15239 times)

Sanem

  • Full Member
  • ***
  • Posts: 173
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #15 on: August 26, 2016, 10:06:56 am »
Thank you so much for your replay Michl, I will give it a try as soon as possible.

Sanem

  • Full Member
  • ***
  • Posts: 173
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #16 on: August 26, 2016, 10:11:59 am »
I have tested what you said, yep the IncColor method from TBGRAWeightedPalette class, does the weight calculating for each pixel of source colors, but as the first problem of mine with TBGRAColorQuantizer, it takes too much time, even without gaining the values for display or sth! (3200ms for a bitmap 1920*1080).
Yes you can access the colors using the Count, Color[...] and Weight[...] properties.

I agree it is slow though.


Thank you Circular.

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #17 on: August 26, 2016, 04:37:07 pm »
The shift for the alpha value can change because of little endian / big endian processor difference.

To be sure to get a value between 0 and 16777215 for a color you can use BGRAToColor() function.
Conscience is the debugger of the mind

Sanem

  • Full Member
  • ***
  • Posts: 173
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #18 on: August 28, 2016, 03:55:25 pm »
HI
I am trying to implement the weight concept of each "unique color pixel" that a bitmap contains, just like what BGRA ColorQuantizer does, and I thought I can do it by computing number of pixels of the colors that the bitmap contains for each color, but I was wrong, it seems that its doing more things. I was tring to read BGRA methods, and now I have some problem understanding purpose and steps of GetAsArrayOfWeightedColors method in class TBGRAColorTree in unit BGRAColorQuantization, (line 1274), as you can see in Screenshot attached.
I am getting BGRA package from git.

Any idea how BGRA ColorQuantizer works to implement weight concept for colors pallet??
I just wanna understand the concept and steps to gaining each color weights in BGRA.


Any help appreciated prior
Regards




Here is my test code to gain color pallet from BGRA:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ColorPallet;
  2. var
  3.   quant: TBGRAColorQuantizer;
  4.   i, j, ColorPalleteCount: integer;
  5.   arr: ArrayOfWeightedColor;
  6. begin
  7.   SourceBmp := TBGRABitmap.Create('Pics' + PathDelim + 'Picture.jpg');
  8.   quant := TBGRAColorQuantizer.Create(SourceBmp, acFullChannelInPalette, 10);
  9.   ColorPalleteCount := quant.ReducedPalette.Count;
  10.   arr := quant.ReducedPalette.GetAsArrayOfWeightedColor;
  11.   //Display the color pallet
  12.   for i := 0 to ColorPalleteCount - 1 do
  13.   begin
  14.     WriteLn('Weight [ ', i, ' ] : ', arr[i].Weight);
  15.     WriteLn('Color [ ', i, ' ] : (', arr[i].Color.red, ' , ', arr[i].Color.green, ' , ', arr[i].Color.blue, ' )');
  16.   end;
  17.   quant.Free;
  18.   SourceBmp.Free;
  19. end;    
  20.  
« Last Edit: August 28, 2016, 03:57:47 pm by simin_sh »

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #19 on: August 28, 2016, 07:44:31 pm »
When creating the palette, the color tree is divided into branches. The set of colors is split in two to create two branches and this process is repeated until there are enough branches. A leaf is not divided and contains one color.

So to get the list of colors, the colors of all leaves are combined recursively.

Conscience is the debugger of the mind

Sanem

  • Full Member
  • ***
  • Posts: 173
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #20 on: August 29, 2016, 08:58:07 am »
Thank you Circular for your replay

I guessed so, but I want to know what is the exact meaning of weight in this tree?? (math behind it). And what is the relationship between weight and a color in a TBGRAWeightedPaletteEntry??
I want to understand weight concept to know about how to gain a limited pallet, for example N must important colors in the bitmap visually.

Any help appreciated prior
Regards

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #21 on: August 29, 2016, 05:58:00 pm »
The weight is the number of pixels approximated by the leaf, when it is in the color tree.

If it is in the source color list, then it is the number of pixel in the source image that have the given color.

Conscience is the debugger of the mind

Sanem

  • Full Member
  • ***
  • Posts: 173
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #22 on: August 29, 2016, 09:03:45 pm »
The weight is the number of pixels approximated by the leaf, when it is in the color tree.

If it is in the source color list, then it is the number of pixel in the source image that have the given color.

So if the weight of a color in the source colors list, is the number of pixels that have that color, if I wanted to produce a limited pallet, for example, to change the way that dominant color is being calculated, by gaining a limited color pallet from source colors , can I gain that limited pallet by picking those colors with biggest weights?? Or I need to produce the tree and understand its process of calculating the colors and weights by the methods like median cut and etc??
Because I need limited pallet for other things that I want to do, like gaining colors difference (Do not know how yet!)

Any help appreciated prior
Regards
« Last Edit: August 29, 2016, 09:06:55 pm by simin_sh »

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #23 on: August 30, 2016, 03:48:49 pm »
Oh I see.

Ok I've added support for weights in approximation palette (on Git).

So now approximation palette contains the number of pixels in the source image.

You can also now do ReducedPalette.Weight[...] instead of going via GetAsArrayOfWeightedColor.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ColorPallet;
  2. var
  3.   PalletBmp: TBGRABitmap;
  4.   quant: TBGRAColorQuantizer;
  5.   i, PalletCount: integer;
  6. begin
  7.   PalletCount := 100;
  8.   PalletBmp := TBGRABitmap.Create('Pics' + PathDelim + 'Pic.png');
  9.   quant := TBGRAColorQuantizer.Create(PalletBmp, acFullChannelInPalette, PalletCount);
  10.   //Show the color pallet
  11.   for i := 0 to quant.ReducedPalette.Count - 1 do
  12.   begin
  13.     WriteLn('Weight of color [', i, '] : ', quant.ReducedPalette.Weight[i]);
  14.   end;
  15.   PalletBmp.Free;
  16.   quant.Free;
  17. end;
Conscience is the debugger of the mind

Sanem

  • Full Member
  • ***
  • Posts: 173
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #24 on: August 30, 2016, 04:06:53 pm »
Thank you Circular

Is there something wrong, for testing ,or something else, with gain the pallet??
Because before when I was gaining the pallet from same picture (720*340), it was taking 700 ms time, but now it is taking more than 9 seconds!

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #25 on: August 30, 2016, 05:27:31 pm »
No, nothing wrong with getting the array of colors.

Median cut was not using quicksort. Ok that's fixed on Git.
Conscience is the debugger of the mind

Sanem

  • Full Member
  • ***
  • Posts: 173
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #26 on: August 31, 2016, 09:05:43 am »
No, nothing wrong with getting the array of colors.

Median cut was not using quicksort. Ok that's fixed on Git.

Yes, Thank you Circular, It is working correctly now and it is taking less time, before when I tested Color quantizer for first time it was taking 3000 MS and now it is taking 700 MS for same picture (1920*1080 with ~65000 source colors). I have 2 questions:

1- Do you think there would be a way to reduce this time to ~ 0.05 of the time it is taking now?? that time would be great!

2- What is the way that source colors divide into branches for gaining the reduced color pallet?? I mean how it is clustering colors??

Any help appreciated prior
Regards
« Last Edit: August 31, 2016, 09:11:32 am by simin_sh »

circular

  • Hero Member
  • *****
  • Posts: 4196
    • Personal webpage
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #27 on: August 31, 2016, 08:03:35 pm »
1. I have thought about optimizing color quantization. There may be a method called oct-tree that could be faster.
2. Currently it is dividing according to a dimension (the simplest being red, green, blue and alpha). Before a leaf is split into a branch, it contains a set of pixels that can be represented as a shape in 4 dimensions (RGBA).
Conscience is the debugger of the mind

Sanem

  • Full Member
  • ***
  • Posts: 173
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #28 on: September 01, 2016, 08:52:18 am »
1. I have thought about optimizing color quantization. There may be a method called oct-tree that could be faster.
2. Currently it is dividing according to a dimension (the simplest being red, green, blue and alpha). Before a leaf is split into a branch, it contains a set of pixels that can be represented as a shape in 4 dimensions (RGBA).

Ok
But how they were divided??
What is the exact relationship between weight and the color pixel in the branches of the tree??
How does it clusters the colors to gain the pallet??
Does it use average of colors or something??
What is the exact math behind this clustering??


Any help appreciated prior
Regards
« Last Edit: September 04, 2016, 03:05:45 pm by simin_sh »

Sanem

  • Full Member
  • ***
  • Posts: 173
Re: How to Optimeze use of BGRAColorQuantizer??
« Reply #29 on: September 04, 2016, 03:05:55 pm »
I was reading BGRAColorQuantization unit and I was attempt to understand what does exactly going on in the function TBGRAColorQuantizer.GetPalette: TBGRACustomApproxPalette;
And in this function I saw that it calls many other functions conditionally, when I was reading it I was wondering what does exactly GetDimensionValue function in the begin of the unit BGRAColorQuantization, does?? (which is called indirectly from that GetPalette function) and for what is this??

Any help appreciated prior
Regards
« Last Edit: September 04, 2016, 03:08:54 pm by simin_sh »

 

TinyPortal © 2005-2018