A tutorial on the image recognition of Web page verification Code on Delphi WebBrowser control (I.)

Source: Internet
Author: User

Step one: Get the URL address of the CAPTCHA picture in the Web pageAdd a bitbtn and a memo to the Delphi and a WebBrowser control to implement the URL address of the verification code picture in the Web page, the following procedures are obtained:
Procedure Tform1.bitbtn1click (sender:tobject); var i:integer;begin for I:=0 to Webbrowser1.oleobject.document.images.length-1 do Memo1.Lines.Add (WebBrowser1.OleObject.document.images.item (i). href); end;

Step Two: Download the verification Code image in the page to image
Add a bitbtn and an image and WebBrowser control to the Delphi image to display the downloaded image
Code:
Procedure Tform1.bitbtn5click (Sender:tobject);
Var
I:integer;
Rang:ihtmlcontrolrange;
s:string;
Begin
Try
S: = (IHTMLDocument2 (webbrowser.document). Images.item (' Img_validatecode ', Emptyparam) as IHTMLElement). getattribute (' src ', 0);
Rang:= ((IHTMLDocument2 (webbrowser.document). Body as HTMLBody). createControlRange) as Ihtmlcontrolrange;
Rang.add (IHTMLDocument2 (webbrowser.document) images.item (' Img_validatecode ', Emptyparam) as IHTMLControlElement);
Rang.execcommand (' Copy ', false,0);
Image1.Picture.Assign (clipboard);//If the CLIPBRD unit is added, the compilation will prompt clipboard to define
Except
End
End
Attention:
1, in the program uses must first join the ACTIVEX,CLIPBRD unit, wherein the CLIPBRD does not join will cause clipboard undefined
2, Img_validatecode for the page verification code image ID number,
3, the above code for the Delphi WebBrowser in the general code, use only need to modify the ID number and uses add the unit can be used
Step three: Grayscale processing of image images
First, we need to know that the grayscale processing of the image is to turn some color verification code image into a gray image of the subsequent processing of convenient images, there are three ways to gray processing of the picture.
Method One: The average of the three pixels of RGB is calculated, then the average value is divided into three pixels;
Method Two: The maximum value of RGB three components per pixel is calculated, then the maximum value is divided into three pixels.
Method Three: According to YUV color space, to find out the value of y;
method One:
Code:
Procedure Tform1.bitbtn4click (sender:tobject); var P:pbytearray; Bit pointer x,y:integer;//x wide, y high bmp:tbitmap;//Bitmap Component (TBITMAP)gray:integer;//grayscale value begin BMP: = tbitmap.create;//Establish a tbitmap bmp.assign (FORM1.IMAGE1.PICTURE.BITMAP);// Convert image Image to bitmap mode bmp.pixelformat: = Pf24bit; Set to 24-bit color bitmap, PixelFormatthe memory format and color depth of the bitmap, a total of 9 valuesFor y: = 0 to Bmp.height-1 do begin P: =bmp.scanline[y];//scanline method forget pixel color valuesFor x:=0 to Bmp.width-1 do begin gray:= (P[3*x+2]+p[3*x+1]+p[3*x]) div 3;//3 The average value of the pixel points p[3*x+2]:=byte (Gray);      P[3*x+1]:=byte (Gray);    P[3*x]:=byte (Gray);  End  End Image4.  Picture.bitmap: = BMP; bmp.free;//release tbitmapend;
Original:
After processing:

