医学数字图像处理实验报告3(2023)

《医学数字图像处理》
|
实验名称 |
实验三 Numpy数组运算 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
实验目的与要求 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
实验过程 |
数组的创建
图 1 空数组及其图像
图2 全0,全1和常数矩阵及其图像 并且利用三个数组利用np.dstack函数进行堆叠,形成三通道彩色图像,并且分别调试了np.hstack和np.vstack, 对应作用分别为三个数组横向堆叠和纵向堆叠,并且了解到imshow函数的cmap映射函数对数组形成的图像就不再起映射作用了,因为三维已经确定了一个颜色,不用再去映射,不会出现灰度图像的假彩色映射,所以我们登录颜色编码网站https://tool.oschina.net/commons?type=3,选取了颜色较为艳丽的紫色,颜色编码形式为(255,0,255),为形成三通道,我们用np.full函数创建了常数为255的数组,然后利用np.zeros函数创建了第二维,然后利用np.dstack函数进行堆叠,形成图像如下所示:
图 3 彩色图像 学习代码如下: 1 #创建空二维数组,高50,宽256,由于只分配了空间,没有初始化,其值是随机的 2 arr = np.empty((100,100), dtype = np.uint8) 3 4 print(arr) 5 6 plt.imshow(arr, cmap = "gray",vmin = 0, vmax = 255 ) 7 8 plt.show() 9 10 #创建全0数组 11 12 arr = np.zeros((100,100), dtype = np.uint8) 13 14 print(arr) 15 16 plt.imshow(arr, cmap = "gray",vmin = 0, vmax = 255 ) 17 18 plt.show() 19 20 #创建全1数组 21 22 arr = np.ones((100,100), dtype = np.uint8) 23 24 print(arr) 25 26 plt.imshow(arr, cmap = "gray",vmin = 0, vmax = 255 ) 27 28 plt.show() 29 30 arr = np.full((100,100), fill_value = 128, dtype = np.uint8) 31 32 print(arr) 33 34 plt.imshow(arr, cmap = "gray",vmin = 0, vmax = 255 ) 35 36 plt.show() 37 38 arr = np.full((100,100, 3),fill_value = 200, dtype = np.uint8) 39 40 print(arr) 41 42 plt.imshow(arr, cmap = "coolwarm", vmin = 0, vmax = 255) 43 44 plt.show() 45 46 r = np.full((100,100), fill_value = 255,dtype = np.uint8) 47 48 g = np.zeros((100,100), dtype = np.uint8) 49 50 b = np.full((100,100), fill_value = 255, dtype = np.uint8) 51 52 arr = np.dstack([r, g, b]) 53 54 # arr = np.hstack([r, g, b]) 55 56 # arr = np.vstack([r, g, b]) 57 58 # cmap 只是对灰度图像起作用 59 60 plt.imshow(arr, vmin = 0, vmax = 255 ) 61 62 plt.show()
图 4 np.arange()创建数组和图像
图 5 np.linspace()创建数组和图像 学习代码如下: 1 arr = np.arange(0, 514, 3, dtype = np.uint8) 2 print(arr) 3 4 arr = arr.reshape(2,-1) 5 6 plt.axis(False) 7 8 plt.imshow(arr, cmap = "gray", vmin = 0, vmax = 255) 9 10 plt.show() 11 12 arr = np.linspace(0, 255, 128) 13 14 print(arr) 15 16 arr = arr.reshape(2, -1) 17 18 plt.axis(False) 19 20 plt.imshow(arr, cmap = "gray", vmin = 0, vmax = 255)
要求3:创建规则格网np.mgrid;
Y, x = np.mgrid[0:255:256j, 255:0:256j]
图 6 np.mgrid()创建数组和图像
1 import numpy as np 2 import matplotlib.pyplot as plt 3 4 y, x = np.mgrid[0:255:256j, 255:0:256j] 5 6 print(y) 7 8 print(x) 9 10 plt.imshow(x, cmap = "coolwarm", vmin = 0, vmax = 255) 11 12 plt.show() 13 14 plt.imshow(y, cmap = "rainbow", vmin = 0, vmax = 255) 15 16 plt.show()
图 7 随机数组创建图像 学习代码如下: 1 arr = np.random.randint(0, 256, (100, 100, 3), dtype = np.uint8) 2 print(arr) 3 4 plt.imshow(arr, vmin = 0, vmax = 255) 5 6 arr = np.random.random((100,100)) * 255 7 8 arr = arr.astype(np.uint8) 9 10 plt.imshow(arr, vmin = 0, vmax = 255) 11 12 plt.show()
arr = np.pad(y, ((100, 100), (100, 100)),mode = 'constant', constant_values = 200) 对于y就是一个二维数组,第二个参数就是填充的大小,分别表示((上,下),(左,右)),第三个参数mode是指填充的方式,比较常见的见下面表1,对于mode为“constant”的时候,才会存在第四个参数,表示填充的值是哪个常数。 表 1 mode各指标作用
表 2 对应mode生成图像
1 y, x = np.mgrid[0:255:100j, 255:0:100j] 2 print(y) 3 4 arr = np.pad(y, ((100, 100), (100, 100)),mode = 'constant', constant_values = 200) 5 6 plt.imshow(arr, cmap = "gray", vmin = 0, vmax = 255) 7 8 plt.show() 9 10 arr = np.pad(y, ((100, 100), (100, 100)),mode = 'reflect') 11 12 plt.imshow(arr, cmap = "gray") 13 14 plt.show() 15 16 # arr = np.pad(y, ((100, 100), (100, 100)),mode = 'symmetric') 17 18 # plt.imshow(arr, cmap = "gray") 19 20 # plt.show() 21 22 arr = np.pad(y, ((100, 100), (100, 100)),mode = 'edge') 23 24 plt.imshow(arr, cmap = "gray") 25 26 plt.show() 27 28 # arr = np.pad(y, ((100, 100), (100, 100)),mode = 'edge') 29 30 # plt.imshow(arr, cmap = "gray") 31 32 # plt.show() 33 34 arr = np.pad(y, ((100, 100), (100, 100)),mode = 'linear_ramp') 35 36 plt.imshow(arr, cmap = "gray") 37 38 plt.show() 39 40 arr = np.pad(y, ((100, 100), (100, 100)),mode = 'minimum') 41 42 plt.imshow(arr, cmap = "gray") 43 44 plt.show() 45 46 arr = np.pad(y, ((100, 100), (100, 100)),mode = 'maximum') 47 48 plt.imshow(arr, cmap = "gray") 49 50 plt.show() 51 52 arr = np.pad(y, ((100, 100), (100, 100)),mode = 'median') 53 54 plt.imshow(arr, cmap = "gray") 55 56 plt.show() 57 58 arr = np.pad(y, ((100, 100), (100, 100)),mode = 'mean') 59 60 plt.imshow(arr, cmap = "gray") 61 62 plt.show() 63 64 arr = np.pad(y, ((100, 100), (100, 100)),mode = 'wrap') 65 66 plt.imshow(arr, cmap = "gray") 67 68 plt.show()
数组属性
表 3 数组参数
数组的索引与切片:
图 8 原图像以及裁切图
1 y, x = np.mgrid[0:255:100j, 255:0:100j] 2 print(type(y)) 3 4 plt.imshow(x, cmap = "autumn",vmin = 0, vmax = 255 ) 5 6 plt.show() 7 8 plt.imshow(x[:10, -10:], cmap = "autumn", vmin = 0, vmax = 255) 9 10 plt.show() 11 12 plt.imshow(x[-10:,:10], cmap = "autumn", vmin = 0, vmax = 255) 13 14 plt.show()
数组的运算:
图 9 原图像
图 10 gama矫正曲线(第一个图,r = 0.5,第二个图,r = 5)
图 11 原图像
图 12 本地图片gama矫正图像及其曲线(第一个图,r = 0.5,第二个图,r = 50)
1 def gama(arr,r,name = 'gray'): 2 3 # plt.axis(False) 4 5 maxvalue = 255 6 7 minvalue = 0 8 9 ar = (arr / maxvalue) ** r * maxvalue 10 11 print(ar) 12 13 plt.plot(ar[:,0]) 14 15 plt.show() 16 17 plt.imshow(ar.astype(np.uint8), cmap = name, vmin = minvalue, vmax = maxvalue) 18 19 plt.show() 20 21 def orginal(arr, name = 'gray'): 22 plt.axis(False) 23 24 plt.imshow(arr, cmap = name, vmin = 0, vmax = 255) 25 26 plt.show() 27 28 arr, x = np.mgrid[0:255:256j, 255:0:256j] 29 30 arr = arr.astype(np.uint8) 31 32 name = 'gray' 33 34 orginal(arr,name) 35 36 r = 0.5 37 38 gama(arr, r, name) 39 40 r = 5 41 42 gama(arr, r, name) 43 44 from PIL import Image 45 46 import matplotlib.pyplot as plt 47 48 img = Image.open('./images/花落夏海.jpg') 49 50 img.show() 51 52 arr = np.array(img) 53 54 arr 55 56 def gama(arr,r,name = 'gray'): 57 58 maxvalue = arr.max() 59 60 minvalue = 0 61 62 print(maxvalue) 63 64 ar = (arr / maxvalue) ** r * maxvalue 65 66 plt.plot(ar[:,0,:]) 67 68 ar1 = ar[:,0, :] 69 70 print(ar1.shape) 71 72 print(ar1) 73 74 print(ar.shape) 75 76 plt.show() 77 78 plt.axis(False) 79 80 plt.imshow(ar.astype(np.uint8), cmap = name, vmin = minvalue, vmax = maxvalue) 81 82 plt.show() 83 84 def orginal(arr, name = 'gray'): 85 86 plt.axis(False) 87 88 plt.imshow(arr, cmap = name, vmin = 0, vmax = 255) 89 90 plt.show() 91 92 name = 'gray' 93 94 arr = arr.astype(np.uint8) 95 96 orginal(arr, name) 97 98 gama(arr, 0.5, name) 99 100 gama(arr, 50, name)
1 numpy.vectorize(pyfunc, otypes=None, doc=None, excluded=None, cache=False, signature=None) 2 Parameters: 3 4 pyfunc :python函数或方法 5 6 otypes : 输出数据类型。必须将其指定为一个typecode字符串或一个数据类型说明符列表。每个输出应该有一个数据类型说明符。 7 8 doc : 函数的docstring。如果为None,则docstring将是 pyfunc.__doc__。 9 10 excluded : 表示函数不会向量化的位置或关键字参数的字符串或整数集。这些将直接传递给未经修改的pyfunc 11 12 cache :如果为True,则缓存第一个函数调用,该函数调用确定未提供otype的输出数。 13 14 signature : 广义通用函数签名,例如,(m,n),(n)->(m)用于矢量化矩阵 - 向量乘法。 15 16 如果提供的话,pyfunc将调用(并期望返回)具有由相应核心维度的大小给出的形状的数组。默认情况下,pyfunc假定将标量作为输入和输出。 17 18 Returns: vectorized :向量化的数组
1
图 13 点运算规则以及运算图像
1 def figure(arr, name): 2 plt.imshow(arr.astype(np.uint8), cmap = name, vmin = 0, vmax = 255) 3 4 plt.show() 5 6 def thesh(x, a = 100, b = 200): 7 8 if x > a and x < b: 9 10 return 0 11 12 return 255 13 14 arr = [np.arange(0, 256)] * 100 15 16 arr = np.array(arr) 17 18 print(arr) 19 20 vector = np.vectorize(thesh, otypes = (np.uint8,), excluded=['a', 'b']) 21 22 arr = vector(arr, 75, 100) 23 24 plt.plot(arr[0]) 25 26 plt.show() 27 28 figure(arr, 'autumn') 29 30 figure(arr, 'autumn_r') 31 32 figure(arr, 'rainbow') 33 34 figure(arr, 'rainbow_r') 35 36 figure(arr, 'gray') 37 38 figure(arr, 'gray_r') 39 40 import numpy as np 41 42 import matplotlib.pyplot as plt 43 44 def thesh(x, a = 100, b = 200, th = 150): 45 46 if x < a or x > b: 47 48 return x 49 50 return th 51 52 vector = np.vectorize(thesh, otypes=(np.uint8,), excluded=['a', 'b', 'th']) 53 54 arr = [np.arange(0, 256)] * 100 55 56 arr = np.array(arr) 57 58 arr = vector(arr, 50, 100, 255) 59 60 plt.plot(arr[0]) 61 62 plt.show() 63 64 figure(arr, 'rainbow')
图 14 两种转化方式的彩色图像和灰度图像
1 def figure(arr, name): 2 plt.imshow(arr.astype(np.uint8), cmap = name, vmin = 0, vmax = 255) 3 4 plt.show() 5 6 r = arr 7 8 g = 255 - arr 9 10 b = np.zeros_like(g) 11 12 # 彩色图转化为灰度图像 13 14 def thesh(r, g, b): 15 16 list1 = [] 17 18 list1.append(r) 19 20 list1.append(b) 21 22 list1.append(g) 23 24 arr = np.array(list1) 25 26 mn = arr.min() 27 28 mx = arr.max() 29 30 return mx 31 32 name = 'gray' 33 34 ar1 = np.dstack([r, g, b]) 35 36 figure(ar1, name) 37 38 vector = np.vectorize(thesh, otypes=(np.uint8,), excluded=['r', 'g', 'b']) 39 40 arr = vector(r, g, b) 41 42 name = 'gray' 43 44 figure(arr, name) 45 46 def rgbTransformGray(rgb): 47 48 mx = rgb.max() 49 50 mn = rgb.min() 51 52 return mx 53 54 name = 'gray' 55 56 ar1 = np.dstack([r, g, b]) 57 58 figure(ar1, name) 59 60 arr = np.apply_along_axis(rgbTransformGray,2,ar1) 61 62 figure(arr, name)
补充
1 import numpy as np 2 3 x = np.array([0,1,2,3,4,5,6,7,8]) 4 print (np.split(x,3)) 5 6 print (np.split(x,[3,5,6,9])) 7 8 print(np.split(x,[3,5,6,8])) 9 10 输出: 11 12 [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])] 13 14 [array([0, 1, 2]), array([3, 4]), array([5]), array([6, 7, 8]), array([], dtype=int32)] 15 16 [array([0, 1, 2]), array([3, 4]), array([5]), array([6, 7]), array([8])] 17 18 # 实验代码 19 20 r, g, b = np.split(arr,3, 2) 21 22 print(type(r)) 23 24 print(r.shape) 25 26 plt.imshow(r, cmap = "gray", vmin = 0, vmax = 255) 27 28 plt.show() 29 30 plt.imshow(r1, cmap = "gray", vmin = 0, vmax = 255) 31 32 plt.show() 33 34 arr = np.dstack([r, g, b]) 35 36 plt.imshow(arr, cmap = "gray", vmin = 0, vmax = 255) 37 38 plt.show()
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
结果分析 |
通过这次实验,我们学习了基础的图像方面有关点运算的操作,并且实操了gama矫正等点运算,联系到第二次实验,我们利用Image将自己本地的图片进行操作,同样进行了gama矫正,图像得到了增亮和压暗,同时我们也熟悉了图像处理邻域运算方面的知识,实现了对于改变某个区间的像素值的操作,同时也熟悉了对于彩色图像转化为灰度图像的过程,这些都增加了我们对图像处理的兴趣。 |

















































浙公网安备 33010602011771号