膨胀与腐蚀

形态学操作

    形态学操作就是基于形状的一些列图像处理操作。通过结构元素作用于输入图像产生输出图像。

    结构元素,就是形态学操作的处理内核。通过内核与图像的卷积完成处理过程。内核可以有各种形状如矩形,圆形,菱形。结构元素的锚点(即与处理元素重合的点)默认为中心(参数cv::Point()-1,-1).opencv一般默认3*3的结构元素,我们也可以自己定义1,3,5,7尺寸的结构元素。例如定义一个7*7的cv::Mat element(7,7,CV_8U,Scalar(1)).

一般情况下形态学操作针对的是单通道图。


最基本的形态学操作有二:腐蚀与膨胀(Erosion 与 Dilation)。 他们的运用广泛:

消除噪声

分割(isolate)独立的图像元素,以及连接(join)相邻的元素。

寻找图像中的明显的极大值区域或极小值区域。

cv::dilate(cornerStrength,dilated,cv::Mat());
  cv::compare(cornerStrength,dilataed,localMax,cv::CMP_EQ);
利用膨胀是用最大值替换的原则,只有最大值不变,将区域中的最大值位置提取出来


膨胀腐蚀是针对较大的元素而言的,故膨胀是亮色的膨胀(暗色腐蚀),腐蚀是亮色的腐蚀(暗色膨胀)


void  erode (const  Mat&  srcMat&  dst, const  Mat&  element, Point  anchor=Point(-1, -1), int  iterations=1, int  borderType=BORDER_CONSTANT, const Scalar& borderValue=morphologyDefaultBorderValue() )

Erodes an image by using a specific structuring element.


Parameters:
  • src – The source image
  • dst – The destination image. It will have the same size and the same type as src
  • element – The structuring element used for dilation. If element=Mat() , a 3\times 3 rectangular structuring element is used
  • anchor – Position of the anchor within the element. The default value (-1, -1) means that the anchor is at the element center
  • iterations – The number of times erosion is applied
  • borderType – The pixel extrapolation method; see borderInterpolate()
  • borderValue – The border value in case of a constant border. The default value has a special meaning, see createMorphoogyFilter()

The function erodes the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the minimum is taken:


The function supports the in-place mode. Erosion can be applied several ( iterations ) times. In the case of multi-channel images each channel is processed independently.



void  erode (const  Mat&  srcMat&  dst, const  Mat&  element, Point  anchor=Point(-1, -1), int  iterations=1, int  borderType=BORDER_CONSTANT, const Scalar& borderValue=morphologyDefaultBorderValue() )

Erodes an image by using a specific structuring element.

Parameters:
  • src – The source image
  • dst – The destination image. It will have the same size and the same type as src
  • element – The structuring element used for dilation. If element=Mat() , a 3\times 3 rectangular structuring element is used
  • anchor – Position of the anchor within the element. The default value (-1, -1) means that the anchor is at the element center
  • iterations – The number of times erosion is applied
  • borderType – The pixel extrapolation method; see borderInterpolate()
  • borderValue – The border value in case of a constant border. The default value has a special meaning, see createMorphoogyFilter()

The function erodes the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the minimum is taken:

\texttt{dst} (x,y) =  \min _{(x',y'):  \, \texttt{element} (x',y') \ne0 } \texttt{src} (x+x',y+y')

The function supports the in-place mode. Erosion can be applied several ( iterations ) times. In the case of multi-channel images each channel is processed independently.

 