Method Two:
Code:
Procedure Tform1.bitbtn4click (sender:tobject); var P:pbytearray;  Bit pointer x,y:integer;//x wide, y high bmp:tbitmap;//bitmap component (TBITMAP) gray:integer;//grayscale value begin BMP: = tbitmap.create;//set up a Tbitmap Bmp.assign (FORM1.IMAGE1.PICTURE.BITMAP);//convert image Image to bitmap mode bmp.pixelformat: = Pf24bit; Set to a 24-bit color bitmap, PixelFormat is the memory format and color depth of the bitmap, a total of 9 values for y: = 0 to Bmp.height-1 do begin P: =bmp.scanline[y];//scanline method is used to get  Pixel color value for x:=0 to Bmp.width-1 do begin Gray:=max (p[3*x+2],p[3*x+1]);      Use the Max function Asahi to define the math unit Gray:=max (gray,p[3*x]);      P[3*x+2]:=byte (Gray);      P[3*x+1]:=byte (Gray);    P[3*x]:=byte (Gray);  End  End Image4.  Picture.bitmap: = BMP; bmp.free;//release tbitmapend;
Original:
After processing:
method Three: Apply the formula y=0.299r+0.857g+0.113b
Procedure Tform1.bitbtn4click (sender:tobject); var P:pbytearray;  Bit pointer x,y:integer;//x wide, y high bmp:tbitmap;//bitmap component (TBITMAP) gray:integer;//grayscale value begin BMP: = tbitmap.create;//set up a Tbitmap Bmp.assign (FORM1.IMAGE1.PICTURE.BITMAP);//convert image Image to bitmap mode bmp.pixelformat: = Pf24bit; Set to a 24-bit color bitmap, PixelFormat is the memory format and color depth of the bitmap, a total of 9 values for y: = 0 to Bmp.height-1 do begin P: =bmp.scanline[y];//scanline method is used to get Pixel color value for x:=0 to Bmp.width-1 do begin Gray:=round (p[3*x+2]*0.3+p[3*x+1]*0.59+p[3*x]*0.11);//Formula y=0.299r+0.857g+      0.113B P[3*x+2]:=byte (Gray);      P[3*x+1]:=byte (Gray);    P[3*x]:=byte (Gray);  End  End Image4.  Picture.bitmap: = BMP; bmp.free;//release tbitmapend;
Original:
After processing:

Step four: Two value of image image
before the image binary needs to grayscale processing, the binary principle: by setting the threshold (threshold), the gray image into only 0 or one to represent the image target and background, where the image target is mostly digital, letters, set to 1, the background is set to 0. In the bitmap 0 corresponds to the 0,1 255, the binary method is many, but the emphasis is on the selection of the threshold value. threshold and local threshold value.
      global The thresholds are divided, such as fixed       Local threshold, (specific local )
Code:
Procedure Tform1.bitbtn4click (sender:tobject); var P:pbytearray;  Bit pointer x,y:integer;//x wide, y high bmp:tbitmap;//bitmap component (TBITMAP) gray:integer;//grayscale value begin BMP: = tbitmap.create;//set up a Tbitmap Bmp.assign (FORM1.IMAGE1.PICTURE.BITMAP);//convert image Image to bitmap mode bmp.pixelformat: = Pf24bit; Set to a 24-bit color bitmap, PixelFormat is the memory format and color depth of the bitmap, a total of 9 values for y: = 0 to Bmp.height-1 do begin P: =bmp.scanline[y];//scanline method is used to get Pixel color value for x:=0 to Bmp.width-1 do begin Gray:=round (p[3*x+2]*0.3+p[3*x+1]*0.59+p[3*x]*0.11);//Formula y=0.299r+0.857g+ 0.113B//Image binary if Gray > then//threshold value ofBegin p[3*x+2]:=255;        p[3*x+1]:=255;      p[3*x]:=255;        end ELSE begin p[3*x+2]:=0;        p[3*x+1]:=0;      p[3*x]:=0; End
Split line-------------------------------------end;  End Image4.  Picture.bitmap: = BMP; bmp.free;//release tbitmapend; original:   
after processing:
threshold, and then determine whether the RGB value of each point is greater or less than The threshold value is greater than 255 or black, less than   Qq:673890989 welcomes interested partners to exchange   Experience

A tutorial on the image recognition of Web page verification Code on Delphi WebBrowser control (I.)

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.