# Rectangle detection

Peter Reid preid at reid-it.co.uk
Sat Sep 5 03:42:45 EDT 2015

```Thanks for the initial thoughts on this.  Here's some more clarification:

1. The rectangles are not the same size or location, but they generally sit substantially in the upper right quadrant of the graph which is otherwise empty/white.

2. The graphs are all the same dimensions, but the actual size depends on the pixel density chosen by the user.  The user can select 1x, 2x or 4x screen resolution, giving images that are either 1916x1061, 3832x2122 or 7664x4244 in size.

3. I'm planning to use the freely available tesseract OCR engine.  This seems to work pretty well as long as the pixel density is 200dpi or higher, which means using the 2x or 4x screen resolution options,  Here are a couple of links about Tesseract:

https://en.wikipedia.org/wiki/Tesseract_(software)
https://github.com/tesseract-ocr

4. I've been trying to use ideas from the image processing tutorials but my code is painfully slow.  My method is as follows (assuming the rectangle is in the upper right quadrant):

a. start along the top row of the graph at about 2/3 (or 3/4) along the row (about the middle of the top right quadrant)

b. drop down vertically until I hit a black pixel

c. follow the black pixels to the left & right to find the ends of the top line of the rectangle - this gives me the Xleft,Ytop and Xright,Ytop coordinates of the rectangle

d. from the left end of the top line of the rectangle, drop down the black pixels until I hit a white pixel marking the bottom of the left line of the rectangle - this gives me the coordinates Xleft,Ybottom from which I also get Xright,Ybottom

However, the X coordinates are correctly identified but not the Y coordinates. Ytop is OK but the drop down fails to stop before going well past the lower line of the rectangle.   My script for the searching down the left edge of the rectangle is like the following:

-- search down for bottom of left line where tXleft,tY is the identified left pixel of the top line:
put tY + 1 into tYchk
repeat while getPixelColour(tXleft,tYchk,tImageWidth) = cBlack and tYchk <= tMaxY
end repeat
if tYchk < tMaxY then
put tYchk - 1 into tYbottom
end if

5. Here is the handler that I use to test the colour of a pixel:

function getPixelColour pX, pY, pImgWidth
local tPix, tImgPos, tRedVal, tGreenVal, tBlueVal
put pY * pImgWidth + pX into tPix
put tPix * 4 into tImgPos
put charToNum(byte (tImgPos + 2) of image "graph" of stack "ImageHolder") into tRedVal
put charToNum(byte (tImgPosn + 3) of image "graph" of stack "ImageHolder") into tGreenVal
put charToNum(byte (tImgPosn + 4) of image "graph" of stack "ImageHolder") into tBlueVal
return (tRedVal)  -- & comma & tGreenVal & comma & tBlueVal)
end getPixelColour

6. I've even tried changing the pixels to red as I check them so I can see where I've been but there's no sign of a red trail in the images!  This is my handler for setting pixel colours:

on setPixelColour  pX, pY, pImgWidth, pRGB
local tPix, tImgPos, tRval, tGval, tBval
put pY * pImgWidth + pX into tPix
put tPix * 4 into tImgPos
set itemdelimiter to comma
put item 1 of pRGB into tRval
put item 2 of pRGB into tGval
put item 3 of pRGB into tBval
put numToChar(tRval) into byte (tImgPos + 2) of image "graph" of stack "ImageHolder"
put numToChar(tGval) into byte (tImgPos + 3) of image "graph" of stack "ImageHolder"
put numToChar(tBval) into byte (tImgPos + 4) of image "graph" of stack "ImageHolder"
end setPixelColour

Thanks for any ideas!

Peter
--
Peter Reid
Loughborough, UK

```