Contents
  1. I. Color-Space Conversion
  2. II. Extracting the Green-Screen Region
  3. III. Replacing the Background

Replacing the background of a green-screen image involves ① color-space conversion, ② extracting the green-screen region, ③ inverting the green-screen region, and ④ copying the image. The various functions and code encountered along the way are also covered in detail in this article.

I. Color-Space Conversion

  To remove the green screen from a green-screen image, you first need to convert the image to the HSV color space, which makes subsequent image processing easier. You can perform color-space conversion with the cvtcolor() function.

void cv::cvtColor(
	InputArray 	src,		//输入图像矩阵
	OutputArray dst,		//输出图像矩阵
	int 		code,		//转换方式
	int 		dstCn = 0	//目标图像通道数
)	

A typical usage is shown below—an example program that converts an image to the HSV color space:

void MyDemo::inRange_Demo(Mat& image) {
	Mat hsv;	//存储转换后的hsv图像
	cvtColor(image, hsv, COLOR_BGR2HSV);//进行色彩空间转换
	imshow("hsv_image", hsv);
}

II. Extracting the Green-Screen Region

  After the image is converted to the HSV color space, you can extract a color region. The function used here is inRange().

void inRange(
	InputArray 	src,	//输入图像
	InputArray	lowerb,	//下边界数组阈值
	InputArray	upperb,	//上边界数组阈值
	OutputArray dst		//输出图像
);

Since the HSV value ranges are H (0-180), S (0-255), and V (0-255), the lower and upper bound array thresholds can be obtained from the table below.

BlackGrayWhiteRedOrangeYellowGreenCyanBluePurple
hmin0000/15611263578100125
hmax18018018010/18025347799124155
smin00043434343434343
smax2554330255255255255255255255
vmin04622146464646464646
vmax46220255255255255255255255255

  Because this article replaces a green screen, we use the green thresholds: hmin=35, hmax=77, smin=43, smax=255, vmin=46, and vmax=255. That gives a lower bound array of Scalar(35, 43, 46) and an upper bound array of Scalar(77, 255, 255).

  The program for extracting the green-screen region is therefore:

void MyDemo::inRange_Demo(Mat& image) {
   Mat hsv;	//存储转换后的hsv图像
   cvtColor(image, hsv, COLOR_BGR2HSV);//进行色彩空间转换
   Mat mask;	//存储提取绿幕区域后的图像
   inRange(hsv, Scalar(35, 43, 46), Scalar(77, 255, 255), mask);
   imshow("mask", mask);
}

II. Extracting the Green-Screen Region

II. Extracting the Green-Screen Region (2)

III. Replacing the Background

  We have already extracted the green-screen region. Next, invert the extracted region to select the person, then copy the person onto the new background.   Inverting the selected region uses the bitwise_not() function.   Copying the image uses the image.copyTo(newimage,mask) method. It overlays mask on image, makes the points in image that correspond to pixels with value 0 (black) in mask transparent, keeps the other points, and copies the retained points into newimage.

void MyDemo::inRange_Demo(Mat& image) {

	//转换色彩空间
	Mat hsv;
	cvtColor(image, hsv, COLOR_BGR2HSV);

	//提取绿幕区域
	Mat mask;
	inRange(hsv, Scalar(35, 43, 46), Scalar(77, 255, 255), mask);
	imshow("mask", mask);

	//反转提取人物区域	
	bitwise_not(mask, mask);

	//人物复制到新背景中
	Mat bg = imread("E:/Program/OpenCV/vcworkspaces/opencv_452/img/plantbg.jpg");//背景图片
	image.copyTo(bg, mask);
	imshow("Finished", bg);
}

III. Replacing the Background

III. Replacing the Background (2)