% of color lazarus / 39-Graphics ---------------------------------------------------------------------- xinyiman · 2016-01-27 08:40 Hello guys, who explained to me how to know the percentage of the presence of an image of a specific color. For example I want to know what red is a percentage of an image. How can I do? Thank you ---------------------------------------------------------------------- balazsszekely · 2016-01-27 09:16 I would use BGRABitmap because it's fast: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses BGRABitmap, BGRABitmapTypes; procedure TForm1.Button1Click(Sender: TObject);var  X, Y: Integer;  P: PBGRAPixel;  BGRABitmap: TBGRABitmap;begin  BGRABitmap := TBGRABitmap.Create(Image1.Picture.Bitmap);  try    for Y := 0 to BGRABitmap.Height - 1 do    begin      P := BGRABitmap.Scanline[Y];    for X := 0 to BGRABitmap.Width - 1 do      begin        //acces pixel here  P^.Red; P^.Green; P^.Blue then calculate percentage      Inc(P);      end;    end;  finally    BGRABitmap.Free;  end;end; ---------------------------------------------------------------------- wp · 2016-01-27 09:31 Your question is not clear: You want the perentage of "red"? What is "red"? Full red, ie. R=255, G=B=0? Or all shades of red, R = 1..255, G=0, B = 0, or maybe R=1..255, G=B? Or the red component of every color? ---------------------------------------------------------------------- xinyiman · 2016-01-27 10:34 Thanks, my actual solution is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  function ColorInPerc(MyColor: TColor; MyCanvas: TCanvas; MyLarg: integer; MyAlt: integer): single;var   ret: single;   numColor, numTot: integer;   i,j: LongInt;begin     ret:=0;     numColor:=0;     numTot:=0;     for i:=0 to MyLarg-1 do         for j:=0 to MyAlt-1 do         begin              if MyCanvas.Pixels[i,j]=MyColor then               Inc(numColor);              Inc(numTot);         end;      ret:=(numColor*100)/numTot;     Result:=ret;end;    But I do not know how to tell him that I want to take all kinds of orange. ---------------------------------------------------------------------- balazsszekely · 2016-01-27 10:53 First of all you can convert between TColor and RGB. Secondly, according to the net: RGB(Orange)      -> (255, 165, 0) RGB(Dark Orange) -> (255, 140, 0) RGB(Red Orange)  -> (255,  69, 0) so: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function IsOrange(R, G, B: Byte): Boolean;begin  Result := (R = 255) and (G >= 69 and G <= 165) and (B = 0);end; You can add more shades if you like. ---------------------------------------------------------------------- xinyiman · 2016-01-27 11:02 Thank you ---------------------------------------------------------------------- xinyiman · 2016-01-27 11:14 And how can I convert my TColor in 3-byte RGB ---------------------------------------------------------------------- rvk · 2016-01-27 11:21 I also would call this orangy: http://www.color-hex.com/color/e59400 which is #E59400 (229,148,0) Color-determination is a really tricky subject. You need to determine how far you want to go with each value of Red, Green and Blue. For your TColor question: Look at the Graphics unit: http://wiki.freepascal.org/Colors Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Blue(rgb: TColor): BYTE; // does not work on system colorfunction Green(rgb: TColor): BYTE; // does not work on system colorfunction Red(rgb: TColor): BYTE; // does not work on system colorfunction RGBToColor(R, G, B: Byte): TColor;procedure RedGreenBlue(rgb: TColor; out Red, Green, Blue: Byte); // does not work on system colorfunction FPColorToTColor(const FPColor: TFPColor): TColor;function TColorToFPColor(const c: TColor): TFPColor; // does not work on system colorfunction IsSysColor(AColor: TColorRef): Boolean; ---------------------------------------------------------------------- circular · 2016-01-27 19:24 To determine the hue of the color, you can call BGRAToHSLA or BGRADiff (using BGRABitmap). BGRAToHSLA returns the hue/saturation/lightness components. So you could check if the values are within a specified range. For example, you can get the values from a test color, and then compare the values. The function HueDiff helps to compute the value of two hues. Otherwise you can call BGRADiff with two colors, and it returns a value that expresses the difference. So if the difference is less than a certain value, you could count it as "orange", or "red"... ---------------------------------------------------------------------- wp · 2016-01-27 19:33 But just for calculation of the hue of a color it is not necessary to add a requirement for a third-party library. Use ColorToHLS or RGBToHLS from GraphUtil, instead. ---------------------------------------------------------------------- avra · 2016-01-28 10:58 Quote from: xinyiman on January 27, 2016, 09:40:54 amHello guys, who explained to me how to know the percentage of the presence of an image of a specific color. For example I want to know what red is a percentage of an image. How can I do?If you are serious about human like ways to determine color in an image, the best way would be to use fuzzy logic. Dark orange or light brown is a little different color for different people, but a general rule can be written in words, and if descriptive rules can be created, accurate color detection can be implemented in fuzzy logic. It is a lot of reading and study if you are new to this area. There are many fuzzy logic tutorials and ways to implement it in a programming language. You might want to look at following link to read about solving your specific problem: http://www.google.rs/search?q=fuzzylogic+color+detection