How can I convert an RGB image into grayscale in Python?

前端 未结 12 1123
Happy的楠姐
Happy的楠姐 2020-11-22 04:43

I\'m trying to use matplotlib to read in an RGB image and convert it to grayscale.

In matlab I use this:

img = rgb2gray(imread(\'image.p         


        
12条回答
  •  失恋的感觉
    2020-11-22 05:02

    Using this formula

    Y' = 0.299 R + 0.587 G + 0.114 B 
    

    We can do

    import imageio
    import numpy as np
    import matplotlib.pyplot as plt
    
    pic = imageio.imread('(image)')
    gray = lambda rgb : np.dot(rgb[... , :3] , [0.299 , 0.587, 0.114]) 
    gray = gray(pic)  
    plt.imshow(gray, cmap = plt.get_cmap(name = 'gray'))
    

    However, the GIMP converting color to grayscale image software has three algorithms to do the task.

提交回复
热议问题