【问题标题】:Morphological erosion on digital images with Python - error使用 Python 对数字图像进行形态侵蚀 - 错误
【发布时间】:2021-05-30 06:06:44
【问题描述】:

我尝试使用 Python 对数字图像实施形态侵蚀。我上传了一个二进制图像并定义了一个结构元素 - one(3,3)。但是当我运行代码时,出现以下错误:

RuntimeError: 序列参数的长度必须等于输入等级

请协助。以下是我的代码:

import numpy as np
import cv2
from matplotlib import pyplot as plt
from skimage import data, io, color

bi_im1 = plt.imread("binary1.jpg")
plt.imshow(bi_im1)
print("Original binary image1")

def plot_comparison(original, filtered, filter_name):

    fig, (ax1, ax2) = plt.subplots( ncols=2, figsize=(512, 256), sharex=True,
                                   sharey=True)
    ax1.imshow(original, cmap=plt.cm.gray)
    #ax1.set_title('original')
    ax1.axis('off')
    ax2.imshow(filtered, cmap=plt.cm.gray)
    ax2.set_title(filter_name)
    ax2.axis('off')

from skimage.morphology import (erosion, dilation, opening, closing)
from skimage.morphology import disk
selem = np.array([[1,1,1],
                    [1,1,1],
                    [1,1,1]])
plt.imshow(selem, cmap='gray', vmax=1, vmin=0);
eroded1 = erosion(bi_im1, selem)
plot_comparison(bi_im1, eroded1, 'erosion')

我的二进制图像“binary1.jpg”上传如下:

binaryimage1

【问题讨论】:

  • 欢迎来到 SO!您能否发布bi_im1.shape 的输出?它应该是一个二进制图像(尺寸为(m,n)),以便腐蚀操作起作用。
  • 我得到的是 (256,256,3)。这是否意味着这张图片仍然不是二进制的?
  • 没错。您需要将其转换为二进制图像,然后执行侵蚀。
  • 如果我的回答能帮助您解决问题,请接受。谢谢!

标签: python mathematical-morphology


【解决方案1】:

发布我提到的comment - 您正在尝试对 RGB 图像执行腐蚀操作。您需要将 RGB 图像转换为二进制图像,然后对结构元素进行腐蚀。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 2016-10-30
    • 2012-09-22
    • 1970-01-01
    相关资源
    最近更新 更多