【OpenCV实现图像:图像处理技巧形态学滤波之腐蚀操作,RuntimeError: sequence argument must have length equal to input rank】】

概要

提示:这里可以添加技术概要

例如:

openAI 的 GPT 大模型的发展历程。

腐蚀操作原理

腐蚀是形态学图像处理中的一种操作,它通过从图像的边缘逐渐去除像素,从而精细地缩小图像中的对象。腐蚀操作的原理是通过考虑每个像素的周围区域,将该像素的值设置为周围区域内所有像素的最小值。在二值图像中,如果周围区域内存在值为0的像素,那么该像素的值就会被设置为0。简单来说,腐蚀操作可以使图像中的对象变小,去除对象边界上的像素,从而实现图像的精细化处理。

首先导入我们今天的图像

import numpy as np
import matplotlib.pyplot as plt
# Define the image
original_image = np.array([[0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 1, 1, 1, 0, 0],
                           [0, 0, 1, 1, 1, 1, 0, 0],
                           [0, 1, 1, 1, 1, 0, 0, 0],
                           [0, 1, 1, 1, 0, 0, 0, 0],
                           [0, 1, 1, 1, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0]])

plt.figure(figsize=(10,10))
plt.imshow(original_image, cmap='gray', extent=[0, 8, 0, 8])
plt.title('Original Image', fontsize=20);
plt.show()

在这里插入图片描述

选择结构化元素

import numpy as np
import matplotlib.pyplot as plt
# Define the image

# Define the structuring element
selem_cross = np.array([[0,1,0],
                        [1,1,1],
                        [0,1,0]])
plt.figure(figsize=(9,9))
plt.imshow(selem_cross, cmap='gray')
plt.title('Structuring Element: Cross', fontsize=20);
plt.show()

在这里插入图片描述

腐蚀操作效果

经过上述操作,我们定义了需要操作的原始图像和相应的结构化模板元素,接着我们使用函数apply_erosion 来将上述结构化模板元素作用于相应的图像中,代码如下:

import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import morphology

# 定义原始图像
original_image = np.array([[0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 1, 1, 1, 0, 0],
                           [0, 0, 1, 1, 1, 1, 0, 0],
                           [0, 1, 1, 1, 1, 0, 0, 0],
                           [0, 1, 1, 1, 0, 0, 0, 0],
                           [0, 1, 1, 1, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0]])

# 定义结构化元素(3x3的十字架形状)
selem_cross = np.array([[0, 1, 0],
                        [1, 1, 1],
                        [0, 1, 0]])

# 执行腐蚀操作的函数
def apply_erosion(image, selem):
    eroded_image = morphology.grey_erosion(image, structure=selem)  # 使用structure参数传递结构化元素
    fig, axes = plt.subplots(1, 3, figsize=(15, 10))
    ax = axes.ravel()
    ax[0].imshow(selem, cmap='gray', extent=[0, selem.shape[1], 0, selem.shape[0]])
    ax[0].set_title('Structuring Element', fontsize=20)
    ax[1].imshow(image, cmap='gray', extent=[0, image.shape[1], 0, image.shape[0]])
    ax[1].set_title('Original Image', fontsize=20)
    ax[2].imshow(eroded_image, cmap='gray', extent=[0, image.shape[1], 0, image.shape[0]])
    ax[2].set_title('Eroded Image', fontsize=20)
    plt.tight_layout()
    plt.show()

# 对原始图像应用腐蚀操作
apply_erosion(original_image, selem_cross)

在这里插入图片描述
遇到的报错:

D:\anaconda\envs\yolov5\python.exe E:\yolo项目\Opencv-project-main\Opencv-project-main\CVZone\guangliu\four.py 
E:\yolo项目\Opencv-project-main\Opencv-project-main\CVZone\guangliu\four.py:22: DeprecationWarning: Please use `grey_erosion` from the `scipy.ndimage` namespace, the `scipy.ndimage.morphology` namespace is deprecated.
  eroded_image = morphology.grey_erosion(image, selem)
