numpy array transpose and image

Basic attributes

First, understand the basic properties of numpy arrays.

Attributes meaning
narray.ndim Rank, dimension, the rank of one-dimensional array is 1, the rank of two-dimensional array is 2
narray.shape Dimensions, number of rows, number of columns, etc.
narray.size The total number of elements, equal to the product of the tuple elements in the shape attribute
narray.dtype Element type

transpose 与 reshape

import numpy as np

# 一维数组transpose无意义
data = np.arange(4)
print(data)
data = data.transpose()
print(data)


# 二维数组,横纵坐标互换
data = np.arange(8).reshape(4, 2)
print(data)
data = data.transpose()
print(data)

# 三维数组,可以理解为2个 3行 * 4列的二维数组
data = np.arange(24).reshape(2, 3, 4)
print(data)

# transpose其实接受参数,不过对于一二维数组没有必要
# 参数可以是0,1,2,可以想象成对应x,y,z坐标
data = data.transpose(2, 1, 0)
print(data)

There is nothing to say about one- and two-dimensional arrays. The transpose of three-dimensional arrays is a little more complicated to find correspondences, but compared to finding specific correspondences, I think it’s better to understand:

  1. The 0th dimension: how many two-dimensional arrays there are
  2. The first dimension: the number of rows corresponding to each two-dimensional array
  3. The second dimension: the number of columns corresponding to each two-dimensional array

Give a simple example:

img = Image.open(r"G:\tmp\wbc.jpg")
img_arr = np.array(img).astype(np.uint8)
print(img_arr.shape)
print(img_arr)

img_arr = img_arr.transpose(2, 0, 1)
print(img_arr.shape)
print(img_arr)

A 540 * 720 picture, the shape of an array converted by numpy is (720,540,3), which is equivalent to 720 arrays with 540 rows and 3 columns. The corresponding relationship between the parameters:

  1. 0th dimension: 720
  2. The first dimension: 540
  3. The second dimension: 3

shape

We can use transpose(2, 0, 1) to change the array into the second parameter to the 0th parameter, the 0th parameter to the first, and the first parameter to the second, so:

  1. 0th dimension: 3
  2. The first dimension: 720
  3. The second dimension: 540

In this way, the shape of the array becomes (3,720,540), which is equivalent to three two-dimensional arrays with 720 rows and 540 columns, so that we can successfully separate RGB for further calculations.

transpose

application

Gray world algorithm in white balance:

import numpy as np
from PIL import Image


def gray_world(src_img_path, dest_img_path):
    """
    灰度世界算法
    """
    src_img = Image.open(src_img_path)
    # 图形转numpy数组
    image_arr = np.array(src_img).astype(np.uint32)
    # 分离RGB
    image_arr = image_arr.transpose(2, 0, 1)

    avg_red = np.average(image_arr[0])
    avg_green = np.average(image_arr[1])
    avg_blue = np.average(image_arr[2])

    gray = (avg_red + avg_green + avg_blue) / 3

    kred = gray / avg_red
    kgreen = gray / avg_green
    kblue = gray / avg_blue

    image_arr[0] = np.minimum(image_arr[0] * kred, 255)
    image_arr[1] = np.minimum(image_arr[1] * kgreen, 255)
    image_arr[2] = np.minimum(image_arr[2] * kblue, 255)

    # 重新合并RGB
    image_arr = image_arr.transpose(1, 2, 0)
    # numpy数组转图像
    img = Image.fromarray(image_arr.astype('uint8')).convert('RGB')
    img.save(dest_img_path)

if __name__ == '__main__':
    img_path = r"G:\tmp\2d.jpg";
    gray_world(img_path, r"G:\tmp\gray_world.jpg")

Guess you like

Origin blog.csdn.net/trayvontang/article/details/108391139