TImage32.ConvertToBoolMask

procedure ConvertToBoolMask(reference: TColor32; tolerance: integer; colorFunc: TCompareFunction; maskBg: TColor32 = clWhite32; maskFg: TColor32 = clBlack32);


Converts a colored image into a 2 colored mask image (default black on white).

The 'reference' color is compared with each pixel in the image using the supplied 'colorFunc' function. Matching pixels will be colored with 'maskFg' (default = black) and non-matching pixels will be colored 'maskBg' (default = white). The meaning of 'tolerance' will depend on the function. For example, using the CompareRGB function, pixels that deviate no more that 'tolerance' from the reference color in each of the three color channels will be considered a match.


  uses Image32, Image32_PNG, Image32_Vector, Image32_Extra;
  ...
  var 
    img: TImage32;
    paths: TArrayOfArrayOfPointD;
  begin
    img := TImage32.Create;
    img.LoadFromFile('beetle.png');
    //create a mask from pure black pixels, making black
    //pixels maroon while everything else becomes clear
    img.ConvertToBoolMask(clBlack32, 0, CompareRGB, clNone32, clMaroon32);
    img.SaveToFile('.\beetle2.png');
    
    //and to vectorize the result
    paths := Vectorize(img, $FF000000, CompareAlpha, $0);
    paths := RamerDouglasPeucker(paths, 2);
    paths := SmoothToBezier(paths, true, 3, 2);
    paths := FlattenCBezier(paths);
    img.Clear;
    DrawPolygon(img, paths, frEvenOdd, clMaroon32);
    img.SaveToFile('.\beetle3.png');
    
    img.Free;
      

Original:
Masked:
Masked & Vectorised:

See Also

ConvertToAlphaMask, SetRGB, GetBoolMask