卷積神經網絡下的圖像處理

運用tensorflow內置卷積函數tf.nn.conv2d(input,filter,strides,padding,name=None)對圖像進行低卷積處理。
conv2d中的參數爲:
input:輸入的圖像(張量)大小爲[batch,in_height,in_width,in_channels]
filter:卷積核,大小爲[filter_height,filter_width,in_channels,out_channels]
strides:爲步長。大小爲:[1,stride_h,stride_w,1]
padding:表示對輸入圖像進行0填充。可選爲:SAME(此時輸入與輸出的大小完全相同),VALID:對輸入圖像不進行0填充。
實例代碼:

import tensorflow as tf
import numpy as np
import cv2
import matplotlib.pyplot as plt

img = cv2.imread("C:\\lena1.png")
plt.subplot(1,2,1)
plt.imshow(img)
img = np.array(img,dtype = "float32")
x_image = tf.reshape(img,[1,512,512,3])
filter =  tf.Variable(tf.ones([7,7,3,1]))

init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    res = tf.nn.conv2d(x_image,filter,[1,2,2,1],padding = "SAME")
    res_image = sess.run(tf.reshape(res,[256,256])) / 128 + 1
plt.subplot(1,2,2)
plt.imshow(res_image)
plt.show()
cv2.imshow("lena",res_image.astype("uint8"))
cv2.waitKey()

輸出顯示:
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章