Converting an image to grayscale using numpy

后端 未结 3 1888
鱼传尺愫
鱼传尺愫 2021-01-13 15:11

I have an image represented by a numpy.array matrix nxm of triples (r,g,b) and I want to convert it into grayscale, , using my own function.

My

3条回答
  •  青春惊慌失措
    2021-01-13 15:34

    Here is a working code:

    def grayConversion(image):
        grayValue = 0.07 * image[:,:,2] + 0.72 * image[:,:,1] + 0.21 * image[:,:,0]
        gray_img = grayValue.astype(np.uint8)
        return gray_img
    
    orig = cv2.imread(r'C:\Users\Jackson\Desktop\drum.png', 1)
    g = grayConversion(orig)
    
    cv2.imshow("Original", orig)
    cv2.imshow("GrayScale", g)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

提交回复
热议问题