From 64ba5b72ec902f636a7c8a4c78bdb275c69db736 Mon Sep 17 00:00:00 2001 From: Wizzup? Date: Mon, 21 Dec 2009 19:48:57 +0000 Subject: [PATCH] OCR inner api changes. Added ``suppport'' for shadows. Changed OCRBench a bit more. git-svn-id: http://www.villavu.com/repositories/merlijn/mufasa@308 3f818213-9676-44b0-a9b4-5e4c4e03d09d --- Projects/OCRBench/project1.lpi | 157 +++++++++++++++++-------- Projects/OCRBench/unit1.lfm | 13 +- Projects/OCRBench/unit1.pas | 16 ++- Projects/SAMufasaGUI/colourhistory.lfm | 4 +- Projects/SAMufasaGUI/colourhistory.lrs | 4 +- Units/MMLCore/ocr.pas | 70 ++++++----- Units/MMLCore/ocrutil.pas | 20 ++-- 7 files changed, 189 insertions(+), 95 deletions(-) diff --git a/Projects/OCRBench/project1.lpi b/Projects/OCRBench/project1.lpi index afa523a..13e10c9 100644 --- a/Projects/OCRBench/project1.lpi +++ b/Projects/OCRBench/project1.lpi @@ -7,7 +7,7 @@ - + @@ -31,12 +31,12 @@ - + - + @@ -44,10 +44,10 @@ - - + + - + @@ -58,8 +58,8 @@ - - + + @@ -77,110 +77,165 @@ - - + + - - - - + + + + - - - - + + + + + + + + + + + + + + + + + + + - + - + - + - - + + - + - + - + - - + + - - + + - + - - + + - + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Projects/OCRBench/unit1.lfm b/Projects/OCRBench/unit1.lfm index f24faa3..8f814a2 100644 --- a/Projects/OCRBench/unit1.lfm +++ b/Projects/OCRBench/unit1.lfm @@ -1,8 +1,9 @@ object Form1: TForm1 - Left = 492 + Left = 493 Height = 305 - Top = 266 + Top = 293 Width = 609 + ActiveControl = BitmapButton Caption = 'Form1' ClientHeight = 305 ClientWidth = 609 @@ -40,6 +41,14 @@ object Form1: TForm1 OnClick = PathButtonClick TabOrder = 2 end + object FShadow: TCheckBox + Left = 507 + Height = 20 + Top = 112 + Width = 79 + Caption = 'Shadow?' + TabOrder = 3 + end object OCRFileOpen: TOpenDialog Filter = '.bmp' left = 528 diff --git a/Projects/OCRBench/unit1.pas b/Projects/OCRBench/unit1.pas index c49adfb..2b24ad4 100644 --- a/Projects/OCRBench/unit1.pas +++ b/Projects/OCRBench/unit1.pas @@ -14,6 +14,7 @@ type TForm1 = class(TForm) BitmapButton: TButton; + FShadow: TCheckBox; PathButton: TButton; OCRButton: TButton; Image1: TImage; @@ -45,9 +46,10 @@ Var C: TClient; bmp: TMufasaBitmap; x,y: integer; + s: string; + Shadow: boolean; begin - writeln(BitmapPath); if not FileExists(BitmapPath) then begin MessageBox(0,pchar('You did not set a valid bitmap'), Pchar('Bitmap Error'), @@ -65,16 +67,24 @@ begin Exit; end; + Form1.Image1.Canvas.Brush.Color := 0; + Form1.Image1.Canvas.Rectangle(0, 0, Form1.Image1.Canvas.Width, Form1.Image1.Canvas.Height); + C := TClient.Create; bmp := TMufasaBitmap.Create; bmp.LoadFromFile(BitmapPath); C.MWindow.SetTarget(bmp); - C.MOCR.InitTOCR(UpTextPath + DS + '..' + DS); - writeln(C.MOCR.GetUpTextAt(7,7)); + + Shadow :=FShadow.Checked; + + C.MOCR.InitTOCR(UpTextPath + DS + '..' + DS, Shadow); + s := C.MOCR.GetUpTextAt(7,7, Shadow); for y := 0 to C.MOCR.debugbmp.Height - 1 do for x := 0 to C.MOCR.debugbmp.Width -1 do Form1.Image1.Canvas.Pixels[x,y] := C.MOCR.debugbmp.FastGetPixel(x,y); + Form1.Image1.Canvas.Font.Color:=clRed; + Form1.Image1.Canvas.TextOut(0, C.MOCR.debugbmp.Height, s); C.Free; end; diff --git a/Projects/SAMufasaGUI/colourhistory.lfm b/Projects/SAMufasaGUI/colourhistory.lfm index b64f2ff..c098773 100644 --- a/Projects/SAMufasaGUI/colourhistory.lfm +++ b/Projects/SAMufasaGUI/colourhistory.lfm @@ -1,7 +1,7 @@ object ColourHistoryForm: TColourHistoryForm - Left = 373 + Left = 374 Height = 250 - Top = 308 + Top = 335 Width = 490 ActiveControl = SelectionName Caption = 'Colour Picker History' diff --git a/Projects/SAMufasaGUI/colourhistory.lrs b/Projects/SAMufasaGUI/colourhistory.lrs index 675f742..278fb4b 100644 --- a/Projects/SAMufasaGUI/colourhistory.lrs +++ b/Projects/SAMufasaGUI/colourhistory.lrs @@ -1,8 +1,8 @@ { This is an automatically generated lazarus resource file } LazarusResources.Add('TColourHistoryForm','FORMDATA',[ - 'TPF0'#18'TColourHistoryForm'#17'ColourHistoryForm'#4'Left'#3'u'#1#6'Height'#3 - +#250#0#3'Top'#3'4'#1#5'Width'#3#234#1#13'ActiveControl'#7#13'SelectionName'#7 + 'TPF0'#18'TColourHistoryForm'#17'ColourHistoryForm'#4'Left'#3'v'#1#6'Height'#3 + +#250#0#3'Top'#3'O'#1#5'Width'#3#234#1#13'ActiveControl'#7#13'SelectionName'#7 +'Caption'#6#21'Colour Picker History'#12'ClientHeight'#3#227#0#11'ClientWidt' +'h'#3#234#1#21'Constraints.MinHeight'#3#200#0#20'Constraints.MinWidth'#3#234 +#1#4'Menu'#7#10'CHMainMenu'#8'OnCreate'#7#10'FormCreate'#6'OnHide'#7#15'UnSe' diff --git a/Units/MMLCore/ocr.pas b/Units/MMLCore/ocr.pas index e4e34be..67703d3 100644 --- a/Units/MMLCore/ocr.pas +++ b/Units/MMLCore/ocr.pas @@ -37,9 +37,9 @@ uses TMOCR = class(TObject) constructor Create(Owner: TObject); destructor Destroy; override; - function InitTOCR(path: string): boolean; - function getTextPointsIn(sx, sy, w, h: Integer): TNormArray; - function GetUpTextAt(atX, atY: integer): string; + function InitTOCR(path: string; shadow: Boolean): boolean; + function getTextPointsIn(sx, sy, w, h: Integer; shadow: boolean): TNormArray; + function GetUpTextAt(atX, atY: integer; shadow: boolean): string; private Client: TObject; OCRData: TocrDataArray; @@ -75,7 +75,7 @@ const Non optimised. ;-) } -function TMOCR.getTextPointsIn(sx, sy, w, h: Integer): TNormArray; +function TMOCR.getTextPointsIn(sx, sy, w, h: Integer; shadow: boolean): TNormArray; var bmp: TMufasaBitmap; x,y: integer; @@ -250,28 +250,42 @@ begin bmp.SaveToFile('/tmp/ocrdebug.bmp'); {$ENDIF} - for y := 0 to bmp.Height - 1 do - for x := 0 to bmp.Width - 1 do - begin - { if bmp.fastgetpixel(x,y) <> clPurple then + if shadow then + begin + for y := 0 to bmp.Height - 1 do + for x := 0 to bmp.Width - 1 do begin - bmp.FastSetPixel(x,y,0); - continue; - end; } - if bmp.fastgetpixel(x,y) = clPurple then - begin - bmp.FastSetPixel(x,y,0); - continue; + if bmp.fastgetpixel(x,y) <> clPurple then + begin + bmp.FastSetPixel(x,y,0); + continue; + end; end; - if bmp.fastgetpixel(x,y) = clOlive then + end else + begin + for y := 0 to bmp.Height - 1 do + for x := 0 to bmp.Width - 1 do begin - bmp.FastSetPixel(x,y,0); - continue; - end; - if bmp.fastgetpixel(x,y) = clSilver then - begin - bmp.FastSetPixel(x,y,0); - continue; + { if bmp.fastgetpixel(x,y) <> clPurple then + begin + bmp.FastSetPixel(x,y,0); + continue; + end; } + if bmp.fastgetpixel(x,y) = clPurple then + begin + bmp.FastSetPixel(x,y,0); + continue; + end; + if bmp.fastgetpixel(x,y) = clOlive then + begin + bmp.FastSetPixel(x,y,0); + continue; + end; + if bmp.fastgetpixel(x,y) = clSilver then + begin + bmp.FastSetPixel(x,y,0); + continue; + end; end; end; @@ -337,7 +351,7 @@ begin inherited Destroy; end; -function TMOCR.InitTOCR(path: string): boolean; +function TMOCR.InitTOCR(path: string; shadow: boolean): boolean; begin { This must be dynamic } @@ -345,17 +359,17 @@ begin result := true; OCRPath := path + DS; if DirectoryExists(path + DS + 'UpChars' + DS) then - OCRData[0] := ocrutil.InitOCR(path + DS + 'UpChars' + DS) + OCRData[0] := ocrutil.InitOCR(path + DS + 'UpChars' + DS, shadow) else result := false; if DirectoryExists(path + DS + 'StatChars' + DS) then - OCRData[1] := ocrutil.InitOCR(path + DS + 'StatChars' + DS) + OCRData[1] := ocrutil.InitOCR(path + DS + 'StatChars' + DS, shadow) else result := false; end; -function TMOCR.GetUpTextAt(atX, atY: integer): string; +function TMOCR.GetUpTextAt(atX, atY: integer; shadow: boolean): string; var n:Tnormarray; @@ -365,7 +379,7 @@ begin ww := 400; hh := 20; - n := getTextPointsIn(atX, atY, ww, hh); + n := getTextPointsIn(atX, atY, ww, hh, shadow); Result := ocrDetect(n, ww, hh, OCRData[0]); //writeln(result); end; diff --git a/Units/MMLCore/ocrutil.pas b/Units/MMLCore/ocrutil.pas index 1de71ea..e8d2573 100644 --- a/Units/MMLCore/ocrutil.pas +++ b/Units/MMLCore/ocrutil.pas @@ -53,8 +53,8 @@ type end; procedure findBounds(glyphs: TocrGlyphMaskArray; out width,height: integer); - function LoadGlyphMasks(path: string): TocrGlyphMaskArray; - function InitOCR(path: string): TocrData; + function LoadGlyphMasks(path: string; shadow: boolean): TocrGlyphMaskArray; + function InitOCR(path: string; shadow: boolean): TocrData; function GuessGlyph(glyph: TNormArray; ocrdata: TocrData): char; function PointsToNorm(points: TpointArray; out w,h: integer): TNormArray; function ImageToNorm(src: tRGBArray; w,h: integer): TNormArray; @@ -153,17 +153,22 @@ begin end; {This Loads the actual data from the .bmp, but does not init all fields} -function LoadGlyphMasks(path: string): TocrGlyphMaskArray; +function LoadGlyphMasks(path: string; shadow: boolean): TocrGlyphMaskArray; var strs: array of string; bmp: array of Tbmp; {-> TMufasaBitmap, and why use an array? } len,size,i,j: integer; color: tRGB; + shadow_i: byte; begin strs:= GetFiles(path,'bmp'); len:= length(strs); SetLength(result,len); SetLength(bmp,len); + if shadow then + shadow_i := 0 + else + shadow_i := 255; for i:= 0 to len-1 do begin bmp[i]:= ReadBmp(path + strs[i]); @@ -172,8 +177,9 @@ begin for j:= 0 to size-1 do begin color:= bmp[i].data[j]; - if (color.r = 255) and (color.g = 255) and (color.b = 255) then - //if (color.r = 255) and (color.g = 0) and (color.b = 0) then + { if (color.r = 255) and (color.g = 255 and not shadow_i) and + (color.b = 255 and not shadow_i) then} + if (color.r = 255) and (color.g = shadow_i) and (color.b = shadow_i) then result[i].mask[j]:= 1 else result[i].mask[j]:= 0; @@ -186,7 +192,7 @@ begin end; {Fully initalizes a TocrData structure, this is LoadFont or whatever, call it first} -function InitOCR(path: string): TocrData; +function InitOCR(path: string; shadow: boolean): TocrData; var masks: TocrGlyphMaskArray; t,b,l,r,w,h,mw: integer; @@ -195,7 +201,7 @@ var pos: integer; ascii: char; begin - masks:= LoadGlyphMasks(path); + masks:= LoadGlyphMasks(path, shadow); w:= 0; h:= 0; findBounds(masks,w,h);