Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test for range in RGB value

Tags:

vb.net

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?

like image 733
Wilko84 Avatar asked Jan 31 '26 10:01

Wilko84


1 Answers

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.

like image 173
aybe Avatar answered Feb 03 '26 08:02

aybe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!