Using GDI + in Delphi to achieve image binarization

Source: Internet
Author: User

 

// 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.