### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: % of color  (Read 5211 times)

#### xinyiman

• Hero Member
• Posts: 2075
##### % of color
« on: January 27, 2016, 09:40:54 am »
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
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

#### GetMem

• Hero Member
• Posts: 3676
##### Re: % of color
« Reply #1 on: January 27, 2016, 10:16:24 am »
I would use BGRABitmap because it's fast:
Code: Pascal  [Select][+][-]
1. uses BGRABitmap, BGRABitmapTypes;
2.
3. procedure TForm1.Button1Click(Sender: TObject);
4. var
5.   X, Y: Integer;
6.   P: PBGRAPixel;
7.   BGRABitmap: TBGRABitmap;
8. begin
9.   BGRABitmap := TBGRABitmap.Create(Image1.Picture.Bitmap);
10.   try
11.     for Y := 0 to BGRABitmap.Height - 1 do
12.     begin
13.       P := BGRABitmap.Scanline[Y];
14.       for X := 0 to BGRABitmap.Width - 1 do
15.       begin
16.         //acces pixel here  P^.Red; P^.Green; P^.Blue then calculate percentage
17.         Inc(P);
18.       end;
19.     end;
20.   finally
21.     BGRABitmap.Free;
22.   end;
23. end;

#### wp

• Hero Member
• Posts: 7090
##### Re: % of color
« Reply #2 on: January 27, 2016, 10:31:01 am »
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?
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

#### xinyiman

• Hero Member
• Posts: 2075
##### Re: % of color
« Reply #3 on: January 27, 2016, 11:34:50 am »
Thanks, my actual solution is:

Code: Pascal  [Select][+][-]
1.
2. function ColorInPerc(MyColor: TColor; MyCanvas: TCanvas; MyLarg: integer; MyAlt: integer): single;
3. var
4.    ret: single;
5.    numColor, numTot: integer;
6.    i,j: LongInt;
7. begin
8.      ret:=0;
9.      numColor:=0;
10.      numTot:=0;
11.      for i:=0 to MyLarg-1 do
12.          for j:=0 to MyAlt-1 do
13.          begin
14.               if MyCanvas.Pixels[i,j]=MyColor then
15.                  Inc(numColor);
16.               Inc(numTot);
17.          end;
18.
19.      ret:=(numColor*100)/numTot;
20.      Result:=ret;
21. end;
22.
23.
24.

But I do not know how to tell him that I want to take all kinds of orange.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

#### GetMem

• Hero Member
• Posts: 3676
##### Re: % of color
« Reply #4 on: January 27, 2016, 11:53:24 am »
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][+][-]
1. function IsOrange(R, G, B: Byte): Boolean;
2. begin
3.   Result := (R = 255) and (G >= 69 and G <= 165) and (B = 0);
4. end;

#### xinyiman

• Hero Member
• Posts: 2075
##### Re: % of color
« Reply #5 on: January 27, 2016, 12:02:51 pm »
Thank you
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

#### xinyiman

• Hero Member
• Posts: 2075
##### Re: % of color
« Reply #6 on: January 27, 2016, 12:14:17 pm »
And how can I convert my TColor in 3-byte RGB
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

#### rvk

• Hero Member
• Posts: 4066
##### Re: % of color
« Reply #7 on: January 27, 2016, 12:21:51 pm »
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.

Look at the Graphics unit:
http://wiki.freepascal.org/Colors

Code: Pascal  [Select][+][-]
1. function Blue(rgb: TColor): BYTE; // does not work on system color
2. function Green(rgb: TColor): BYTE; // does not work on system color
3. function Red(rgb: TColor): BYTE; // does not work on system color
4. function RGBToColor(R, G, B: Byte): TColor;
5. procedure RedGreenBlue(rgb: TColor; out Red, Green, Blue: Byte); // does not work on system color
6. function FPColorToTColor(const FPColor: TFPColor): TColor;
7. function TColorToFPColor(const c: TColor): TFPColor; // does not work on system color
8. function IsSysColor(AColor: TColorRef): Boolean;

#### circular

• Hero Member
• Posts: 3312
##### Re: % of color
« Reply #8 on: January 27, 2016, 08:24:58 pm »
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"...
Conscience is the debugger of the mind

#### wp

• Hero Member
• Posts: 7090
##### Re: % of color
« Reply #9 on: January 27, 2016, 08:33:54 pm »
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.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

#### avra

• Hero Member
• Posts: 1887
##### Re: % of color
« Reply #10 on: January 28, 2016, 11:58:32 am »
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?
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
« Last Edit: January 28, 2016, 12:05:47 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib