码迷,mamicode.com
首页 > 编程语言 > 详细

图像的数组表示

时间:2020-06-27 09:54:03      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:abs   eric   参数   --   ali   size   变换   return   tle   

from PIL import Image
import numpy as np
import numpy.core.numeric as _nx
import matplotlib.pyplot as plt

image_name = ‘Lenna_(test_image).png‘
image_name_2 = ‘Lenna_(test_image)2.png‘
image_name_3 = ‘Lenna_(test_image)3.png‘

def image_gradient(f, **kwargs):
"""
图像梯度求导
:param f: 类数组
:return: 数组或数组列表,dtype=np.double
可选参数:axis - 空或整数或整数列表
"""
f = np.asanyarray(f, dtype=np.double)
N = f.ndim # number of dimensions

axes = kwargs.pop(‘axis‘, None)
if axes is None:
axes = tuple(range(N))
else:
axes = _nx.normalize_axis_tuple(axes, N)
len_axes = len(axes)

outvals = []

# create slice objects --- initially all are [:, :, ..., :]
slice1 = [slice(None)] * N
slice2 = [slice(None)] * N
slice3 = [slice(None)] * N
slice4 = [slice(None)] * N

otype = f.dtype

for axis in axes:
if f.shape[axis] < 2:
raise ValueError(
"Shape of array too small to calculate a numerical gradient, "
"at least 2 elements are required.")
# result allocation
out = np.empty_like(f, dtype=otype)

# Numerical differentiation: 2nd order interior
slice1[axis] = slice(1, -1)
slice2[axis] = slice(None, -2)
slice3[axis] = slice(1, -1)
slice4[axis] = slice(2, None)
# 1D equivalent -- out[0] = (f[1] - f[-1]) / 2
out[tuple(slice1)] = (f[tuple(slice4)] - f[tuple(slice2)]) / 2

slice1[axis] = 0
slice2[axis] = 1
slice3[axis] = 0
# 1D equivalent -- out[0] = (f[1] - f[0]) / (x[1] - x[0])
out[tuple(slice1)] = (f[tuple(slice2)] - f[tuple(slice3)])

slice1[axis] = -1
slice2[axis] = -1
slice3[axis] = -2
# 1D equivalent -- out[-1] = (f[-1] - f[-2]) / (x[-1] - x[-2])
out[tuple(slice1)] = (f[tuple(slice2)] - f[tuple(slice3)])

outvals.append(out)

# reset the slice object in this dimension to ":"
slice1[axis] = slice(None)
slice2[axis] = slice(None)
slice3[axis] = slice(None)
slice4[axis] = slice(None)

if len_axes == 1:
return outvals[0]
else:
return outvals

def show_image(grad_x, grad_y, grad):
plt.figure(figsize=(10, 5)) # 设置窗口大小
plt.suptitle(‘image_gradient‘) # 图片名称
plt.subplot(1, 3, 1)
plt.title(‘grad_x‘)
plt.imshow(grad_x, cmap=‘gray‘), plt.axis(‘off‘)
plt.subplot(1, 3, 2)
plt.title(‘gray_y‘)
plt.imshow(grad_y, cmap=‘gray‘), plt.axis(‘off‘)
plt.subplot(1, 3, 3)
plt.title(‘grad‘)
plt.imshow(grad, cmap=‘gray‘), plt.axis(‘off‘)
plt.savefig(‘./grad.png‘) # 保存图像
plt.show()

if __name__ == ‘__main__‘:
‘‘‘
# 图像的数组表示
img = np.array(Image.open(image_name))
#图像的变换,反色
b = [255, 255, 255] - img
im = Image.fromarray(b.astype(np.uint8))
im.save(image_name_2)

‘‘‘
# 图像的数组表示
img = np.array(Image.open(image_name).convert(‘L‘))
# 图像的变换,反色
b = 255 - img
im = Image.fromarray(b.astype(np.uint8))
im.save(image_name_3)

# grad_x = image_gradient(img, axis=1)
# grad_y = image_gradient(img, axis=0)
# grad = np.sqrt(grad_x ** 2 + grad_y ** 2)
# abs_x = np.array(np.abs(grad_x), dtype=np.uint8)
# abs_y = np.array(np.abs(grad_y), dtype=np.uint8)
# abs_grad = np.array(np.abs(grad), dtype=np.uint8)
# show_image(abs_x, abs_y, abs_grad)

图像的数组表示

标签:abs   eric   参数   --   ali   size   变换   return   tle   

原文地址:https://www.cnblogs.com/pencil2001/p/13197216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!