一、轮廓检测提取图像前景
背景和介绍
前景提取是计算机视觉领域中非常流行的任务。使用前景提取方法,我们尝试提取任何我们感兴趣的图像或对象,并丢弃其余的背景。最近基于深度学习的图像分割技术使这变得非常容易。但我们也可以使用纯计算机视觉技术来实现这一点。
在基于计算机视觉的图像前景提取方面,Grabcut 是最流行的方法之一。在 Grabcut 中,我们提供了一个矩形区域,其中可能存在感兴趣的对象。之后,Grabcut 算法会处理其余部分。
那么,如果不使用 Grabcut 算法,我们该怎么做呢?
使用OpenCV轮廓检测进行图像前景提取
简单来说,我们需要找到感兴趣对象的边界区域或像素。之后,我们可以将其视为前景图像,而将其余部分视为背景图像。
我们可以使用轮廓检测技术来实现这一点。使用轮廓检测,我们可以找到我们想要提取的对象周围的像素,然后继续进行。我们将在本文中详细介绍如何使用 OpenCV 轮廓检测实现图像前景提取。
不仅如此,我们还将尝试改变结果前景的背景,使事情变得更有趣。所以,你可以期待类似下图的效果。
在上图中,顶部图像显示原始未经编辑的图像,背景为白色。没有什么特别之处。中间图像显示前景图像。这是我们仅从顶部图像中提取人物时的图像。没问题。您在最底部图像中看到的图像是我们将提取的前景图像与新的彩色背景合并后的图像。
库和依赖项
对于本教程,我们只需要一个主要库。那就是OpenCV计算机视觉库。
我使用的是 4.2.0.32 版本。虽然我建议使用与我相同的版本,但如果使用任何 4.x 版本,您仍然不会遇到任何问题。
目录结构
在本教程中,我们将使用以下目录结构。
- 在父项目目录中,我们有两个 Python 文件,extract_foreground.py和utils.py。
- 输入文件夹包含我们将在本教程中使用的输入图像。总共有四张图片。
- 最后,输出运行 Python 脚本后,文件夹将包含输出图像。
下载后,只需将文件解压到项目目录中即可。所有图片均取自Pixabay,可免费使用。
使用OpenCV轮廓检测进行图像前景提取
从这里开始,我们将在编写代码时深入了解这两个Python文件的细节。
我们将从utils.py然后进入Python脚本extract_foreground.py文件。
这里的所有代码都将进入utils.py文件。此 Python 文件包含一些实用函数,我们可以在需要时执行这些函数。我们将这些函数分开,以便我们的代码尽可能保持干净和易读。
下面的代码块包含实用函数所需的两个导入。
查找最大轮廓的函数
我们要编写的第一个函数是找到图像中最大的轮廓区域。
这查找find_largest_contour()函数接受二值图像,找出图像中的所有轮廓,并返回最大的轮廓面积。
显示OpenCV图像的函数
我们可能需要在extract_foreground.py文件。而不是执行 OpenCV 的imshow()和waitKey()几次,我们可以定义一个函数,只用一行代码来处理可视化。
每当我们想要可视化图像时,我们都会调用show()函数,同时传递窗口名称字符串和图像数组作为参数。每次至少可以减少一行代码。
将新背景应用到提取的前景图像的函数
现在,您已经在图中看到了我们如何向提取的前景图像添加新背景。我们可能不想对每个前景图像都这样做。因此,我们将为此编写一个函数。每当我们想要将新背景应用于提取的前景图像时,我们都会调用该函数。
这应用新背景()函数接受三个参数。一个是mask3d,即前景图像蒙版。前景参数是提取的前景对象(RGB 格式)。保存名称是字符串,我们将用它将新图像保存到磁盘。
第一步是实现正常化mask3d并得到缩放后的图像mask3d和前景使用cv2.multiply(第 34 和 36 行)。
然后我们读取背景图像,调整其大小以匹配前景图像的形状,并转换其数据类型以进行进一步的操作。
在第 43 行,我们再次使用cv2.multiply得到缩放后的产品1-mask3d和新的背景。
然后我们通过添加前景和背景图像来获得带有背景的新图像。
最后,我们在屏幕上显示图像并将其保存到磁盘。
我们已经完成了所需的所有实用函数。现在我们可以继续编写使用 OpenCV 轮廓检测进行图像前景提取的代码。
使用 OpenCV 轮廓检测进行图像前景提取的代码
接下来,我们将在extract_foreground.py文件。此 Python 文件将包含我们使用 OpenCV 轮廓检测方法提取前景图像/对象所需的所有代码。
让我们开始导入我们需要的所有模块和库。
我们正在导入所有函数utils.py我们在上一节中已经介绍过了。
现在,让我们定义参数解析器来解析命令行参数。
上面的代码块中有两个标志。
-input是我们在执行代码时提供的输入图像文件的路径。
-new-background确定是否将新的背景图像应用于提取的前景。默认情况下,它将值存储为错误的执行代码时,如果我们传递-n或者--新背景然后我们才会调用函数将新的背景应用到提取的前景图像。
读取图像并转换为二值图像
现在,我们将读取要从中提取前景对象的图像。我们还将应用阈值处理将其转换为仅包含黑色和白色像素的二值图像。
读取图像后,我们应用高斯模糊来平滑边缘。这还可以消除背景中非常小的噪音。然后我们将图像转换为灰度格式并应用阈值将其转换为二进制图像。
找到最大的轮廓面积
由于我们已将图像转换为二进制格式,因此我们可以轻松找到图像中的所有轮廓。
我们称之为find_largest_contour()在第 24 行,同时将二进制图像作为参数传递。该函数返回最大的轮廓区域。然后我们创建原始图像的副本并将该轮廓区域应用于图像。我们用绿色标记所有像素,以完美地可视化轮廓区域。我们将在执行代码时看到此输出。
创建蒙版并标记确定和可能的像素
要进行任何进一步的操作,我们首先必须创建一个新的蒙版(黑色背景)。这将具有与灰度图像相同的大小。由于我们尚未调整图像大小,这意味着此蒙版将与原始图像的大小相同。
我们先看一下接下来几个操作的代码,然后进入解释部分。
首先,我们在第 29 行创建上面讨论的掩码。
在第 32 行,我们用白色像素填充创建的蒙版上的一个区域,该区域的形状将与我们迄今为止获得的最大轮廓的形状相同。例如,如果最大的轮廓区域是人的,那么我们在新蒙版上创建该形状并用白色像素填充该区域。
接下来的几行很重要。第 35 行创建了面具以免编辑原始蒙版。
在创建新蒙版时,我们将所有像素值都设为零。这意味着蒙版全是黑色。然后我们用白色轮廓形状填充它,将所有像素标记为 255。这意味着我们确切地知道所有黑色像素构成背景,所有白色像素构成前景或对象。
因此,在第 36 行,我们说任何值为 0 的像素肯定是背景像素。我们使用cv2. GC_BGD。
第 37 行表示,任何值为 255 的像素都可能是前景。我们使用cv2. GC_PR_BGD。
但由于所有像素都是 0 或 255,我们确信值为 255 的像素肯定是前景。因此,我们在第 38 行也使用以下代码标记了明显的前景:cv2. GC_FGD。
执行上述步骤非常重要,否则,新掩码上的任何未来处理都将无法正常进行。
使用已知的前景和背景像素创建最终蒙版
现在,我们知道哪些像素肯定是背景,哪些像素可能是前景,哪些像素肯定是前景。利用这些知识,我们将创建最终的二进制掩码。
以下代码块包含该代码。
在缓冲区掩码,我们已经标记了明显且可能的前景像素。因此,在创建新的掩码2,无论哪个像素肯定是前景缓冲区掩码填充值为 255。并且任何像素都是可能的前景缓冲区掩码用 0 值填充掩码2最终,我们将整个新掩码2转换为 8 位无符号整数格式。最后,上述步骤为我们提供了一个二进制掩码(二维),其中所有像素均为黑色或白色。
目前,想象一切可能有点困难。执行代码时一切都会清楚。
使mask三维化并获取最终的前景图像
现在,mask2也是二进制和二维的。但如果我们想在未来将它与彩色图像(三维)一起用于任何操作,那么我们将无法以当前形式进行操作。因此,我们将创建一个最终的mask2它将是三维的。
使用mask2,我们创建一个new_mask3d最后再增加一个维度来复制 3D 图像。然后mask3d成为我们最终的蒙版,我们在第 50 行和第 51 行对其进行像素级操作。在第 54 行,我们对最终的 3D 蒙版应用高斯模糊,使边缘更平滑一些。
现在,让我们得到最终的前景图像。
在第 58 行,我们创建原始图像的副本并将其保存为前景. 然后,掩码2为零,我们让它们在前景也是。它们是我们不需要的背景像素。我们在第 59 行执行此操作。我们有最终的前景图像。这意味着我们已成功使用 OpenCV 轮廓检测进行图像前景提取。
只剩下几个步骤了。首先是保存所有前景图像、最终的 3D 蒙版以及检测到轮廓的图像。
如果你还记得的话,我们讨论过在前景图像上应用新的背景,如果--新背景国旗是真的。我们已经在utils.py,对于我们来说现在只需要两行代码。
这标志着使用 OpenCV 轮廓检测进行前景提取的编码结束。下一步是执行代码并分析输出。 开发板商城 天皓智联 TB上有视觉设备哦 支持AI相关~ 大模型相关也可用
执行代码并分析输出
现在是时候看看执行代码后我们会得到什么结果了。
我希望你已经下载了输入图像。我们将从图片3.jpg在输入文件夹。
写在最后
我们在上一步中了解了使用 OpenCV 轮廓检测进行图像前景提取的局限性。但也有一些方法可以克服这个问题。
在应用轮廓检测之前使用良好的边缘检测技术。
使用 Grabcut 算法并按照预期的步骤进行图像前景提取。
我们可以使用深度学习分割技术来提取选择的对象。