To make our contrast enhancement processing, we need to design the graphical user interface (GUI) first. The GUI is similar with our previous brightness modification user interface. The following steps explains how to make it:
- Open (or download first) our previous brightness modification project.
- Create a new form (File->New->Form).
- Change the name property to ContrastForm, change the Caption property to 'Contrast'. Set the FormStyle property to fsMDIChild. Resize the form to fit the main form (parent form).
- On the Object Inspector, click on Events tab, and double click in the OnClose event. You'll be directed to the event handler, and assign the Action variable with caFree between begin and end
procedure TContrastForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action:=caFree;
end; - Add two scroll bar components (from the standard component pallet) to the form. Change the Name property to CenterScrollBar and ContrastScrollBar. Set the Minimum property of CenterScrollBar to 0 and the Maximum to 255. Set the minimum property of ContrastScrollBar to -100 and the maximum to 100. Set the position of CenterScrollBar to 127, set the position of ContrastScrollBar to 0.
- Place two label components (from the standar component pallet) to the form, set the layout to associate each label to each scroll bar. Change the Label's caption to 'Center' and 'Contrast' accordingly (see Figure 1).
- Add two buttons (from standard component pallet), change their caption to OK and Cancel, and set their name to OKButton and CancelButton accordingly.
- Save the unit as ContrastUnit (using menu File->Save As).
- Go to Project->Options->Form, point to ContrastForm to select, and press the > button, so the ContrastForm will move to Available forms box, then press OK.
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 (SetContrast) to associate the image input to the brightness form is also needed.
- Edit the BrightnessForm abstraction to provide TImage (TemporaryImage and OriginalImage) object and the SetBrightness procedure. 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 we're gonna use TImage component. Edit as shown below:
type
TContrastForm = class(TForm)
CenterScrollbar: TScrollBar;
ContrastScrollBar: TScrollBar;
Label1: TLabel;
Label2: TLabel;
OKButton: TButton;
CancelButton: TButton;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
TemporaryImage:TImage;
OriginalImage:TImage;
Applied:boolean;
public
{ Public declarations }
procedure SetContrast(Image: TImage);
end; - Write the SetContrast procedure definition in the implementation section:
procedure TContrastForm.SetContrast(Image: TImage);
begin
try
begin
TemporaryImage:=Image;
TemporaryImage.Picture.Bitmap.PixelFormat:=pf24bit;
OriginalImage:=TImage.Create(self);
OriginalImage.Picture.Bitmap.Assign(Image.Picture.Bitmap);
end;
except
begin
Free; //free the contrast form
ShowMessage('Cannot complete the operation');
end;
end;
end; - Double click the CenterScrollBar component, the you'll be directed to its event handler, edit as shown below
procedure TContrastForm.CenterScrollbarChange(Sender: TObject);
var
i,j:Integer;
temp:real;
pixelPointer:PByteArray;
originalPixelPointer:PByteArray;
begin
try
begin
if TemporaryImage.Picture.Bitmap.PixelFormat=pf8bit then
for i:=0 to TemporaryImage.Picture.Height-1 do
begin
pixelPointer:=TemporaryImage.Picture.Bitmap.ScanLine[i];
originalPixelPointer:=OriginalImage.Picture.Bitmap.ScanLine[i];
for j:=0 to TemporaryImage.Picture.Width-1 do
begin
temp:=((originalPixelPointer[j]-CenterScrollBAr.Position)
*exp(ContrastScrollBar.Position/50))
+ CenterScrollBar.Position;
if temp<0>255 then temp:=255;
pixelPointer[j]:=round(temp);
end;
end;
if TemporaryImage.Picture.Bitmap.PixelFormat=pf24bit then
for i:=0 to TemporaryImage.Picture.Height-1 do
begin
pixelPointer:=TemporaryImage.Picture.Bitmap.ScanLine[i];
originalPixelPointer:=OriginalImage.Picture.Bitmap.ScanLine[i];
for j:=0 to TemporaryImage.Picture.Width-1 do
begin
temp:=((originalPixelPointer[3*j]-CenterScrollBAr.Position)
*exp(ContrastScrollBar.Position/50))
+ CenterScrollBar.Position;
if temp<0>255 then temp:=255;
pixelPointer[3*j]:=round(temp);
temp:=((originalPixelPointer[3*j+1]-CenterScrollBAr.Position)
*exp(ContrastScrollBar.Position/50))
+ CenterScrollBar.Position;
if temp<0>255 then temp:=255;
pixelPointer[3*j+2]:=round(temp);
temp:=((originalPixelPointer[3*j+2]-CenterScrollBAr.Position)
*exp(ContrastScrollBar.Position/50))
+ CenterScrollBar.Position;
if temp<0>255 then temp:=255;
pixelPointer[3*j+2]:=round(temp);
end;
end;
TemporaryImage.Refresh;
end;
except
begin
Free;
ShowMessage('Cannot complete the operation');
end;
end;
end;
In the code shown above, you can see the contrast gain is computed as exp(ContrastScrollBar.Position/50), with this formula, when the scroll bar position is zero (the default), the gain will be exp(0/50)=1, and at maximum the contrast gain will be exp(100/50)=100, and at the minimum the contrast gain will be exp(-100/50)=1/100. - On the ContrastForm, double clicks the OKButton component, you'll be directed to its event handler, then edit as shown below:
procedure TContrastForm.OKButtonClick(Sender: TObject);
begin
Applied:=true;
Close();
end; - Double click the CancelButton component in the ContrastForm, and you'll be directed to its event handler, edit as shown below:
procedure TContrastForm.CancelButtonClick(Sender: TObject);
begin
Applied:=false;
Close();
end; - To implement the decision whether the image on the source form will be modified or reverted to the original bitmap, edit the ContrastForm's OnClose event handler as shown below:
procedure TContrastForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if Applied=false then
TemporaryImage.Picture.Bitmap.Assign(
OriginalImage.Picture.Bitmap);
Action:=caFree;
end;
To show the contrast enhancement interface and to connect the active image form to the contrast GUI, we need to add a new item in the main menu. Double click the main menu component in the main form, and add a Contrast menu item under menu Image (Image->Contrast, see Figure 2.

Figure 2. Contrast Menu Item
Write the event handler for the menu as shown below:
procedure TMainForm.Contrast1Click(Sender: TObject);Don't forget to add ContrastUnit under the uses in the MainUnit's implementation. Use menu File->Use Unit (Alt+F11). Now you can save, compile and run the executable. The execution look like figure 3.
begin
if ImageForm<>nil then
begin
ImageForm:=TImageForm(ActiveMDIChild);
try
begin
Application.CreateForm(TContrastForm,ContrastForm);
ContrastForm.SetContrast(ImageForm.Image1);
end;
except
ContrastForm.Free;
ShowMessage('Cannot complete the operation');
end;
end;
end;
Source Code Download
The source code can be downloaded here for Delphi 7 project, and here for the Turbo Delphi Explorer source code. Besides contains new source codes for contrast enhancement, if you compare to our previous brightness modification project's source codes, you'll see that in the codes for the brightness manipulation codes has also been updated to accommodate both two different image formats, pf8bit (8 bit monochrom/gray sacle) and pf24bit (true color). When applied to a monochrome image, sliding one scroll bar will move other two scroll bars to reflect that monochrome image doesn't have any separate RGB components.


1 comments:
hi!! hello! i am josue , i need your help for a proyecct, a like to speak with you in gmail(jamesjara+gmail.com). Is a comparation 2 images, wich is the best way ,
Post a Comment