Wilko84
Wilko84

Reputation: 131

Test for range in RGB value

so what I'm doing is creating a simple green screen image maker in vb.net. My code is quite simple (albeit probably not the most efficient)

Dim overlay As Bitmap = New Bitmap(My.Resources.GreenScreen)
    Dim backImage As Bitmap = New Bitmap(My.Resources.Tunnel)

    Dim x As Integer
    Dim y As Integer

    For x = 0 To overlay.Width - 1
        For y = 0 To overlay.Height - 1
            Dim pixelColor As Color = overlay.GetPixel(x, y)
            If pixelColor = Color.FromArgb(255, 0, 254, 0) Then
                overlay.MakeTransparent(overlay.GetPixel(x, y))
            End If
        Next
    Next

    Dim g As Graphics = Graphics.FromImage(backImage)
    g.DrawImage(overlay, 0, 0)
    PictureBox1.Image = backImage

but for now it works, and I'm happy with it (sort of)

What I am facing is that I've downloaded a sample image of someone stood in front of a green screen. The above code removes the majority of the green, but not all. I need to feather the edge of the subject (I think that's what it's called anyway).

What I was thinking was that I need to catch all the different shades of green in the image, is there a way I can specify a max and min range for the RGB?

Upvotes: 3

Views: 180

Answers (1)

aybe
aybe

Reputation: 16662

This is a non-trivial problem to get consistent results across any input sample, however, if you approach gives satisfying results to you, maybe that's all you will need.

You should compute the distance to a particular color:

enter image description here

https://en.wikipedia.org/wiki/Color_difference

So basically, you pick green color, decide of an acceptable distance, then check how close a pixel color is and transform it or not.

Also, for performance, consider using Bitmap.LockBits, read all pixels at once instead of GetPixel which is very slow by nature.

EDIT

Consider using the approach @djv has commented, it will be slightly more complex to setup but the results will be much more consistent.

Upvotes: 1

Related Questions