Android Question Fast location pixel position X Y from a bitmap with only 1 pixel.

scsjc

Well-Known Member
Licensed User
Longtime User
What would be the fastest way to locate the position of a single pixel that has a bitmap image.
That bitmap image does not have any other pixels,is empty.
I've tried looping X and Y through all the pixels using bitmap.GetPixel and it's extremely slow.
 

emexes

Expert
Licensed User
yes exactly

Well, you're going to have to massage this into shape because I don't have B4X handy at moment, but:

give this a burl:
Dim source() As Float = Array As Float(4.0, 4.0, 29.0, 16.0, 27.0, 37.0, 5.0, 20.0)
Dim destination() As Float = Array As Float(0, 0, anchopantalla, 0, anchopantalla, altopantalla, 0, altopantalla)

For X = 0 To anchopantalla
    For Y = 0 to altopantalla
        Dim ToXY() As Float = Array As Float(X, Y)
        Dim FromXY() As Float = WhereDidItComeFrom(source, destination, ToXY)
        Log(ToXY(0) & Tab & ToXY(1) & Tab & FromXY(0) & Tab & FromXY(1))
    Next 'X
Next 'Y
 
Sub WhereDidItComeFrom(src() As Float, dst() As Float, dstxy() As Float) As Float()

    'dst must be rectangle with top-left = (0, 0)
    Dim dstwidth As Float = dst(4)
    Dim dstheight As Float = dst(5)
 
    Dim dstx As Float = dstxy(0)
    Dim dsty As Float = dstxy(1)
 
    Dim HowMuchRight As Float = dstx / dstwidth
    Dim HowMuchLeft As Float = 1 - HowMuchRight
    Dim HowMuchBottom As Float = dsty / dstheight
    Dim HowMuchTop As Float = 1 - HowMuchBottom
 
    'if we are lucky then these should sum to 1.0
    Dim HowMuchTopLeft As Float = HowMuchTop * HowMuchLeft
    Dim HowMuchTopRight As Float = HowMuchTop * HowMuchRight
    Dim HowMuchBottomLeft As Float = HowMuchBottom * HowMuchLeft
    Dim HowMuchBottomRight As Float = HowMuchBottom * HowMuchRight
 
    Dim srcx As Float = src(0) * HowMuchTopLeft _
                      + src(2) * HowMuchTopRight _
                      + src(4) * HowMuchBottomRight _
                      + src(6) * HowMuchBottomLeft
 
    Dim srcy As Float = src(1) * HowMuchTopLeft _
                      + src(3) * HowMuchTopRight _
                      + src(5) * HowMuchBottomRight _
                      + src(7) * HowMuchBottomLeft
               
    Dim srcxy() As Float = Array As Float(srcx, srcy)
 
    Return srcxy()
 
End Sub
 
Last edited:
Upvote 0

scsjc

Well-Known Member
Licensed User
Longtime User
Well, you're going to have to massage this into shape because I don't have B4X handy at moment, but:

give this a burl:
Dim source() As Float = Array As Float(4.0, 4.0, 29.0, 16.0, 27.0, 37.0, 5.0, 20.0)
Dim destination() As Float = Array As Float(0, 0, anchopantalla, 0, anchopantalla, altopantalla, 0, altopantalla)

For X = 0 To anchopantalla
    For Y = 0 to altopantalla
        Dim ToXY() As Float = Array As Float(X, Y)
        Dim FromXY() As Float = WhereDidItComeFrom(source, destination, ToXY)
        Log(ToXY(0) & Tab & ToXY(1) & Tab & FromXY(0) & Tab & FromXY(1))
    Next 'X
Next 'Y
 
Sub WhereDidItComeFrom(src() As Float, dst() As Float, dstxy() As Float) As Float()

    'dst must be rectangle with top-left = (0, 0)
    Dim dstwidth As Float = dst(4)
    Dim dstheight As Float = dst(5)
 
    Dim dstx As Float = dstxy(0)
    Dim dsty As Float = dstxy(1)
 
    Dim HowMuchRight As Float = dstx / dstwidth
    Dim HowMuchLeft As Float = 1 - HowMuchRight
    Dim HowMuchBottom As Float = dsty / dstheight
    Dim HowMuchTop As Float = 1 - HowMuchBottom
 
    'if we are lucky then these should sum to 1.0
    Dim HowMuchTopLeft As Float = HowMuchTop * HowMuchLeft
    Dim HowMuchTopRight As Float = HowMuchTop * HowMuchRight
    Dim HowMuchBottomLeft As Float = HowMuchBottom * HowMuchLeft
    Dim HowMuchBottomRight As Float = HowMuchBottom * HowMuchRight
 
    Dim srcx As Float = src(0) * HowMuchTopLeft _
                      + src(2) * HowMuchTopRight _
                      + src(4) * HowMuchBottomRight _
                      + src(6) * HowMuchBottomLeft
 
    Dim srcy As Float = src(1) * HowMuchTopLeft _
                      + src(3) * HowMuchTopRight _
                      + src(5) * HowMuchBottomRight _
                      + src(7) * HowMuchBottomLeft
              
    Dim srcxy() As Float = Array As Float(srcx, srcy)
 
    Return srcxy()
 
End Sub
THANKSSSSSSSSSSS !!!!!!!!!!!! :)
I've been testing and it seems to work correctly.
 

Attachments

  • trasformacionb4proyeccion.zip
    10.8 KB · Views: 118
Upvote 0
Top