在Python中将plt.imshow()更改为cv2.imshow()

时间:2020-06-25 14:26:27

标签: python opencv

我有如下的python代码。

def konvolusi(self, X, F):
    X_height = X.shape[0]
    X_width = X.shape[1]
    F_height = F.shape[0]
    F_width = F.shape[1]
    H = (F_height) // 2
    W = (F_width) // 2
    out = np.zeros((X_height, X_width))
    for i in np.arange(H+1, X_height - H):
        for j in np.arange(W+1, X_width - W):
            sum = 0
            for k in np.arange(-H, H+1):
                for l in np.arange(-W, W+1):
                    a = X[i+k, j+l]
                    w = F[H+k, W+l]
                    sum += (w*a)
                out[i,j] = sum
    return out

def gaussianBlur(self):
    img1 = self.image
    gaussian = (1.0 / 345) * np.array(
        [[1, 5, 7, 5, 1],
        [5, 20, 33, 20, 5],
        [7, 33, 55, 33, 7],
        [5, 20, 33, 20,5],
        [1, 5, 7, 5, 1]])
    img = self.konvolusi(img1, gaussian)
    self.image = img;
    plt.imshow(img, cmap='gray', interpolation='bicubic')
    plt.xticks([], plt.yticks([]))
    plt.show()

在def gaussianBlur()中,使用plt.imshow()函数显示img变量,如何使用cv2.imshow()函数显示img变量?

1 个答案:

答案 0 :(得分:1)

您需要导入cv2

img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
cv2.imshow('img',img)
cv2.waitKey(0)