简体   繁体   English

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

[英]Change plt.imshow() into cv2.imshow() in Python

I have python code like below.我有 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()

in def gaussianBlur() , the img variable is displayed using the plt.imshow() function, how do I display the img variable with the cv2.imshow() function?在 def gaussianBlur()中,使用plt.imshow() function 显示 img 变量,如何使用 cv2.imshow( cv2.imshow() function 显示 img 变量?

You need to import cv2您需要导入 cv2

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM