Jump to content

My look, and problem.. with basic OCR


Recommended Posts

Hi, I'm new to this forum, though I've been reading a lot of the posts to get used to the language. I learn quickly, and have been working on this character recognition project for about a week now. Using multiple methods found here and through much trial and error, I've come up with an app that looks at a user-defined area of pixels, in ms paint (for now), and determines (based on the background color given) how many pixels are NOT the background color. The app uses a tooltip message for debugging.

Here's an example of it working:

Posted Image

Now, that said, I can't store the coordinates of the foreground pixels right now, because something is wrong with the subscript or number of subscripts in the array. Here's my source code so far, I would appreciate any input on what could possibly be the problem.. I am still new to this syntax, and arrays seem slightly different than what I'm used to (AS2.0, c++, etc.)

Anyway here's the source:

#cs ---------------------------------------------------------------
    The Latest Version of Pixel Pirate
-------------------------------------------------------------------
        - Basic OCR, almost
        - Recognizes Pixels at x,y coords that have different
          colors.
        - No error handling yet sorry
        
            The way it works right now, you open MS Paint,
        and leave the document untitled. Draw something, then
        open up this program ( BEFORE MODIFICATION, just
        trust me on this one ). Try using the selection tool
        to just get a color value, and use the brush to
        see the pixel counting in action.
        
            If you are syntax savvy, have a shot at figuring
        out why the returning array doesn't work.
------------------------------------------------------------------
    v0.4.2 by Langford 
        ~ yes... quite a few tries already... ~
#ce --------------------------------------------------------------
; Set Options;

;Opt( "pixelcoordmode", 2 )
Opt( "WinTitleMatchMode", 2)

; Hotkeys;
HotKeySet("{PAUSE}", "_End")

; -----------------------------------------------------------
; Includes;
#include <Array.au3>
#include <File.au3>

; -----------------------------------------------------------
; Vars;
$wName = "untitled - Paint"
$Run = 1

$Statmessage = "Length: 0" & @CRLF & "Width: 0" & @CRLF & "Area: 0"
$Countmessage = "0"
$CEmessage = "0"
$Coordmessage = "Waiting.."
$FGCmessage = "Foreground: 0"
$BGCmessage = "Background: 0"

$mainMessage = ""

$searchArea = ( 8 * 8 ); 64 square pixels
$CurrentElement = 1
$pos = 0

WinWaitActive( $wName )
; -----------------------------------------------------------
; Main Loop;
Do
; Array used later
;Dim $pxArray[$searchArea][2]
    
    $pos = MouseGetPos()
    _UpdateMessage()
    _GetAllPixels( ( $pos[0] - 5 ), ( $pos[1] - 5 ), ( $pos[0] + 5 ), ( $pos[1] + 5 ), 16777215)
    
Until $Run = 0

#cs --------------------------------------------------------
    Main Functions
        _GetAllPixels( $StartX, $StartY, $EndX, $EndY, $Color )
        _UpdateMessage()
        _End()
#ce --------------------------------------------------------
; Pixel Function;

Func _GetAllPixels( $StartX, $StartY, $EndX, $EndY, $Color )
    
; Just some local vars
    Local $aCoord, $i, $x, $y       ; Should be obvious, I am not using Coord yet though.
    Local $Length = $EndX - $StartX ; Length ( x ) of Search Area
    Local $Width = $EndY - $StartY  ; Width ( y ) of Search Area
    Local $mCount = 0               ; Initialize this search's Foreground Pixel Count to 0
    Local $Area = $Length * $Width  ; Calculate the area of the search, for the Coord Array Later
    
; Debug Message ( there are a few more too.. more to help me find a good solution to defining characters later )
    $StatMessage = "Length: " & $Length & @CRLF & "Width: " & $Width & @CRLF & "Area: " & $Area
    
; The Coordinate Array
    Dim $aReturn[$Area][2]
    $CurrentElement = 1             ; Current element inside the array
    
; Loop From Row1(y) -> Column1(x), Column2(x), Column3(x), ... , Row2(y) -> Column1(x)... etc.
    For $y = 1 to $Width
        For $x = 1 to $Length
            
        ; Actual Coordinates
            Local $curX = $x + $startX
            Local $curY = $y + $startY
            
        ; Debug Message 
            $Coordmessage = "X: " & String( $curX ) & " Y: " & String( $curY )
            
            #cs
             Parse the colors per pixel found, separate foreground from $Color
             Note: This script assumes you're looking at one color on one background,
                   and only one 'character' so far. As the purpose this far is
                    only to read a single character of a certain font at a certain
                    position. While still being able to move around the search
                    area.
            #ce
            Local $curColor = PixelGetColor( $curX, $curY )
            $FGCmessage = "Foreground: " & $curColor
            $BGCmessage = "Background: " & $Color
            
            If $curColor = $Color Then
            ; do nothing
            Else
            ; iterate foreground pixel count
                $mCount = $mCount + 1
                
                #cs
                    If these arrays would work, I'd be able to start
                    on character definitions.. below is my problem.
                    Also if I ReDim the array after the loop it still
                    doesn't work... I'm at a loss.
                #ce
                
            ;$aReturn[$CurrentElement][0] = $curX
            ;$aReturn[$CurrentElement][1] = $curY
                
            EndIf
            
        ; Debug Message
            $Countmessage = "Foreground Px: " & $mCount & @CRLF & $FGCmessage & @CRLF & "Background Px: " & $Area - $mCount & @CRLF & $BGCmessage
            $CEmessage = "Current Element: " & $CurrentElement
            
        ; Iterate Element Count
            $CurrentElement = $CurrentElement + 1
            
        Next
    Next
    
; Resize Array? ( doesn't work );
;ReDim $aReturn[ $CurrentElement ][2]
;Sleep( 1 )
    
; I want it to return the whole array;
   ;Return $aReturn
    
EndFunc

; -----------------------------------------------------------
; Message Handler;
Func _UpdateMessage()

    Local $sep = @CRLF & "------------------------------------" & @CRLF 
    $mainMessage = $sep & $Statmessage & $sep & $Countmessage & $sep & $CEmessage & $sep & $Coordmessage
    
    $sToolTipAnswer = ToolTip( $mainMessage , ( $pos[0] + 10 ), ( $pos[1] + 10 ), "Pixel Pirate - Test: [ " & $wName & " ]" )
    
EndFunc 

; -----------------------------------------------------------
; Exit Handler;
Func _End()
   Exit
EndFunc

Thanks,

~ Lang

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...