RGBSurface

  1. 3 months ago

    Alexander v

    Mar 20 Europe (Houten, The Netherland...
    Edited 3 months ago

    For a math project I place 21 random points in a picture and after that I scan the surface to connect the dots via a line in a specific fashion. 2018R4 Windows10

    This code runs in de Paint event of the Canvas

    Dim PointRandom As New Random
    Dim CanvasPicture As New Picture(Canvas1.Width,Canvas1.Height,32)
    Dim X,Y,OldX,OldY As Integer
    Dim CanvasSurface As RGBSurface
    Dim PointsFound As Integer = 0
    
    
    CanvasPicture.Graphics.ForeColor = RGB(0,0,0)
    
    For i As Integer = 0 To 20
    Dim PixelPoint As New OvalShape
    PixelPoint.Width = 1
    PixelPoint.Height = 1
    X = PointRandom.InRange(0,Canvas1.Width)
    Y = PointRandom.Inrange(0,Canvas1.Height)
    
    CanvasPicture.Graphics.DrawObject(PixelPoint,X,Y)
    
    Next
    
    CanvasSurface = CanvasPicture.RGBSurface
    
    For J As Integer = 0 To CanvasPicture.Width-1
    For K As Integer = 0 To CanvasPicture.Height-1
    If CanvasSurface.Pixel(J,K) = RGB(0,0,0) Then
    PointsFound = PointsFound+1
    End If
    
    If PointsFound > 1 Then
    CanvasPicture.Graphics.DrawLine(OldX,OldY,J,K)
    End If
    
    OldX = J
    OldY = K
    Next
    Next
    
    Canvas1.Backdrop = CanvasPicture
    
    MsgBox(Str(PointsFound))

    The RGBSurface.Pixel should return 21 pixels, but I got around 970? Looks like it detects more pixels with this RGB(0,0,0) value, but that shouldn't be possible.

    ps the code block doesn't format the code anymore?

  2. Dave S

    Mar 20 San Diego, California USA

    code block never formatted your code.. it took it as-is when you paste it

    as to your code... once PointsFound has a value, it looks like you just connect any dot that follows

    should it be something like this?

    For J As Integer = 0 To CanvasPicture.Width-1
       For K As Integer = 0 To CanvasPicture.Height-1
          If CanvasSurface.Pixel(J,K) = RGB(0,0,0) Then
          PointsFound = PointsFound+1
          If PointsFound > 1 Then
             CanvasPicture.Graphics.DrawLine(OldX,OldY,J,K)
          End If
          OldX = J
          OldY = K
       end if
       Next
    Next

    This of course "assumes" that the ONLY black points are the ones you placed.

  3. Alexander v

    Mar 20 Europe (Houten, The Netherland...
    Edited 3 months ago

    @Dave S code block never formatted your code.. it took it as-is when you paste it

    as to your code... once PointsFound has a value, it looks like you just connect any dot that follows

    should it be something like this?

    For J As Integer = 0 To CanvasPicture.Width-1
       For K As Integer = 0 To CanvasPicture.Height-1
          If CanvasSurface.Pixel(J,K) = RGB(0,0,0) Then
          PointsFound = PointsFound+1
          If PointsFound > 1 Then
             CanvasPicture.Graphics.DrawLine(OldX,OldY,J,K)
          End If
          OldX = J
          OldY = K
       end if
       Next
    Next

    This of course "assumes" that the ONLY black points are the ones you placed.

    You are right - I detected it after I went through the code for the 50th time but your code doesn't work either. It finds no black dots? But they are there - I can see them.

    ps my code is formatted in the editor - strange

    Thanks

  4. Dave S

    Mar 20 San Diego, California USA

    @Alexander vnbsp;der Linden But they are there - I can see them.

    just because what you SEE looks "black" doesn't mean the computer thinks so.

    Try turning AntiAlias off when you create the dots

  5. jim m

    Mar 20 Pre-Release Testers, Xojo Pro piDog.com

    Shouldn't you draw into a separate picture from the one you're reading? I would think once you draw a line, that each pixel of that line will be counted as a hit in your loop.

  6. Alexander v

    Mar 20 Europe (Houten, The Netherland...

    @jim m Shouldn't you draw into a separate picture from the one you're reading? I would think once you draw a line, that each pixel of that line will be counted as a hit in your loop.

    Aahhhh. You’re right! I ended up with a whole black canvas. Think I should draw the lines in different color. Thanks!

  7. jim m

    Mar 20 Pre-Release Testers, Xojo Pro piDog.com

    Better yet, store the points in an array and loop through the array to draw the lines. If you draw the lines in a different color, you may draw over points and miss some when looking for a color.

  8. Alexander v

    Mar 23 Europe (Houten, The Netherland...

    I found out that RGBSurface.Pixel only works (Win10/2018R4) when the points are at least 3 pixels in width. A width < 3 is not detected. Antialias setting doesn't change this. Neither does HiDPI.

  9. Emile S

    Mar 23 Europe (France, Strasbourg)

    Do you really need RGBSurface.Pixel for 3 pixels ?

    Why don’t you use the standard (and low speed) g.Pixel instead ?

  10. Julen I

    Mar 23 Europe
    Edited 3 months ago

    As others have implicitely suggested, it's probable that the pixels you are trying to paint are blending with the surrounding ones. I would think an OvalShape is prone to such blending, because its shape is not compatible with the square nature arrangement of pixels. I would try a RectShape instead.

    Julen

  11. Emile S

    Mar 23 Europe (France, Strasbourg)

    @Julen I Uriguen square

    ?
    I was thingking a pixel is a circle, a scan document use square pixels (go understand !)… That what web site says years ago…

  12. Julen I

    Mar 23 Europe

    Let me rephrase that: I would think an OvalShape is prone to such blending, because its shape is not compatible with the square ARRANGEMENT of pixels

  13. Alexander v

    Mar 23 Europe (Houten, The Netherland...

    @Emile S Do you really need RGBSurface.Pixel for 3 pixels ?

    Why don’t you use the standard (and low speed) g.Pixel instead ?

    g.pixel is deprecated

  14. Alexander v

    Mar 23 Europe (Houten, The Netherland...
    Edited 3 months ago

    @Julen I Uriguen As others have implicitely suggested, it's probable that the pixels you are trying to paint are blending with the surrounding ones. I would think an OvalShape is prone to such blending, because its shape is not compatible with the square nature arrangement of pixels. I would try a RectShape instead.

    Julen

    I tried with RectShape and it gives results with RectShape.Width and RectShape.Height = 2. If I use 1, then RGBSurface.Pixel doesn't find it.

  15. Julen I

    Mar 23 Europe
    Edited 3 months ago

    You can use RGBSurface.Pixel to set the color of a single pixel, if I am not wrong.

  16. Emile S

    Mar 23 Europe (France, Strasbourg)

    @Alexander vnbsp;der Linden g.pixel is deprecated

    Sorry; it was deprecated in a version after the one I use on an everyday basis (I do not knew), sorry.

  17. Alexander v

    Mar 23 Europe (Houten, The Netherland...

    @Julen I Uriguen You can use RGBSurface.Pixel to set the color of a single pixel, if I am not wrong.

    Set it or read it.

  18. Julen I

    Mar 23 Europe

    @Alexander vnbsp;der Linden Set it or read it.

    Yes, what I meant is you can use it for your purpose. Here is an example: https://drive.google.com/file/d/1IsOvLSCLZExq-Iz5avyVFPoaH4hIyqqW/view?usp=sharing

    I created three pictures and drew an ovalshape, a rectsahpe and a single pixel on them. I am showing each picture in a listbox (each cell of the listbox corresponds to one pixel in the picture) and giving the hexadecimal value of the color of the 6,6 pixel in each of the three pictures (that pixel is where I place the ovalshape, the rectshape and the single pixel).

    Julen

  19. Jeff T

    Mar 24 Pre-Release Testers Midlands of England, Europe

    Seems to me that if all you need is 27 random points, you can just create 27 random co-ordinates, create variables of type POINT
    and add them to an array.
    Saves you even looking for them in a picture, you know exactly where they are

    Then you can draw into a picture if you need to.

    https://docs.xojo.com/index.php/Realbasic.Point

    It even has a handy 'Distance' function which compares two points

or Sign Up to reply!