Gray image pixel color brightness processing [original] vczhgame 2004-06-12 -------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------- I saw some The book on image processing has a great help to me. So, today I have sorted out the knowledge I have learned. On the one hand, I can give people to learn. On the other hand, you can also ask your guidance. The image processing I want to say is for the program. So, make a program to place graphics. Here, I used Delphi as a tool. Because, in the many compilers I have, Delphi supports the graphics. Also, here I am not talking about the syntax. So some code I will not explain. Sorry for any inconveniences caused. Note: The things used in this article are not exceeding the range of GDI. In image processing, the speed is important. Therefore, we have to reprocess Tbitmap and get TVCZHbitmap. This is just because getpixels and setpixels are too slow, change a method.
unit untBitmapProc; interfaceuses Graphics, SysUtils; type TVczhBitmap = class (TBitmap) private Data: PByteArray; Line: Integer; procedure SetFormat; function GetBytePointer (X, Y: Integer): PByte; procedure SetBytes (X, Y: Integer; Value: byte); function GetBytes (X, Y: Integer): byte; protected published constructor Create; public property Bytes [X, Y: Integer]: byte read GetBytes write SetBytes; procedure LoadFromFile (FileName: String); procedure ToGray; end; implementationprocedure TVczhBitmap.SetFormat; begin HandleType: = bmDIB; PixelFormat: = pf24bit; end; function TVczhBitmap.GetBytePointer (X, Y: Integer): PByte; begin if Line <> Y then begin Line: = Y; Data: = ScanLine [ Y]; end; longint (result): = longint (data) x; end; procedure tvzhbitmap.setbytes (x, y: integer; value: byte); begin getBytepointer (x, y) ^: = value; Function tvczhbitmap.getbytes (x, y: integer): Byte; begin result: = getBytepointer (x, y) ^; end; constructor tvczhbitmap.create; begin inherited create; setFormat; line: = - 1; e nd; procedure TVczhBitmap.LoadFromFile (FileName: String); begin inherited LoadFromFile (FileName); SetFormat; Line: = - 1; end; procedure TVczhBitmap.ToGray; var X, Y, R: Integer; B: Byte; begin for Y : = 0 to Height-1 Do Me X: = 0 to width-1 Do Begin R: = 0; for B: = 0 to 2 DO R: = R GetBytes (x * 3 b, y); for B : = 0 to 2 do setBytes (x * 3 b, y, r div 3); end; end; end. Since then, we need to build several forms. The first is used to display pictures, and the second is used to process pictures, and other forms are inherited from the second form, including actual processing methods.
Look at the second window: unit untProc; interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, untBitmapProc, StdCtrls, ComCtrls; type TfrmProcessor = class (TForm) pbBar: TPaintBox; gpProc: TGroupBox; Button1: TButton; procedure FormCreate (Sender: TObject); procedure FormDestroy (Sender: TObject); procedure FormShow (Sender: TObject); procedure pbBarPaint (Sender: TObject); procedure Button1Click (Sender: TObject); private {Private declarations } public {public declarations} BarData: array [0..255] of Byte; Bar: TVczhBitmap; procedure DrawBar; end; var frmProcessor: TfrmProcessor; implementation {$ R * .dfm} uses untViewer; procedure TfrmProcessor.DrawBar; var I : Integer; begin bar.canvas.FillRect (bar.canvas.clipRect); bar.canvas.moveto (0,255-bartata [0]); for i: = 1 to 255 do bar.canvas.lineto (i, 255-batata [I]); end; procedure tfrmprocessor.formcreate (sender: TOBJECT); begin bar: = tvczhbitmap.create; bar.width: = 256; bar .Height: = 256; Bar.Canvas.Brush.Color: = clWhite; Bar.Canvas.Brush.Style:=bsSolid;end;procedure TfrmProcessor.FormDestroy (Sender: TObject); begin Bar.Free; end; procedure TfrmProcessor. FormShow (Sender: TOBJECT); VAR i: Integer; Begin for i: = 0 to 255 Do Bardata [I]: = i; Drawbar; End; Procedure TFRMPROCESSOR.PBBARPAINT (Sender: Tobject); Begin Pbbar.canvas.DRAW 0,0, bar); end; procedure tfrmprocessor.button1click (sender: Tobject); var x, y: integer; begin for y: = 0 to buffer.Height-1 do for x: = 0 to buffer.width * 3 -1 Do Played.bytes [x, y]: = Bardata [buffer.bytes [x, y]]; frmviewer.formpaint (frmviewer); end; end, make a window inheritance from it, adjust Bardata [] After you press Apply to see the result. Image processing is now processed. See the sample program for the specific effect. First, the color reverses.
The color of the grayscale image is from 0 to 255, so in order to reverse the color, we can use 255 to subtract the color value to get the inversion. Var i: integer; begin inherited; for i: = 0 to 255 do bardata [i]: = 255-i; // minus the color value Drawbar; pbbarpaint (pbbar); END; II, reduce color range Enhanced or diminishing the brightness color is originally from 0 to 255. If it is adjusted, for example from 0 to 16, it will be significantly dimmed. We can set the start value to a, set the termination value to B, then the new color value new = a (b-1) * OLD / 255. If this is done, the brightness can be changed and the order in which the original color is not destroyed. The code is the following Var i: integer; Begin for i: = 0 to 255 do bardata [i]: = (255-sbmin.position) round ((sbmin.position-sbmax.position) / 255 * i); drawbar; pbbarpaint (PBBAR); Button1Click (Button1); END; sbmin.position and sbmaxposition here are reversed. So use 255 to reduce the three, increase the color range within a range If the color range of the image itself is small, you can increase the contrast of the image in this way, which is advantageous for the analysis of the image. Specific practice: Select a value A as the start value, select a value B as the termination value, then deform on the formula: | 0 (x <= a) f (x) = | 255 / (ba) * (XA) ), 255 (x> = b) VAR i: integer; begin for i: = 0 to 255 do begin if i <= sbmin.position kilse [i]: = 0 else if i> = sbmax.position thrartata [ I: = 255 else Bardata [I]: = Round (255 / (sbmax.position-sbmin.position) * (i-sbmin.position)); end; drawbar; pbbarpaint (pbbar); button1click (button1); end 4. When you use the third function, you will find that when B <= a, the color on the image is white in addition to the black. The advantage of this operation cannot be displayed directly. This is only possible to have a relatively advanced image processing such as edge detection. In this case, the formula of the third method can be reformable, so it is not described in detail. 5. The exponential brightness adjustment We assume that the defined domain of this figure is [0, 1], and the value field is also [0, 1]. Then, the definition function f (x) = x ^ c, then the image of F (X) has a paragraph. When we use the mouse to operate, it can be taken to P (A, B) above, and then f (x) passes Point P, then C = ln (b) / ln (a).