【问题标题】:QPixmap(): argument 1 has unexpected type 'numpy.ndarray'QPixmap():参数 1 具有意外类型“numpy.ndarray”
【发布时间】:2021-05-23 18:31:49
【问题描述】:

我有这段代码可以拍摄图像并检测边缘,但它给了我这个错误

TypeError: QPixmap(): argument 1 has unexpected type 'numpy.ndarray'
def detect_Image(self):
    self.statusbar.showMessage('Processing ...')
    img = plt.imread(str(self.ImageFile))
    greyImg = img.mean(axis=2, keepdims=True)/255.0
    greyImg = np.concatenate([greyImg]*3, axis=2)
    vertical_filter = [[-1, -2, -1], [0, 0, 0], [1, 2, 1]]
    horizontal_filter = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]
    n, m, d = greyImg.shape
    edges_img = np.zeros_like(img)
    for row in range(3, n-2):
        for col in range(3, m-2):
            local_pixels = img[row-1:row+2, col-1:col+2, 0]
            vertical_treansformed_pixels = vertical_filter * local_pixels
            vertical_score = vertical_treansformed_pixels.sum() / 8
            horizontal_treansformed_pixels = horizontal_filter*local_pixels
            horizontal_score = horizontal_treansformed_pixels.sum() / 4
            edge_score = (vertical_score ** 2 + horizontal_score ** 2) ** 0.5
            edges_img[row, col] = [edge_score] * 3

    edges_img = edges_img/edges_img.max()
    
    self.NewImageFile = edges_img
    self.img_NewImage.setPixmap(QtGui.QPixmap(edges_img))
    self.statusbar.showMessage('Edges detected and new image generated')

【问题讨论】:

    标签: python numpy matplotlib pyqt5


    【解决方案1】:

    您可以使用 python 图像库 (PIL) 将 numpy.ndarray 转换为图像格式

    ##First import PIL library
    from PIL import Image as im
    ## then convert it to image format
    data = im.fromarray(edges_img)
    ## save the image file as png
    data.save('image.png')
    ##display saved image in Qpixmap
    self.img_NewImage.setPixmap(QtGui.QPixmap("image.png"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 2012-12-12
      • 2021-03-18
      • 1970-01-01
      • 2020-10-17
      • 2015-04-12
      相关资源
      最近更新 更多