November 21, 2006

Points In Circle

I need to get all valid coloured pixels within a given radius of an origin point. The follow code creates a Form with an image. When clicking the image, all black pixels within a 10 pixel radius of the clicked pixel are coloured red.
Special care is taken not to go off the edge of the image (note the Max and Min function calls).

using System
using System.Drawing
using System.Math
using System.Windows.Forms

def GetColoredPointsInCircle(bmp : Bitmap, origin : Point, radius : int, color : Color) : list[Point]
    def xLine = $[Max(0, origin.X - radius) .. Min(bmp.Width - 1, origin.X + radius)]
    def yLine = $[Max(0, origin.Y - radius) .. Min(bmp.Height - 1, origin.Y + radius)]
    def r2 = radius * radius
    def InCircle(x, y)
        def dX = x - origin.X
        def dY = y - origin.Y
        (dX * dX + dY * dY) < r2

    def CorrectColorAt(x, y)
        bmp.GetPixel(x, y).ToArgb() == color.ToArgb()

    $[ Point(x, y) | x in xLine, y in yLine, InCircle(x,y) && CorrectColorAt(x,y)]

def f = Form()
def bmp = Bitmap("line.png")

f.Paint += fun(_, e)
    e.Graphics.DrawImage(bmp, 0,0)

f.MouseUp += fun(_, e)
    def pts = GetColoredPointsInCircle(bmp, Point(e.X, e.Y), 10, Color.Black)
    using (g = f.CreateGraphics())
        foreach (pt in pts)
            g.FillRectangle(Brushes.Red, pt.X, pt.Y, 1, 1)


This allows me to visually check that the right pixels would be used when performing a linear regression to approximate the line. The size of the radius, 10 pixels, is arbitrary at the moment. It reflects the width of the line drawn in the image.
Also the choice of using ”<” and not ”<=” to check if a point is in the circle is arbitrary for now.

Check out the awesome list comprehensions Nemerle lets me do!

- No comments Not publicly viewable

Add a comment

You are not allowed to comment on this entry as it has restricted commenting permissions.

Google Ads

Search this blog

Most recent comments

  • I scratched my eye while i was holding some kind of plastic packaging.. Anyways the corner of the pl… by Ercan on this entry
  • About a year ago my contacts that i was wearing, i guess were fautly, because shortly after they wer… by Jon on this entry
  • I got shower gel in my eye 4 and a half days ago, and becasue i rubbed my eyes a lot, i have scratch… by Chris on this entry
  • This website may help–health/tc/Eye–Injuries–Home–Treatment by S on this entry
  • I somehow got dirt, or debris in my eye. The terrible pain sent me in a tailspin. I was afraid of sa… by Bobbi on this entry


November 2006

Mo Tu We Th Fr Sa Su
Oct |  Today  | Dec
      1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30         


Blog archive

Not signed in
Sign in

Powered by BlogBuilder