It is currently Sat May 27, 2017 11:40 pm



Welcome
Welcome to rfobasic

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today. **You are not required to provide truthful information to any registration questions. Be whomever you wish to be.!


Post new topic Reply to topic  [ 26 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
 Post subject: Re: Image to Text
Unread postPosted: Mon May 08, 2017 11:41 am 
Offline

Joined: Mon Oct 22, 2012 7:32 am
Posts: 139
I've already looked at some OCR. I figure if you could use a marker and mark the upper left corner with a stencil like "XXX" you'd always know where the upper left corner of each card was.

How this project got started:
I've never played Bingo before. Someone invited me to play at a local casino. At this place you buy several sheets with 8 card layouts on it. Each sheet is a game. When they call the numbers you have to mark the cards with the number on it. The problem is they call the numbers too fast ( for me anyway). By the time you search the cards, you don't have time to see if the cards have a winning pattern. Usually there are 3 or 4 winning patterns.
To get around this, thee casino sells a computer tablet with the sheets loaded. About $45. It's linked into the system so all you do is watch the cards get marked on the screen, and if you win, it tells the system.
So I decided to automate the system. You enter the sheet info and the win patterns and then you enter the called number and it'd all be done automatically.

I always knew entering the sheet info would be a long process - about 15 minutes per sheet. I knew eventually I'd want to scan the sheets somehow. I did enter some sheets just to see if the system works. And it does work great. It updates the cards and checks for wins and lets you know if you win. But it's not usable until I automate the sheet input.


Report this post
Top
 Profile  
 
 Post subject: Re: Image to Text
Unread postPosted: Mon May 08, 2017 1:07 pm 
Offline

Joined: Sat Dec 22, 2012 2:32 pm
Posts: 830
humpty wrote:
>Doing it from the scratch in BASIC! sounds like an interesting challenge..
I was hoping to stir up enough interest for someone to have a go!
...
...

hmm, interesting, finding the corners doesn't worry me too much.
For me it's the question how to recognizing of the number itself, after having it successfully, located/seperated/cropped.

However, here what i have so far (below), based upon a screenshot from above link...so perfect (rectangular) alignment (...spaghtti-code...q&d)


Remarks/main steps:

- 1st is to start a "radial search" from upper left corner of bitmap, i.e. running along "quater-circles" with increasing radius until finding the 1st non-white pix (see green at upper-left)

- 2nd: searching next two horizontal lines downwards
--> defining this as reference point of the 5×5 grid

- 3rd: searching next 5 horiz. and then 5 vert. lines
--> having a average gridsize in x,y-dir now
--> assuming this precise is enough for navigating through the rest of the grid

- 4th creating a "weight-function": simply the sum of a vertical cut through a single cropped number field, from (almost) top to (almost) bottom at different left-right-positions
--> purpose: detect if single- or double- number-field; when high weight close to the center of cropped field then cutting through a number, if low weight close to center then cutting through a almost blank region -> double number


toDo:

- find the left-right-center of each single number

- make a top-bottom-cut similar to weight-func. at center, but store the single values of the cut into a vector (not summing up)
--> this -i hope- is a characteristic function ("fingerprint") for each single number( 0..9)

- make cross-correlation between the actual searched function and 10 reference functions (created/stored before)
--> the highest integral value should indicated a hit


...that's the idea ...remarks/ideas/hints are very welcome !


brochi



Code:
!!

GR.OPEN     255,0,0,0,,1
GR.COLOR    255,0,255,0,0

bmp$      = "bingo_1.png"

GR.BITMAP.LOAD bmp1, bmp$

offx      = 10
offy      = 10

GR.BITMAP.DRAW nn,bmp1,offx,offy


! Find initial corner -------------------
drad = 5
DO
rad += drad
FOR phi=2 TO 88 STEP 2
  phir = TORADIANS(phi)
  px = rad*COS(phir)
  py = rad*SIN(phir)
  GR.GET.BMPIXEL bmp1, px,py,a,r,g,b
  GR.POINT  nn,px+offx,py+offy
  IF r<5 THEN : GR.RENDER : ex=1
  IF ex THEN F_N.BREAK
NEXT
IF ex THEN D_U.BREAK
UNTIL rad >=100

py = ROUND(py)
px = ROUND(px)


!Seach to next two horizontal lines towards "south" ------
FOR i=1 TO 2
py +=10
DO
  GR.GET.BMPIXEL bmp1, px+4,py++,a,r,g,b
UNTIL r<200
NEXT

!Fine-adust left border -----------------
!...just from the hip -> has to be refined !! -------
px +=-2

! now we have the reference-point:lower-left of first number-field
! mark it with two circles ---------
GR.COLOR 255,255,0,0,0
GR.CIRCLE nn, px+offx,py+offy,5
GR.CIRCLE nn, px+offx,py+offy,8
GR.RENDER
!------------------------------------------

!------------------------------------------

!Find next  lines to south --------------
LIST.CREATE   n,lpy
LIST.ADD      lpy, py
FOR i=1 TO 4
pyold= py
py +=10
DO
  GR.GET.BMPIXEL bmp1, px+4,py++,a,r,g,b
UNTIL r<200
LIST.ADD lpy, py
dy += py-pyold
GR.CIRCLE nn, px+offx,py+offy,4
NEXT
! dy is the average y-grid-size --
dy = ROUND(dy/4)
PRINT dy
!------------------------------------------

!Find next  lines to east --------------
LIST.CREATE n,lpx
LIST.ADD lpx, px
FOR i=1 TO 4
pxold= px
px +=10
DO
  GR.GET.BMPIXEL bmp1, px++,py-4,a,r,g,b
UNTIL r<200
LIST.ADD lpx, px
dx += px-pxold
GR.CIRCLE nn, px+offx,py+offy,4
NEXT
! dx is the average x-grid-size --
dx = ROUND(dx/4)
PRINT dx
!------------------------------------------

LIST.TOARRAY lpx,lpx[]
LIST.TOARRAY lpy,lpy[]


ox = offx*15
oy = offy*90


FOR xx=1 TO 5
FOR yy=1 TO 5
  PRINT : PRINT "------  ",xx,yy
  GR.BITMAP.CROP     bmp2,bmp1, lpx[xx], lpy[yy]-dy , dx,dy
  GR.BITMAP.DRAW     nn,bmp2, ox,oy
  GR.RENDER
  tic = CLOCK()
  minWeight = 999999
  FOR relx = 0.44 TO 0.56 STEP 0.02
   GOSUB weight
   IF weight< minWeight THEN minWeight= weight
  NEXT
  toc = CLOCK()
  IF minWeight >200 THEN outp$="one number" ELSE outp$="two numbers"
  PRINT    minWeight, outp$; "  time:";INT$(toc-tic)
NEXT
NEXT


GR.RENDER
DO:UNTIL forever




weight:
rxdx = relx *dx
!GR.LINE  nn , ox + rxdx, oy, ox+ rxdx, oy+dy
weight =0
FOR i=5 TO dy-5 STEP 2
GR.GET.BMPIXEL bmp2, rxdx ,i,  a,r,g,b
weight += 255-r
NEXT
PRINT relx, weight
RETURN



Attachments:
bingo1.zip [491.98 KiB]
Downloaded 3 times
Report this post
Top
 Profile  
 
 Post subject: Re: Image to Text
Unread postPosted: Mon May 08, 2017 1:23 pm 
Offline
User avatar

Joined: Tue Mar 05, 2013 5:19 am
Posts: 592
Location: Netherlands
Hi,

Brochi is very right with his remarks.
It might even be necessary to do more image enhancement, like (color)contrast normalisation, scratch/noise removal etc.
All very, very slow in RFO-Basic.
Just a thought.... you might have a look at this option:
Run HTML from your Basic app, then use java to have access to image processing/analysis libraries and hopefully an OCR library (like maybe Tesseract, a C++ java callable lib).
Then send the results from HTML/java back to your app and continue in RFO-Basic.
I know it doesn't sound easy, but well it isn't....... :D
If this is doable it might be acceptably fast.....

Good luck,
Aat

_________________
To view all my Apps in the BASIC! repository: http://laughton.com/basic/programs/Author_Index/Aat.pdf

When Sod's Law equals Murphy's Law you are bound to be on the right track....!


Last edited by Aat on Mon May 08, 2017 1:27 pm, edited 1 time in total.

Report this post
Top
 Profile  
 
 Post subject: Re: Image to Text
Unread postPosted: Mon May 08, 2017 1:25 pm 
Offline

Joined: Sat Dec 22, 2012 2:32 pm
Posts: 830
@kokenge
i would hope get about 0.5 sec per single number-box. So 5x5x6=150 boxes per sheet would give about 75 sec per sheet. But exclusive the center box, this will be remaining for manual input in first step! If interesred, you could provide a representative scan...

brochi


Last edited by brochi on Mon May 08, 2017 9:17 pm, edited 1 time in total.

Report this post
Top
 Profile  
 
 Post subject: Re: Image to Text
Unread postPosted: Mon May 08, 2017 1:34 pm 
Offline

Joined: Sat Dec 22, 2012 2:32 pm
Posts: 830
@Aat
I understand your alternative path for solving the problem...but for me it's more interesting to focus on "from-scratch-solution" (...just for fun...)

brochi


Report this post
Top
 Profile  
 
 Post subject: Re: Image to Text
Unread postPosted: Mon May 08, 2017 1:36 pm 
Offline
User avatar

Joined: Tue Mar 05, 2013 5:19 am
Posts: 592
Location: Netherlands
Hi Brochi,

I see you have seriously stepped in!
Great, if anyone can give useful code it's you!
Perhaps some more questions to Kokenge:
Are the cards always the same w.r.t. colors, font, size, aspect ratio, decorations, lay-out, angle of view.....

Aat

_________________
To view all my Apps in the BASIC! repository: http://laughton.com/basic/programs/Author_Index/Aat.pdf

When Sod's Law equals Murphy's Law you are bound to be on the right track....!


Report this post
Top
 Profile  
 
 Post subject: Re: Image to Text
Unread postPosted: Tue May 09, 2017 5:11 am 
Offline

Joined: Mon Oct 22, 2012 7:32 am
Posts: 139
Quote:
Are the cards always the same w.r.t. colors, font, size, aspect ratio, decorations, lay-out, angle of view....


I was trying to make the system general purpose. So no - the cards are not all the same. I played at 2 places. One was 4 across and 2 down (8 cards). Another was like the link above with 2 across and 3 down (6 cards). I wrote a couple of programs trying to do this. I found if you ignore everything like lines and alphabetic and squares and simply tried to get the numbers only it would work. For example if you had a card with 2 across and 3 down, you would get the numbers only as:
Code:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
...

We would know what Card [1-6], column [BINGO], and row [1-5] was by entering two parameters; across=2 and down=3
Code:
a database with card, col, row, val would hold the values;
card = 1 col = b row = 1 val = 1
card = 1 col = i row = 1 val = 2
card = 1 col = n row = 1 val = 3
card = 1 col = g row = 1 val = 4
card = 1 col = o row = 1 val = 5
card = 2 col = b row = 1 val = 6
card = 2 col = i row = 1 val = 7
...


Anyway - that was my best guess at how to do this.

Thanks for the help..
Dan


Report this post
Top
 Profile  
 
 Post subject: Re: Image to Text
Unread postPosted: Tue May 09, 2017 11:11 am 
Offline

Joined: Sat Dec 22, 2012 2:32 pm
Posts: 830
......on my side it doesn't work as expected. I was much to optimistic.
I think it will become to slow when doing it from scratch on basic side. I think you need a solution as Aat mentioned.

regards, brochi


Report this post
Top
 Profile  
 
 Post subject: Re: Image to Text
Unread postPosted: Tue May 09, 2017 6:53 pm 
Offline
User avatar

Joined: Sat Mar 26, 2016 9:23 am
Posts: 94
Aat wrote:
Hi Brochi,
I see you have seriously stepped in!
Aat


I think of him as Archimedes 2.0.

Guy knows his was around a circle.


Report this post
Top
 Profile  
 
 Post subject: Re: Image to Text
Unread postPosted: Wed May 10, 2017 9:57 am 
Offline
User avatar

Joined: Thu Nov 21, 2013 7:00 pm
Posts: 638
Perhaps we should start off more simply.

Assuming we have cropped out one card to a bitmap.
Cards are always 5x5 square (right?)
This means its easy to divide and crop out the individual numbers (all 25 of them with the
center one ignored).

So we have 24 bitmaps (indiviual numbers of one card)
Decide the threshold of if a colour is a background color or ink color.

There's two ways of I can think of recognising a number;
A) Do some clever A.I testing of pixels with a lot of IF..THEN statements.
or
B) Compare each bitmap with a set of pre-defined bitmaps which were prepared with a similar font (and scale to the same size).
To speed up the process, don't test every pixel. Just randomly pick a subset. If the number of
hits are high enough, then it's probably the same number.

edit/ I reckon if you scale down the bitmaps to a small size, the processing will be a lot quicker.

_________________
- Failure is not an option. It is bundled with the software. -


Report this post
Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 26 posts ]  Go to page Previous  1, 2, 3  Next


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
suspicion-preferred