CHROMA KEY - OpenCV


Background Image Mask Video



Source Code
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

/// Global Variables
const int redlowmax = 255;
const int redhighmax = 255;
int redlow,redhigh;
double redl,redh;

const int greenlowmax = 255;
const int greenhighmax = 255;
int greenlow,greenhigh;
double greenl,greenh;

const int bluelowmax = 255;
const int bluehighmax = 255;
int bluelow,bluehigh;
double bluel,blueh;


void on_trackbar( int, void* )
{
red_l = getTrackbarPos("Red Low","Image Result1");
red_h = getTrackbarPos("Red High","Image Result1");

green_l = getTrackbarPos("green Low","Image Result1");
green_h = getTrackbarPos("green High","Image Result1");

blue_l = getTrackbarPos("blue Low","Image Result1");
blue_h = getTrackbarPos("blue High","Image Result1");

}

void chromakey(const Mat under, const Mat over, Mat *dst, const Scalar& color);

int main (){
cv::VideoCapture cap("2.avi");
cv::Mat imgRBG,combined_images;

int key = 0;
Mat background = imread("color3.jpg");
//cvtColor(background, background, CV_BGR2Luv);

/// Create Windows
namedWindow("Image Result1", 1);


createTrackbar( "Red Low", "Image Result1", 0, 255, on_trackbar );
createTrackbar( "Red High", "Image Result1", 0, 255, on_trackbar );

createTrackbar( "green Low", "Image Result1", 0, 255, on_trackbar );
createTrackbar( "green High", "Image Result1", 0, 255, on_trackbar );

createTrackbar( "blue Low", "Image Result1", 0, 255, on_trackbar );
createTrackbar( "blue High", "Image Result1", 0, 255, on_trackbar );

cvSetTrackbarPos("Red Low", "Image Result1",92);
cvSetTrackbarPos("Red High", "Image Result1",242);

cvSetTrackbarPos("green Low", "Image Result1",0);
cvSetTrackbarPos("green High", "Image Result1",255);

cvSetTrackbarPos("blue Low", "Image Result1",0);
cvSetTrackbarPos("blue High", "Image Result1",121);

//createTrackbar( "BLUE", "Image Result2", &red_low, red_low_max, on_trackbar );

while(key!=27){
cap >> imgRBG;

chromakey(background,imgRBG, &combined_images, Scalar(100, 200,185));

imshow("Image Result2", combined_images);
/// Show some stuff
//on_trackbar( alpha_slider, 0 );

key = cv::waitKey(1650);
}

cap.release();
return 0;
}

void chromakey(const Mat under, const Mat over, Mat *dst, const Scalar& color) {

// Create the destination matrix
*dst = Mat(under.rows,under.cols,CV_8UC3);

for(int y=0; y<under.rows; y++) {
for(int x=0; x<under.cols; x++) {

if (over.at<Vec3b>(y,x)[0] >= red_l && over.at<Vec3b>(y,x)[0] <= red_h && over.at<Vec3b>(y,x)[1] >= green_l && over.at<Vec3b>(y,x)[1] <= green_h && over.at<Vec3b>(y,x)[2] >= blue_l && over.at<Vec3b>(y,x)[2] <= blue_h)
{
dst->at<Vec3b>(y,x)[0]= under.at<Vec3b>(y,x)[0];
dst->at<Vec3b>(y,x)[1]= under.at<Vec3b>(y,x)[1];
dst->at<Vec3b>(y,x)[2]= under.at<Vec3b>(y,x)[2];}
else{
dst->at<Vec3b>(y,x)[0]= over.at<Vec3b>(y,x)[0];
dst->at<Vec3b>(y,x)[1]= over.at<Vec3b>(y,x)[1];
dst->at<Vec3b>(y,x)[2]= over.at<Vec3b>(y,x)[2];}

}
}
}


Last edited Oct 23, 2014 at 9:30 AM by mobin_ksa, version 1