尝试在8x8矩阵上进行形态转换时出错

时间:2019-05-05 19:59:12

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

我正在尝试对具有3x3十字形内核的8x8矩阵进行形态转换。我想对内核B1进行侵蚀,扩张,开放和接近A1。我遇到错误,不知道该如何解决。 Here what the matrix and kernel look like

这是我到目前为止所拥有的。

setMidiNote

我不知道为什么会收到此错误?

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

1 个答案:

答案 0 :(得分:1)

您的错误消息

  

'getMorphologyFilter'函数中不受支持的数据类型(= 4)

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

The documentation说受支持的类型为:CV_8UCV_16UCV_16SCV_32FCV_64F

因此解决方案只是创建一个以这些类型之一作为输入的数组。