【问题标题】:Getting an error when trying to do morphological transformations on an 8x8 matrix尝试对 8x8 矩阵进行形态变换时出错
【发布时间】:2019-09-23 12:57:27
【问题描述】:

我正在尝试对具有 3x3 十字形内核的 8x8 矩阵进行形态转换。我想用内核 B1 应用腐蚀、膨胀、打开和关闭 A1。我收到一个错误,我不知道如何解决这个问题。

这是我目前所拥有的。

import cv2 as cv
import numpy as np

# A1 = 8x8 matrix
A1 = np.array([[0,0,0,0,0,0,0,0],
               [0,0,0,1,1,1,1,0],
               [0,0,0,1,1,1,1,0],
               [0,1,1,1,1,1,1,0],
               [0,1,1,1,1,1,1,0],
               [0,1,1,1,1,0,0,0],
               [0,1,1,1,1,0,0,0],
               [0,0,0,0,0,0,0,0]])

# Cross-shaped kernel (structuring element)
cv.getStructuringElement(cv.MORPH_CROSS,(3,3))
kernel = np.array ([[0, 1, 0],
                    [1, 1, 1],
                    [0, 1, 0]], dtype = np.uint8)

# Dilation
dilation = cv.dilate(A1,kernel,iterations = 1)
cv.imshow('dilation', dilation)

# Erosion
erosion = cv.erode(A1,kernel,iterations = 1)
cv.imshow('erosion', erosion)

# Opening
opening = cv.morphologyEx(A1, cv.MORPH_OPEN, kernel)
cv.imshow('opening', opening)

# Closing
closing = cv.morphologyEx(A1, cv.MORPH_CLOSE, kernel)
cv.imshow('closing', closing)

cv.waitKey(0)
cv.destroyAllWindows()
cv.waitKey(1)  # If I dont have this line then the window won't close for me on my mac when I enter a key

我不知道为什么会出现这个错误?


  File "/Users/p/.spyder-py3/My_OpenCV_Files/Homework3_CSC317/Hw3-problem1.py", line 24, in <module>
    dilation = cv.dilate(A1,kernel,iterations = 1)

error: OpenCV(3.4.2) /opt/concourse/worker/volumes/live/
9523d527-1b9e-48e0-7ed0a36adde286f0/volume/opencvsuite_1535558719691/work
/modules/imgproc/src/morph.cpp:973: 
error: (-213:The function/feature is not implemented) Unsupported data type (=4) in function 'getMorphologyFilter'

【问题讨论】:

    标签: python opencv image-processing mathematical-morphology image-morphology


    【解决方案1】:

    您的错误信息

    函数“getMorphologyFilter”中不支持的数据类型 (=4)

    表示cv.dilation 不支持输入图像的数据类型(type 4 is signed 32-bit integer)。

    The documentation 表示支持的类型有:CV_8UCV_16UCV_16SCV_32FCV_64F

    所以解决方案就是创建一个数组,其中一种类型作为输入。

    【讨论】:

    • A1转换为np.uint8数据类型为:A1 = np.uint8(A1)
    • 解决了我的问题谢谢!
    猜你喜欢
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    相关资源
    最近更新 更多