oencv去除背景的两种方法(来源OpenCV By Example)

  1. 两幅图片相减

  2. 两幅图片相除(0~1),取反(0~1),复原(0~255)

Mat removeLight(Mat img, Mat pattern, int method)
{
	Mat aux;
	// if method is normalization
	if(method==1)
	{
		// Require change our image to 32 float for division
		Mat img32, pattern32;
		img.convertTo(img32, CV_32F);
		pattern.convertTo(pattern32, CV_32F);
		// Divide the image by the pattern
		aux= 1-(img32/pattern32);
		// Scale it to convert to 8bit format
		aux=aux*255;
		// Convert 8 bits format
		aux.convertTo(aux, CV_8U);
	}else{
			aux= pattern-img;
		}
		return aux;
}

oencv去除背景的两种方法(来源OpenCV By Example)_第1张图片

note:

当没有背景图片时:可以利用滤波,blur with a large kernel size(eg:OCR中)等获取大致的背景图片,还可以利用多张图片(视频帧中)来获取背景模板

Mat calculateLightPattern(Mat img)
{
    Mat pattern;
    // Basic and effective way to calculate the light pattern from one image
    blur(img, pattern, Size(img.cols/3,img.cols/3));
    return pattern;
}

 

你可能感兴趣的:(图像处理)