如何返回plt图像?

问题描述 投票:0回答:1

我想读取图像并使用

cmap="gray"
,然后返回图像以进行进一步的图像处理。我试过这个:

import cv2
import tkinter.filedialog as fd
from matplotlib import pyplot as plt

def change_plt_colormap(filepath):
    image = cv2.imread(filepath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray_cmap = plt.imread(gray, cmap="gray")
    return gray_cmap

filepath = fd.askopenfilename()
change_plt_colormap(filepath)

但这给出了一个错误:

TypeError: imread() got an unexpected keyword argument 'cmap'

我尝试从网络上搜索,但没有找到任何有关如何更改

cmap
参数然后返回灰度图像的说明。我怎么能做到这一点?

python
1个回答
0
投票

使用 matplotlib 的

imshow
cmap='gray'
:

import cv2
import matplotlib.pyplot as plt
import tkinter.filedialog as fd
def display_image_in_gray(filepath):
    image = cv2.imread(filepath)
    plt.imshow(image, cmap='gray')
    plt.show()

filepath = fd.askopenfilename()
display_image_in_gray(filepath)
© www.soinside.com 2019 - 2024. All rights reserved.