hi i'm creating a program with the same effect as the green screen used in the movies and i'm having problem in getting the right value of green colors, but when i uses black or white as a color to be mask it works but not with the green, can anyone give me an advise for this.

thanks

here is my codes

For k = 0 To pic1.ScaleHeight
          clr = GetPixel(pic1.hdc, i, k)
                If clr = vbGreen Then
                SetPixel pic3.hdc, i, k, vbWhite
        Else
               SetPixel pic3.hdc, i, k, vbBlack

Dani AI

Generated

Building on 's example and the pointers from and , the reliable way to chroma-key is not to test for a single exact color but to measure how "green" each pixel is and apply a tolerance. Also be aware that per-pixel GetPixel/SetPixel loops are slow; see the note below on faster approaches.

A simple, robust rule you can tune: extract the R/G/B channels from the COLORREF, require the green channel to be above a minimum, and require green to exceed the red and blue channels by a margin. Example (VB6-style), tune the numeric thresholds to match your footage:

Dim clr As Long, r As Long, g As Long, b As Long

clr = GetPixel(hdcSrc, x, y)

r = clr And &HFF
g = (clr And &HFF00) \ &H100
b = (clr And &HFF0000) \ &H10000

' Example test: green must be reasonably high and clearly above R and B
If g > 120 And (g - r) > 60 And (g - b) > 60 Then
    SetPixel hdcDest, x, y, bgColor
Else
    SetPixel hdcDest, x, y, clr
End If

For better quality, (a) use a squared Euclidean distance to a target green to handle mixed tones, (b) convert to HSV and test hue+sat to handle lighting changes, and (c) create a trimap and do a small blur or alpha ramp to avoid hard jagged edges.

Performance note: per-pixel GetPixel/SetPixel is very slow for large images. Copy the bitmap into a DIBSection or use GetDIBits/CreateDIBSection and process the bitmap bytes directly, then blit the result back. Microsoft docs: COLORREF layout and CreateDIBSection.

Tip: generate a debug mask (white = keyed, black = kept) to tune thresholds quickly, and sample several green areas from your source to choose robust numbers before final processing.

Recommended Answers

All 2 Replies

Hi,

Was that Complete code..? or you forgot one more Outside loop for ScaleWidth...?

Regards
Veena

Hi,
GetPixel() returns the combination of Red, Green and Blue. So the Color you are checking

clr = GetPixel(pic1.hdc, i, k)
If clr = vbGreen Then

This will check only Red is 0, Blue is 0 and Green is 255.
But you may need to separate the Color values.
Also u can use Point(X as Single, y as Single) to get the (x,y) Pixel Value instead of GetPixel().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.