Threshold Below, Threshold Above, and Threshold Inside Implementation in Delphi


Image Format

Image thresholding process change the image to binary value, zero or one. Ideally, this binary data is stored in the computer memory as bit format, where 1 byte memory could store 8 pixels. Unfortunately, the modern computer hardware organization and software (compiler) technology doesn't handle the bit operation as fast and efficient as byte operation, so it is better to use ordinary 8bit or 24bit for practical reason, although it use actually only the least significant bit to store zero or one.

The Graphical User Interface (GUI)
To implement threshold various basic thresholding (below, above, and inside thresholding points), we need to design the graphical user interface first.
  1. Open our previous RGB to gray scale conversion project.
  2. Create a new form, resize the form to fit the main form. Set the name property to ThresholdForm, and set the FormStyle to fsMDIChild. Save the unit as ThresholdUnit.pas.
  3. On the menu Project->Options->Forms, select ThresholdForm (in the auto created form list) and click the > button, so the ThresholdForm will move from auto created form list to available forms list, then press OK.
  4. Add two CheckBoxes (from standard component pallet) to the form, set the name properties to InvertCheckBox and ThresholdInsideCheckBox. Set the captions to "Invert" and "Threshold Inside" respectively.
  5. Add two ScrollBars (from standard component pallet) to the form. Set the name properties to LowScrollBar and HighScrollBar. Set their maximum properties to 255. Set their position properties to 127. Add two labels to address them with "low" and "high" (see figure 1).
  6. Add two buttons to the form, set their name properties to OKButton and CancelButton, and set their caption properties to OK and Cancel accordingly.
To modify the image, it's common to backup the original image, so we can do a cancel operation after displaying the change on the original image form. To do this, we have to provide TImage object in the form to temporarily store the image. A procedure (SetThreshold) to associate the image input to the threshold form is also needed. To do this, edit the ThresholdForm abstraction (in the ThresholdUnit.pas) to provide TImage (TemporaryImage and OriginalImage) object and the SetThreshold procedure declaration. A boolean flag (Applied) is also needed to mark whether a Cancel or OK button has been pressed. Add ExtCtrls under uses in the Interface section because use TImage component. After we edit the ThresholdForm abstraction, the code should look like below:
interface

uses
ExtCtrls, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TThresholdForm = class(TForm)
LowScrollBar: TScrollBar;
HighScrollBar: TScrollBar;
InvertCheckBox: TCheckBox;
ThresholdInsideCheckBox: TCheckBox;
Label1: TLabel;
Label2: TLabel;
OKButton: TButton;
CancelButton: TButton;
private
TemporaryImage:TImage;
OriginalImage:TImage;
Applied:boolean;
public
procedure SetThreshold(Image: TImage);
{ Public declarations }
end;
The Implementation
(to be continued)

2 comments:

Cesar said...

Show de bola toda estas informações, mas vejo que parou de postar a continuação do Threshold por quê?

Gururaj said...

It will be great if bit can be handled faster than bytes by computers