// Grayscale or binarization Color Image
Procedure GrayImage (Image: TGpImage; Threshold: Single = 0.0 );
Const
ColorMatrix: TColorMatrix =
(0.3, 0.3, 0.3, 0.0, 0.0 ),
(0.59, 0.59, 0.59, 0.0, 0.0 ),
(0.11, 0.11, 0.11, 0.0, 0.0 ),
(0.0, 0.0, 0.0, 1.0, 0.0 ),
(0.0, 0.0, 0.0, 0.0, 1.0 ));
Var
Tmp: TGpImage;
Attr: TGpImageAttributes;
G: TGpGraphics;
Begin
Tmp: = Image. Clone;
G: = TGpGraphics. Create (Image );
Attr: = TGpImageAttributes. Create;
Try
Attr. SetColorMatrix (ColorMatrix); // grayscale
If Threshold> 0.0 then // if the Threshold value is given, binarization Based on the grayscale image
Attr. SetThreshold (Threshold );
G. DrawImage (Tmp, GpRect (0, 0, Image. Width, Image. Height ),
0, 0, Tmp. Width, Tmp. Height, utPixel, attr );
Finally
G. Free;
Attr. Free;
Tmp. Free;
End;
End;
Procedure TForm1.Button1Click (Sender: TObject );
Var
Image: TGpImage;
G: TGpGraphics;
Begin
Image: = tgpimage.create('.mediafruit.jpg ');
G: = TGpGraphics. Create (Handle, False );
G. DrawImage (Image, 10, 10, Image. Width, Image. Height );
GrayImage (images, 0.5 );
G. DrawImage (Image, 220, 10, Image. Width, Image. Height );
G. Free;
Image. Free;
End;
From the example, we can see that the binarization of a color image is to give a threshold value between 0 and 1 based on its grayscale. The so-called threshold value is the demarcation point of each color component. If the threshold is set to 0.7 and the red, green, and blue components in the current color are 230, 50, and 220 respectively, then the red component 230 is greater than 0.7x255. Therefore, the red component is changed to 255 (full brightness); the green component 50 is less than 0.7x255, so the green component is changed to 0; the blue component 220 is greater than 0.7x255, so, the blue component is changed to 255.
From this we can see that the key to the degree of binarization is the threshold value after the grayscale image is correctly obtained. Therefore, the theory of how to determine the threshold is also the most important theory of image binarization.