### 回答1: 膨胀腐蚀是数字图像处理中常用的形态学基础操作。Pytorch 是一种广泛应用于深度学习领域的开源机器学习框架。在 Pytorch 中实现膨胀腐蚀可以使用 torch.nn.functional 模块中提供的相关函数。 膨胀操作可以使目标物体膨胀,通常用于填充孔洞或连接不规则的物体。在 Pytorch 中,可以使用 dilation 函数实现膨胀操作。该函数接受两个参数,分别是输入张量和结构元素张量(即卷积),并返回进行膨胀操作后的张量。例如,使用以下代码实现一个 $3\times3$ 的半径为 1 的圆形结构元素的膨胀操作: ``` import torch.nn.functional as F input_tensor = torch.tensor([[0, 0, 0], [0, 1, 0], [0, 0, 0]], dtype=torch.float32) struct_elem = torch.tensor([[0, 1, 0], [1, 1, 1], [0, 1, 0]], dtype=torch.float32) dilated_tensor = F.conv2d(input_tensor.unsqueeze(dim=0).unsqueeze(dim=0), struct_elem.unsqueeze(dim=0).unsqueeze(dim=0), padding=1).squeeze(dim=0).squeeze(dim=0) ``` 腐蚀操作可以使目标物体收缩,通常用于去除小的噪点或分离相互重叠的物体。在 Pytorch 中,可以使用 erosion 函数实现腐蚀操作。该函数接受两个参数,分别是输入张量和结构元素张量(即卷积),并返回进行腐蚀操作后的张量。例如,使用以下代码实现一个 $3\times3$ 的半径为 1 的圆形结构元素的腐蚀操作: ``` import torch.nn.functional as F input_tensor = torch.tensor([[1, 1, 0], [0, 0, 1], [1, 0, 0]], dtype=torch.float32) struct_elem = torch.tensor([[0, 1, 0], [1, 1, 1], [0, 1, 0]], dtype=torch.float32) eroded_tensor = 1 - F.conv2d(1 - input_tensor.unsqueeze(dim=0).unsqueeze(dim=0), 1 - struct_elem.unsqueeze(dim=0).unsqueeze(dim=0), padding=1).squeeze(dim=0).squeeze(dim=0) ``` 以上是在二维图像上的操作,如果是在三维图像上的操作,则需要将卷积操作从二维扩展到三维,以处理 z 方向的信息。 ### 回答2: 膨胀(Dilation)和腐蚀(Erosion)是数字图像处理中常用的形态学处理方法,它们可以用来增强或者减弱图像中的一些特征区域。PyTorch是一种基于Python的机器学习框架,它提供了强大的张量计算功能和自动微分机制,能够方便地实现这两种操作。 膨胀操作是将图像中的特定区域进行扩张。具体来说,膨胀过程是将每个像素点的值替换为该点周围像素中最大的值。这可以对于删除孔洞等操作非常有效。在PyTorch中,我们可以利用`torch.nn.functional`模块中的`dilation`函数来进行膨胀操作。具体来说,该函数有两个参数: - input:进行操作的张量,可以是一幅灰度图或者一个彩色图像 - kernel:表示膨胀或者腐蚀的形状,也可以称为结构元素,如果希望膨胀的区域更大,可以增加`kernel`的大小。 下面是一个样例代码,通过对一个图片进行膨胀操作实现了对黑色斑点去除的效果: ```python import torch import torch.nn.functional as F from PIL import Image img = Image.open('example.png') # 将图片转换为张量 img_tensor = F.to_tensor(img).unsqueeze(0) # 定义一个结构元素 kernel = torch.ones((3,3)) # 进行膨胀操作 dilated_img = F.dilation(img_tensor, kernel).squeeze(0) # 显示原图和处理后的图像 img.show() Image.fromarray((dilated_img * 255).byte().numpy(), mode='L').show() ``` 腐蚀操作是将图像中的特定区域进行收缩。具体来说,腐蚀过程是将每个像素点的值替换为该点周围像素中最小的值。这可以对于去除噪声等操作非常有效。在PyTorch中,我们可以利用`torch.nn.functional`模块中的`erosion`函数来进行腐蚀操作。与`dilation`函数相似,`erosion`函数也有两个参数: - input:进行操作的张量,可以是一幅灰度图或者一个彩色图像。 - kernel:表示膨胀或者腐蚀的形状,也可以称为结构元素,如果希望腐蚀的区域更小,可以缩小`kernel`的大小。 下面是一个样例代码,通过对一个图片进行腐蚀操作实现了对白色的噪点去除的效果: ```python import torch import torch.nn.functional as F from PIL import Image img = Image.open('example.png') # 将图片转换为张量 img_tensor = F.to_tensor(img).unsqueeze(0) # 定义一个结构元素 kernel = torch.ones((3,3)) # 进行腐蚀操作 eroded_img = F.erosion(img_tensor, kernel).squeeze(0) # 显示原图和处理后的图像 img.show() Image.fromarray((eroded_img * 255).byte().numpy(), mode='L').show() ``` 总的来说,膨胀腐蚀是数字图像处理中非常常见的操作,它们可以用于去除噪声、检测边缘等多种场景。在PyTorch中,我们可以利用`torch.nn.functional`模块中的`dilation`和`erosion`函数来进行操作,同时需要注意选择合适的结构元素以及张量的维度等问题。 ### 回答3: 膨胀腐蚀是数字图像处理中常用的一种形态学运算,它们可以对二值化图像进行简单的形态改变,从而改善图像质量、提取图像特征等。在PyTorch中实现膨胀腐蚀,我们可以借助PyTorch自带的卷积函数进行运算。 首先,我们需要导入torch和torchvision模块,以及PIL库中的Image模块。如果没有安装这些库,可以使用pip install进行安装。 ``` import torch import torchvision from PIL import Image ``` 接下来,我们可以读取需要进行膨胀腐蚀的二值化图像,可以使用Image模块中的open函数打开图像,并转换为numpy数组和Tensor张量。 ``` # 读取图像 img = Image.open('example.png') # 转换为numpy数组 img_np = np.array(img) # 转换为Tensor张量 img_tensor = torch.from_numpy(img_np).unsqueeze(0).float() ``` 在PyTorch中,可以使用nn.functional模块中的dilation和erosion函数来实现膨胀腐蚀。这两个函数都需要输入一个Tensor张量和一个卷积,卷积通常是一个方形矩阵,其大小决定了膨胀腐蚀的程度。在本例中,我们选择一个边长为5的方形矩阵作为卷积进行膨胀腐蚀。 ``` from torch.nn.functional import dilation, erosion # 定义卷积 kernel_size = 5 kernel = torch.ones((kernel_size, kernel_size)) # 进行膨胀操作 img_dilate = dilation(img_tensor, kernel) # 进行腐蚀操作 img_erode = erosion(img_tensor, kernel) ``` 最后,我们可以将膨胀腐蚀的结果转换为PIL图像,并保存到本地文件。 ``` # 将Tensor张量转换为numpy数组 img_dilate_np = img_dilate.squeeze(0).detach().numpy().astype('uint8') img_erode_np = img_erode.squeeze(0).detach().numpy().astype('uint8') # 将numpy数组转换为PIL图像 img_dilate_pil = Image.fromarray(img_dilate_np) img_erode_pil = Image.fromarray(img_erode_np) # 保存膨胀腐蚀的结果 img_dilate_pil.save('dilated.png') img_erode_pil.save('eroded.png') ``` 通过以上步骤,我们可以实现在PyTorch中进行膨胀腐蚀操作,从而对二值化图像进行形态学改变。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值