Area calculation from image

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
How can I calculate the green area in the attachment? Or simply the number of green pixels it contains would be enough to get me there.
 

Attachments

Are we talking pure green (i.e. 0,255,0) or shades of green? If we're also looking at shades, then what's your tolerance? What about the areas of white, if it's the interior of the building you're looking at?...
 
Are we talking pure green (i.e. 0,255,0) or shades of green? If we're also looking at shades, then what's your tolerance? What about the areas of white, if it's the interior of the building you're looking at?...

The image has been produced by scanning a diagram, maximising the contrast, and then using the fill function in MS Paint to fill in all the internal areas in green. So I'm simply interested in the area that MS Paint has filled in. So the answer to your question is simply "whatever green MS Paint has provided."

I've found that it doesn't take my computer more than a minute to check each pixel in the diagram, and it's easy to code, so that's what I've now done in fact.
 
How are you examining the pixels?

Like this

Code:
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        With Me.PictureBox1
            Dim b As New Bitmap(.Image.Width, .Image.Height)
            b = .Image
            Dim s As Integer
            For n As Integer = 1 To .Image.Width
                For m As Integer = 1 To .Image.Height
                    Dim q As Color = b.GetPixel(n - 1, m - 1)
                    If q.G = 0 And q.R > 250 Then s = s + 1 'plan had red infill. Check for no green as well so white is excluded.
                Next
            Next
            MsgBox(s)
        End With

    End Sub
 
You could make this much, much faster by examining the raw image data rather than using the GetPixel method. I'll see if I can find the tutorial I wrote on the topic.
 
Back
Top