# Correct chromakey (some code inside)



## anon347 (Oct 15, 2013)

Hi! I want to overlay image on video the use green chromakey. For example, there's two pictures that I want to combine together:
 - example of video
and
 - example of my overlay

At the moment, I can choose green color in the settings and get something like this:
 - 10% similarity


----------



## anon347 (Oct 15, 2013)

I can increase level of similarity to 100%, but I steel get some green channel:


I can add blen level to 100, but it don't help to remove all green pixels:


For example, you can see here After Effects filter "key light", I expect to see more
 - no green ever!


----------



## anon347 (Oct 15, 2013)

*GIMP*

For example, it GIMP "color to alpha" with green color:
 - good, but some purple colors on gradients, compare with:
 - it's more expected, but the previous version is acceptable

You may be able to take the algorithm of GIMP and use it in your program?


----------



## anon347 (Oct 15, 2013)

*My version*


My algo, here steel some dirty-yellow colors, but black and white gradients are perfect

My code here. At first, I use ImageMagick to load images:


```
int width;
  int height;
  int bytes;
  unsigned char *back;
  unsigned char *mask;

  MagickWandGenesis ();
  MagickWand *wand = NewMagickWand ();

  // load background

  MagickReadImage (wand, "back.png");
  width = MagickGetImageWidth (wand);
  height = MagickGetImageHeight (wand);
  bytes = width * height * 3;

  back = malloc (bytes);
  MagickExportImagePixels (wand, 0, 0, width, height, "RGB", CharPixel, back);

  // load overlay

  MagickWand *wand2 = NewMagickWand ();
  MagickReadImage (wand2, "text.png");
  mask = malloc (bytes);
  MagickExportImagePixels (wand2, 0, 0, width, height, "RGB", CharPixel, mask);
```

and then process image
The code is not optimal, but it works and gives to understand how the algorithm works


```
for (q = 0; q < bytes; q += 3){
      r = mask[q];
      g = mask[q + 1];
      b = mask[q + 2];

      if (g == 255 && r == 0 && b == 0)
        {
          alpha = 0;
          goto redraw;
        }
      if (g <= r || g <= b)
        {
          alpha = 1;
          goto redraw;
        }

      lev = g - r > g - b ? g - b : g - r;
      g -= lev;
      alpha = lev / 255.0;

    redraw:

      if (alpha == 1)
        {
// nothing
        }
      else if (alpha == 0)
        {
          r = back[q];
          g = back[q + 1];
          b = back[q + 2];
        }
      else
        {
          scale = 1 - alpha;
          r = (int) ((double) r + (double) back[q] * alpha);
          g = (int) ((double) g + (double) back[q + 1] * alpha);
          b = (int) ((double) b + (double) back[q + 2] * alpha);
        }

      back[q] = r;
      back[q + 1] = g;
      back[q + 2] = b;
    }
```

and finally, save image:


```
MagickImportImagePixels (wand, 0, 0, width, height, "RGB", CharPixel, back);
  MagickWriteImage (wand, "result.png");
```

Please, add this chromakey to OBS. Or gimp version.

There are several versions of images, simply attach to the case, if they are interested in


----------



## Muf (Oct 15, 2013)

Image Source uses *Color* Key, not *Chroma* Key. It's a different algorithm, made for different purposes. For what you want, just use a PNG file with embedded alpha channel; no keying needed. If you want to see OBS' chroma key algorithm at work, try adding a Video Capture Device and enable the Chroma Key feature there.


----------



## anon347 (Oct 15, 2013)

Muf said:
			
		

> For what you want, just use a PNG


No, I do not want a static image, I want another video on top of it (realtime)



			
				Muf said:
			
		

> try adding a Video Capture Device and enable the Chroma Key feature there.


Surprised by the presence of such a function in "Video Capture Device", but its absence in "Monitor Capture/Window Capture". In games capture mode, none at all keying is provided at all.

I want to capture a window/monitor with some realtime graphics, and its contents are to composed over the video.
I don't have any devices for "video capture device", and I think the emulation webcam (screen capture video to virtual webcam) through another program - not the best solution


----------



## umer147 (Oct 23, 2013)

Surprised by the presence of such a function in "Video Capture Device", but its absence in "Monitor Capture/Window Capture". In games capture mode, none at all keying is provided at all.


----------



## anon347 (Nov 4, 2013)

Any suggestions? Or fork is best way?


----------



## dodgepong (Nov 4, 2013)

The standard way to get a feature added to an open source project is to fork the project, add the code, then submit a pull request.


----------

