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

本文介绍了一种图像处理方法,通过两幅图片相减或相除(归一化)来去除背景光,实现图像增强。文中详细解释了如何使用OpenCV将图像转换为32位浮点数格式进行除法运算,再将其转换回8位格式。此外,还提供了在无背景图片时获取背景模板的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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;
}

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值