Numpy study notes (3)

Wednesday, January 19, 2022

Numpy learning the third lesson - examples (hand-drawn processing of images)

Image processing is actually to deform the matrix.

1. Code :

from PIL import Image (pil库用于图像处理)

import numpy as np a = np.asarray(Image.open('./beijing.jpg').convert('L')).astype('float')

depth = 10.  # (0-100)

grad = np.gradient(a) #取图像灰度的梯度值 grad_x, grad_y = grad  #分别取横纵图像梯度值 grad_x = grad_x*depth/100.

grad_y = grad_y*depth/100. A = np.sqrt(grad_x**2 + grad_y**2 + 1.)

uni_x = grad_x/A

uni_y = grad_y/A

uni_z = 1./A

vec_el = np.pi/2.2  # 光源的俯视角度,弧度值 
vec_az = np.pi/4.  # 光源的方位角度,弧度值 
dx = np.cos(vec_el)*np.cos(vec_az)  #光源对x 轴的影响 
dy = np.cos(vec_el)*np.sin(vec_az)  #光源对y 轴的影响 
dz = np.sin(vec_el)  #光源对z 轴的影响 
b = 255*(dx*uni_x + dy*uni_y + dz*uni_z)  #光源归一化 
b = b.clip(0,255) 
im = Image.fromarray(b.astype('uint8'))  #重构图像 
im.save('./beijingHD.jpg')

效果:

2. Problems that arise:

problem analysis:

The currently allocated storage space is insufficient, and the computer terminates the running of the program prematurely.

Solution:

  1. Change float to float32 (change data type)
  2. Change the computer's virtual memory allocation (I didn't succeed)

Successfully resolved Windows MemoryError: Unable to allocate 6.38 GiB for an array with shape (38_Wang Yilang's Blog-CSDN Blog

3. Easter eggs

The conversion is like a ghost:

Guess you like

Origin blog.csdn.net/m0_57491181/article/details/125534829