数学算法——Numpy在图像处理中的应用

import numpy as np

import matplotlib.pyplot as plt

from PIL import Image

import numpy as np

img = Image.open('car.jpg')

img = np.array(img)

plt.imshow(img)

plt.show()

def get_color_channels(img):

    img = img.copy()

    channels_num = len(img.shape)

    result = []

    

    channels = np.split(img, channels_num, axis=2) #沿着通道这个维度,将数组进行切分

    for i in channels:

        result.ap_pend(i.sum(axis=2)) #将切分之后的数组按照维数进行计算 分离出1通道的矩阵将他变成一维进行输出。

    return result

R, G, B, = get_color_channels(img)

print(R)

R.shape

L = R * 299 / 1000 + G * 587 / 1000 + B * 114 / 1000  

plt.imshow(L, cmap="gray")

plt.show()

temp = np.array([ 0.299,  0.587, 0.114])

plt.imshow(img@temp, cmap="gray")#@就是矩阵乘法

plt.show()

plt.imshow(L.T, cmap="gray")

B_img = img.copy()

B_img[:,:, [0,1]]=0

R_img = img.copy()

R_img[:,:, [0,2]]=0

G_img = img.copy()

G_img[:,:, [2,1]]=0

fig,ax = plt.subplots(2,2)

ax[0,0].imshow(img)

ax[1,1].imshow(R_img)

ax[1,0].imshow(G_img)

ax[0,1].imshow(B_img)

fig.set_size_inches(15, 15)

plt.tight_layout()

plt.show()

t1 = np.concatenate((img, img, img), axis=1) # 横向拼接

t2 = np.concatenate((t1, t1), axis=0)#纵向拼接

plt.imshow(t2)

plt.show()

mirrow_img_x = img[::-1]

plt.imshow(mirrow_img_x)

plt.show()

mirrow_img_y = img[:,::-1]

plt.imshow(mirrow_img_y)

plt.show()

plt.imshow(img.transpose(1,0,2))

plt.show()

plt.imshow(img.transpose(1,0,2)[::-1])

plt.show()

k = np.random.randint(0, 256, size=(200, 200, 3), dtype=np.uint8)

test = img.copy()

test[100:300,100:300] = k

plt.imshow(test)

plt.show()

t = img.copy()

height=t.shape[0]

li = np.split(t, range(100, height, 30), axis=0)

np.random.shuffle(li)

t = np.concatenate(li, axis=0)

plt.imshow(t)

plt.show()

t = img.copy()

plt.imshow(t[:,:,[2,0,1]])

plt.show()

test = img[:, :, [2,1,0]]

plt.imshow(test)

plt.show()

本文采集自智慧教育云平台
https://ms.jse.edu.cn/index.php?r=studio/post/view&sid=25&id=3240发布者:王璋

发表评论

评论已关闭。

相关文章

最热点击文章

    暂无文章