How to do Simple Image Binarization using Embarcadero C++ Builder XE5?
How to do simple image binarization using Embarcadero C++ Builder XE5?¶
Introduction
Sometimes, we need to transform color images to monochrome image. There is one way that it is very simple to help you to do. You can download source code and binary files. Even I refer to OpenCV book, but I think it is so easy. I decide to implement it by myself.
Equipment
Operation System: Microsoft Windows 7 (64 bit)
Development Utility: Embarcadero C++ Builder XE5
Usage
//--------------------------------------------------------------------------- #include#pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; // Create three points of TBitmap object Graphics::TBitmap *TheBitmap, *TempBitmap, *OriginBitmap; // Create a threshold. int Threshold; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Exit1Click(TObject *Sender) { // Exit the program Close(); } //--------------------------------------------------------------------------- void __fastcall TForm1::OpenFile1Click(TObject *Sender) { // If you got a file of the picture, you are able to load it. if ( OpenPictureDialog1->Execute() ) { // Disable automatic resize. Image1->AutoSize=false; // Enable an automatic stretch. Image1->Stretch=true; // Loads a picture file. Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName); // Set a point to the picture of loading. TheBitmap=Image1->Picture->Bitmap; // Create a new TBitmap object to keep original picture. OriginBitmap = new Graphics::TBitmap(); // To keep original picture. OriginBitmap->Assign(TheBitmap); // Set up TImage object to keep original picture. Image1->Picture->Bitmap->Assign(TheBitmap); // If you open picture file successful and then set a flag. // The means that you have been opened a file. OpenFile = 1; // Set the threshold is 100. Threshold = 100; // Set the position of the scroll bar is 100. ScrollBar1->Position = Threshold; } } //--------------------------------------------------------------------------- void __fastcall TForm1::ScrollBar1Change(TObject *Sender) { // If you have not opened a file, the flag will be zero, and then returns. if (OpenFile == 0) { return; } Byte *ptr, *tptr; // To get a counter of binarization from the scroll bar. Threshold = (int) ScrollBar1->Position; // Set up the TImage restore to the original picture. Image1->Picture->Bitmap->Assign(OriginBitmap); // To do the times of binarization TempBitmap = new Graphics::TBitmap(); // To get a current bitmap. TempBitmap->Assign(TheBitmap); // To do scan line of the whole picture. for (int y=0; y < TheBitmap->Height; y++) { // Set Y position of the image point. ptr = (Byte*) TheBitmap->ScanLine[y]; // Set Y position of the image point. tptr = (Byte*) TempBitmap->ScanLine[y]; // To do scan X-axis of the line. for (int x=0; x < TheBitmap->Width; x++) { // if the pixel more than the threshold, it should be white. if (tptr[x] > Threshold) { ptr[x] = (Byte) 255; } // Otherwise, it should be black. else { ptr[x] = (Byte) 0; } } // End x } // End y // Release the temporal bitmap. delete TempBitmap; // Refresh and draw the TImage object Repaint(); // Set result of binarization of the picture. Image1->Picture->Bitmap->Assign(TheBitmap); } //---------------------------------------------------------------------------
Download source code and binary files:
https://drive.google.com/file/d/0BzHb_OyLyVZlZUhaZlZ0eXZmNEk/view?usp=sharing
Exception
If you encounter the error, don’t worry. You just step by step to solve it.
Reference
[1] Gary Bradski and Adrian Kaehler, “Learning OpenCV: Computer Vision with the OpenCV Library,” O’REILLY, September 2008, ISBN:978-0-596-51613-0
Acknowledge
Thank you (Embarcadero C++ Builder XE5, Lenna Sjööblom) very much for this great development utility and beautiful photo.