RGBSurface

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?

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.

[quote=429430:@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.[/quote]

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

just because what you SEE looks “black” doesn’t mean the computer thinks so.

Try turning AntiAlias off when you create the dots

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!

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.

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.

Do you really need RGBSurface.Pixel for 3 pixels ?

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

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 was thingking a pixel is a circle, a scan document use square pixels (go understand !)… That what web site says years ago…

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

[quote=430017:@Emile Schwarz]Do you really need RGBSurface.Pixel for 3 pixels ?

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

g.pixel is deprecated

[quote=430018:@Julen Ibarretxe 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[/quote]

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.

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

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

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

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://documentation.xojo.com/index.php/Realbasic.Point

It even has a handy ‘Distance’ function which compares two points