Numpy remove a dimension from np array

前端 未结 5 1470
南方客
南方客 2020-12-14 15:02

I have some images I want to work with, the problem is that there are two kinds of images both are 106 x 106 pixels, some are in color and some are black and white.

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 15:31

    well, you should be careful when you are trying to reduce the dimensions of an image. An Image is normally a 3-D matrix that contains data of the RGB values of each pixel. If you want to reduce it to 2-D, what you really are doing is converting a colored RGB image into a grayscale image.

    And there are several ways to do this like you can take the maximum of three, min, average, sum, etc, depending on the accuracy you want in your image. The best you can do is, take a weighted average of the RGB values using the formula

    Y = 0.299R + 0.587G + 0.114B

    where R stands for RED, G is GREEN and B is BLUE. In numpy, this can be written as

    new_image = img[:, :, 0]*0.299 + img[:, :, 1]*0.587 + img[:, :, 2]*0.114
    

提交回复
热议问题