Configuring Chroma Key with code
Main object to configure Chroma Key is MChromaKey. It operates all the keys that you use for your image.
MChromaKey myChromaKey = new MChromaKey();
Each key is managed by an MKey object. You can get the key settings from the MChromaKey object with KeyGet method:
MKey myKey; myChromaKey.KeyGet(null, out myKey, "");
Input parameters for this method are recommended to keep like in the example above.
And then you can apply your changes with KeySet method:
All the internal operations are made with the MKey object.
Add area for keying
You can detect required area automatically with a KeyDetect method:
object keyFrame; string configuration = "type='RGB' max_keys='1'"; m_pKey.KeyDetect(configuration , out keyFrame);
where configuration contains required properties for detection. type is a colorspace to work with (possible values are 'RGB', 'HLS', 'YUV' and 'auto'), max_keys is a maximal number of cycles for detection.
To add a point from image to the settings you should use KeyAddPoint method:
object resultFrame; int targetX = 50; int targetY = 30; string configuration = "type='RGB' max_keys='1'"; myKey.KeyAddPoint(targetX, targetY, configuration, out resultFrame);
You can use KeyExcludePoint to exclude a point from settings:
object resultFrame; int targetX = 36; int targetY = 18; string configuration = "type='RGB' max_keys='1'"; myKey.KeyExcludePoint(targetX, targetY, configuration, out resultFrame);
You can also add a rectangle to keying area with the KeyAddRect method:
object resultFrame; int targeLeft = 50; int targetTop = 30; int targetRight = 120; int targetBotom = 70; string configuration = "type='RGB' max_keys='1'"; myKey.KeyAddRect(targeLeft, targetTop, targetRight, targetBotom, configuration, out resultFrame);
You can get and set adjust settings with KeyAdjustGet and KeyAdjustSet methods. In both these methods you should set required type of setting as the first parameter, for example, to get minimal transparency you should call:
eCK_Adjust settingType = eCK_Adjust.eCKA_MinTransparent; double settingValue; myKey.KeyAdjustGet(settingType, out settingValue);
And to set base power you should call:
eCK_Adjust settingType = eCK_Adjust.eCKA_Power; double settingValue = 0.7; object resultFrame; myKey.KeyAdjustSet(settingType, settingValue, out resultFrame);
All the settings are listed in the eCK_Adjust enumeration.
Undo and Redo operations
You can cancel previous settings with the KeyStepBack method:
int numberOfBackSteps = 2; object resultFrame; myKey.KeyStepBack(numberOfBackSteps, out resultFrame);
And redo the actions with the KeyStepFwd method:
int numberOfFwdSteps = 1; object resultFrame; myKey.KeyStepFwd(numberOfFwdSteps, out resultFrame);
Note please that if there are no steps in a buffer to redo then an exception is raised, so please use a try-catch block for these methods.
Get a result frame
You can get a result frame object with KeyFrameGet method. This method returns you a result frame after chroma key operation and a frame that is used for background:
object resultFrame; object backgroundFrame; myKey.KeyFrameGet(out resultFrame, out backgroundFrame);
Once grabbed, you can work with a frame like with any frame object.