usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Collections;usingSystem.Diagnostics;usingSystem.Drawing.Imaging;//form CallsPrivateBitmap rotateimage (Bitmap bmp,Doubleangle) {Graphics g=NULL; Bitmap tmp=NewBitmap (BMP. Width, BMP. Height, Pixelformat.format32bpprgb); Tmp. Setresolution (BMP. HorizontalResolution, BMP. VerticalResolution); G=graphics.fromimage (TMP); Try{g.fillrectangle (Brushes.white,0,0, BMP. Width, BMP. Height); G.rotatetransform ((float) angle); G.drawimage (BMP,0,0); } finally{g.dispose (); } returntmp; } Private voidButton1_Click (Objectsender, EventArgs e) { stringFnin ="f:\\test\\image0097_4.tif"; stringFnout ="f:\\test\\output.tif"; Bitmap Bmpin=NewBitmap (Fnin); Gmsedeskew SK=NewGmsedeskew (Bmpin); DoubleSkewangle =SK. Getskewangle (); Bitmap Bmpout= Rotateimage (Bmpin,-skewangle); Bmpout.save (Fnout, Imageformat.tiff);//easy to save here, can be saved by compression method }#regionAlgorithm Processing class Public classGmsedeskew { Public classHougline {//Count of points in the line. Public intCount; //Index in Matrix. Public intIndex; //The represented as all x, Y, solve Y*cos (alpha)-x*sin (Alpha) =d Public DoubleAlpha; Public DoubleD; } Bitmap cbmp; DoubleCalphastart =- -; DoubleCalphastep =0.2; intCsteps = +*5; Double[] Csina; Double[] ccosa; Doublecdmin; DoubleCdstep =1; intCdcount; //Count of points this fit in a line. int[] Chmatrix; Public DoubleGetskewangle () {gmsedeskew.hougline[] HL=NULL; inti =0; Doublesum =0; intCount =0; //Hough TransformationCalc (); //Top of the detected lines in the image.HL = GetTop ( -); //Average Angle of the lines for(i =0; I <= +; i++) {sum+=Hl[i]. Alpha; Count+=1; } returnSum/count; } PrivateHougline[] GetTop (intCount) {hougline[] HL=NULL; inti =0; intj =0; Hougline tmp=NULL; intAlphaindex =0; intDindex =0; HL=NewHougline[count +1]; for(i =0; I <= Count-1; i++) {Hl[i]=NewHougline (); } for(i =0; I <= chmatrix.length-1; i++) { if(Chmatrix[i] > Hl[count-1]. Count) {Hl[count-1]. Count =Chmatrix[i]; Hl[count-1]. Index =i; J= Count-1; while(J >0&& Hl[j]. Count > Hl[j-1]. Count) {tmp=Hl[j]; HL[J]= Hl[j-1]; Hl[j-1] = tmp; J-=1; } } } for(i =0; I <= Count-1; i++) {Dindex= Hl[i]. Index/csteps; Alphaindex= Hl[i]. Index-dindex *csteps; Hl[i]. Alpha=Getalpha (Alphaindex); HL[I].D= Dindex +cdmin; } returnHL; } PublicGmsedeskew (Bitmap bmp) {cbmp=bmp; } Private voidCalc () {intx =0; inty =0; intHmin = cbmp.height/4; intHMax = Cbmp.height *3/4; Init (); for(y = hmin; y <= hMax; y++) { for(x =1; X <= Cbmp.width-2; X + +) { //Only lower edges is considered. if(Isblack (x, y)) {if(! Isblack (x, y +1) {Calc (x, y); } } } } } Private voidCalc (intXinty) {intAlpha =0; DoubleD =0; intDindex =0; intIndex =0; for(alpha =0; Alpha <= Csteps-1; alpha++) {D= y * Ccosa[alpha]-X *Csina[alpha]; Dindex= (int) Calcdindex (d); Index= Dindex * Csteps +Alpha; Try{Chmatrix[index]+=1; } Catch(Exception ex) {Debug.WriteLine (ex). ToString ()); } } } Private DoubleCalcdindex (Doubled) {returnConvert.ToInt32 (D-cdmin); } Private BOOLIsblack (intXinty) {Color c=default(Color); DoubleLuminance =0; C=cbmp.getpixel (x, y); Luminance= (C.R *0.299) + (C.G *0.587) + (C.B *0.114); returnLuminance < $; } Private voidInit () {inti =0; DoubleAngle =0; //precalculation of sin and Cos.Csina =New Double[Csteps]; Ccosa=New Double[Csteps]; for(i =0; I <= csteps-1; i++) {Angle= Getalpha (i) * Math.PI/180.0; Csina[i]=Math.sin (angle); Ccosa[i]=Math.Cos (angle); } //Range of D:Cdmin =-Cbmp.width; Cdcount= (int)(2* (Cbmp.width + cbmp.height)/cdstep); Chmatrix=New int[Cdcount * csteps +1]; } Public DoubleGetalpha (intIndex) { returnCalphastart + Index *Calphastep; } }#endregion
The specific algorithm is: Starting from the left edge of the scan, from the beginning to find the melanin to the average, the length of the distance and the height of the alleys through the right triangle function to convert, so you know the angle of tilt.
C # implements bitmap image rectification