Traceback (most recent call last):
  File "E:\yolo项目\Opencv-project-main\Opencv-project-main\CVZone\guangliu\four.py", line 35, in <module>
    apply_erosion(original_image, selem_cross)
  File "E:\yolo项目\Opencv-project-main\Opencv-project-main\CVZone\guangliu\four.py", line 22, in apply_erosion
    eroded_image = morphology.grey_erosion(image, selem)
  File "D:\anaconda\envs\yolov5\lib\site-packages\scipy\ndimage\_morphology.py", line 1229, in grey_erosion
    return _filters._min_or_max_filter(input, size, footprint, structure,
  File "D:\anaconda\envs\yolov5\lib\site-packages\scipy\ndimage\_filters.py", line 1149, in _min_or_max_filter
    sizes = _ni_support._normalize_sequence(size, input.ndim)
  File "D:\anaconda\envs\yolov5\lib\site-packages\scipy\ndimage\_ni_support.py", line 67, in _normalize_sequence
    raise RuntimeError(err)
RuntimeError: sequence argument must have length equal to input rank

Process finished with exit code 1

修改部分:

这个错误是由于传递给morphology.grey_erosion函数的结构化元素selem的形状不符合函数的要求引起的。

根据错误信息,sequence argument must have length equal to input rank,这意味着结构化元素的维度(即形状的长度)应该等于输入图像的维度。原始图像original_image是一个8x8的二维数组,所以结构化元素的形状应该也是一个2x2的二维数组。

eroded_image = morphology.grey_erosion(image, structure=selem)  # 使用structure参数传递结构化元素

使用:

 eroded_image = morphology.grey_erosion(image, selem)

在这里插入图片描述

小结

例如,如果选择使用一个正方形作为的结构元素,它会考虑到像素周围的所有相邻像素。这意味着,如果结构元素的大小较大,腐蚀操作会更加明显地缩小图像中的对象。相反,如果选择一个较小的结构元素,腐蚀操作的效果会更加细微,可能只会去除对象的边缘像素。

另外,也可以选择圆形结构元素,它的影响范围会更加均匀,适用于需要保持对象形状的情况。同时,还可以根据特定需求创建自定义形状的结构元素,以满足特定形态学操作的要求。
正方形结构元素:


# Define the structuring element 
selem_square = np.array([[0,0,0,0],
                         [0,1,1,0],
                         [0,1,1,0],
                         [0,0,0,0]])

# Apply erosion on the original image with square structuring element
apply_erosion(original_image, selem_square)

完整代码:

import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import morphology

# 定义原始图像
original_image = np.array([[0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 1, 1, 1, 0, 0],
                           [0, 0, 1, 1, 1, 1, 0, 0],
                           [0, 1, 1, 1, 1, 0, 0, 0],
                           [0, 1, 1, 1, 0, 0, 0, 0],
                           [0, 1, 1, 1, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0],
                           [0, 0, 0, 0, 0, 0, 0, 0]])

# 定义结构化元素(3x3的十字架形状)

# Define the structuring element
selem_square = np.array([[0,0,0,0],
                         [0,1,1,0],
                         [0,1,1,0],
                         [0,0,0,0]])

# Apply erosion on the original image with square structuring element

# 执行腐蚀操作的函数
def apply_erosion(image, selem):
    eroded_image = morphology.grey_erosion(image, structure=selem)  # 使用structure参数传递结构化元素
    fig, axes = plt.subplots(1, 3, figsize=(15, 10))
    ax = axes.ravel()
    ax[0].imshow(selem, cmap='gray', extent=[0, selem.shape[1], 0, selem.shape[0]])
    ax[0].set_title('Structuring Element', fontsize=20)
    ax[1].imshow(image, cmap='gray', extent=[0, image.shape[1], 0, image.shape[0]])
    ax[1].set_title('Original Image', fontsize=20)
    ax[2].imshow(eroded_image, cmap='gray', extent=[0, image.shape[1], 0, image.shape[0]])
    ax[2].set_title('Eroded Image', fontsize=20)
    plt.tight_layout()
    plt.show()

# 对原始图像应用腐蚀操作
apply_erosion(original_image, selem_square)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_47869094/article/details/134191197