Rectangle detection

Peter Reid preid at reid-it.co.uk
Mon Sep 7 06:40:47 EDT 2015


With some input from Bernd Niggemann, I've been able to get my rectangle detection and extraction working OK.  Just in case anyone else needs to do something similar, here's my rectangle hunting code:

        -- start looking for top line of params box:
        put empty into tXleft
        put empty into tXright
        put empty into tYtop
        put empty into tYbottom
        put (tImgWth * 2) div 3 into tX
        put tImgWth into tMaxX
        put 0 into tY
        put 0 into tMinY
        put (tImgHght * 3) div 4 into tMaxY
        put tImgWth div 4 into tMinX
        put false into gotTopLine
        set itemDelimiter to comma
        repeat while tY <= tMaxY and not gotTopLine
            put item 1 of getPixelColour(tX,tY,tImgWth) into tRedVal
            if tRedVal = cBlack then
                put true into gotTopLine
            else 
                add 1 to tY
            end if
        end repeat
        -- found top line, search for left & right ends of the line:
        if gotTopLine then
            -- search left for start of top line:
            put tX - 1 into tXchk
            repeat while getPixelColour(tXchk,tY,tImgWth) = cBlack and tXchk >= tMinX
                subtract 1 from tXchk
            end repeat
            if tXchk > tMinX then 
                put tXchk + 1 into tXleft  -- note Xleft
            end if
            -- search right for end of top line:
            put tX + 1 into tXchk
            repeat while getPixelColour(tXchk,tY,tImgWth) = cBlack and tXchk <= tMaxX
                add 1 to tXchk
            end repeat
            if tXchk < tMaxX then
                put tXchk - 1 into tXright  -- note Xright
            end if
        end if
        -- found top line OK, search for right line:
        if gotTopLine and tXleft is not empty and tXright is not empty then
            -- yes, note top Y:
            put tY into tYtop
            -- search down for bottom of right line:
            put tY + 1 into tYchk
            repeat while getPixelColour(tXright,tYchk,tImgWth) = cBlack and tYchk <= tMaxY
                add 1 to tYchk
            end repeat
            if tYchk < tMaxY then
                put tYchk - 1 into tYbottom
            end if
        end if

and here's my getPixelColour function:

function getPixelColour pX, pY, pImgWidth
    local tPix, tImgPosn, tRdVal, tGrVal, tBlVal, tRbyte, tGbyte, tBbyte
    put pY * pImgWidth + pX into tPix
    put tPix * 4 into tImgPosn
    put byte (tImgPosn + 2) of gImage into tRbyte
    if tRbyte is empty then
        put 0 into tRdVal
    else
        put byteToNum(tRbyte) into tRdVal
    end if
    -- greyscale images so ignore green & blue values:
    return (tRdVal)
end getPixelColour

Bernd asked me for timings for the above – I get the following timings for 7 graphs converted 1 after each other: 4 ms, 3 ms, 3 ms, 4 ms, 3 ms and 3 ms for this under LC 6.7.6 on my iMac.

I'm sure others can come up with better/faster/cleverer ways of doing this but what I have works and is fast enough for my purposes.

Thanks to anyone who gave this some thought.

Peter
--
Peter Reid
Loughborough, UK




More information about the use-livecode mailing list