Recent

Author Topic: Get amount of unique colors from picture  (Read 2291 times)

lulZghost

  • Guest
Get amount of unique colors from picture
« on: October 16, 2018, 03:34:39 pm »
For my project I need a function way to get the amount of unique colors from a single picture.
My Idea was to make an array of strings, go pixel by pixel, convert the color to string, check if the string already exists and if not add it to the array.

This is my code for it:

Code: Pascal  [Select][+][-]
  1. private
  2.   colorlist : array of string;    
  3.  
  4. ---------------------------------
  5.  
  6. function TFormMain.checkColors(): integer;
  7. var i, x, y, doesexist : integer;
  8.     colorstring : string;
  9. begin    
  10.     for y:=0 to image1.canvas.Height do
  11.      begin
  12.       for x:=0 to image1.canvas.Width do
  13.        begin
  14.         colorstring:=ColorToString(FPColorToTColor(image1.Canvas.Colors[x,y]));
  15.         for i:=0 to length(colorlist) do
  16.          begin
  17.           doesexist:=0;
  18.           if colorlist[i] = colorstring then doesexist:=1;
  19.          end;
  20.         if doesexist=0 then
  21.          begin
  22.           SetLength(colorlist, Length(colorlist)+1);
  23.           colorlist[High(colorlist)]:=colorstring;
  24.          end;
  25.        end;
  26.       end;
  27.  result:=length(colorlist);
  28. end;

SO, apparently this doesn't work, because I get this error

Code: Pascal  [Select][+][-]
  1. 000000010000AC15 668b41e8                 mov    -0x18(%rcx),%ax

I don't get any syntax errors, so somewhere I have another error, but I don't see it. Maybe someone else can spot the logic error in there, because I honestly don't know what the error could be.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: Get amount of unique colors from picture
« Reply #1 on: October 16, 2018, 03:35:58 pm »

Code: [Select]
for i:=0 to length(colorlist) do

Iterates over length(colorlist)+1, is this really what you want?

Also,  as a rule, don't convert to string what is not a string.

Blaazen

  • Hero Member
  • *****
  • Posts: 3239
  • POKE 54296,15
    • Eye-Candy Controls
Re: Get amount of unique colors from picture
« Reply #2 on: October 16, 2018, 04:31:36 pm »
I would allocate array with size 256*256*256/8 = 2MB, it will cover RGB values from (0,0,0),(0,0,1)...(255,255,254),(255,255,255) and each bit represents color: 0..not present, 1..present. In the end you can only do sum of "1" bits.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

LemonParty

  • Jr. Member
  • **
  • Posts: 63
Re: Get amount of unique colors from picture
« Reply #3 on: October 19, 2018, 10:57:56 am »
Using strings for such purpouses is ineffective.
Blaazen's idea is really good.
Use https://www.freepascal.org/docs-html/rtl/system/popcnt.html to count the number of setted bits.

 

TinyPortal © 2005-2018