ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Most frequent colors in a TImageEnView image?

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

View 
UserName:
Password:
Format  Bold Italicized Underline  Align Left Centered Align Right  Horizontal Rule  Insert Hyperlink   Browse for an image to attach to your post Browse for a zip to attach to your post Insert Code  Insert Quote Insert List
   
Message 

 

Emoji
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Black Eye [B)]
Frown [:(] Shocked [:0] Angry [:(!] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
PeterPanino Posted - Apr 28 2024 : 10:21:44
Is there a built-in method to get the 16 most frequent colors from a TImageEnView image in a TColor array?

In the meantime, I have created my own function to get the most frequent colors:

function GetMostFrequentColors(Image: TImageEnView; Count: Integer): TArray<TColor>;
// get the most frequent colors in a TImageEnView image
var
  Bitmap: Vcl.Graphics.TBitmap;
  x, y: Integer;
  Color: TColor;
  Dict: TDictionary<TColor, Integer>;
  PairList: TList<TPair<TColor, Integer>>;
  Pair: TPair<TColor, Integer>;
begin
  Dict := TDictionary<TColor, Integer>.Create;
  try
    // Use the internal bitmap of ImageEnView directly
    Bitmap := Image.IEBitmap.VclBitmap;

    // Process each pixel to calculate color frequency
    for y := 0 to Bitmap.Height - 1 do
    begin
      for x := 0 to Bitmap.Width - 1 do
      begin
        Color := Bitmap.Canvas.Pixels[x, y];
        if Dict.ContainsKey(Color) then
          Dict[Color] := Dict[Color] + 1
        else
          Dict.Add(Color, 1);
      end;
    end;

    // Prepare to find the most common colors
    PairList := TList<TPair<TColor, Integer>>.Create;
    try
      for Pair in Dict do
        PairList.Add(Pair);

      // Sort by frequency:
      PairList.Sort(System.Generics.Defaults.TComparer<TPair<TColor, Integer>>.Construct(
        function(const L, R: TPair<TColor, Integer>): Integer
        begin
          Result := R.Value - L.Value;
        end));

      // Extract the top 'Count' colors:
      SetLength(Result, PAMin(Count, PairList.Count));
      for x := 0 to High(Result) do
      begin
        Result[x] := PairList[x].Key;
        // Log each color to CodeSite for debugging:
        CodeSite.SendColor('Common Color ' + IntToStr(x+1) + ': ', PairList[x].Key);
      end;
    finally
      PairList.Free;
    end;
  finally
    Dict.Free;
  end;
end;


Is there a better function for this purpose in ImageEn?
1   L A T E S T    R E P L I E S    (Newest First)
xequte Posted - Apr 29 2024 : 00:16:17
Hi

This is available in next week's release, which you can use as follows:

// Show the 16 most used colors of an image
SetLength( domColors, 16 );
count := DestIEViewer.Proc.GetDominantColor( domColors, 16 );
IEDrawColorPalette( ImageEnView1.IEBitmap, domColors, 30, 30, 8, clWhite, clBlack, count );
ImageEnView1.Update();


https://www.imageen.com/help/TImageEnProc.GetDominantColor.html

Nigel
Xequte Software
www.imageen.com