java 提取轮廓_TensorFlow 卷积操作模拟sobel算子提取图像轮廓
注意:
sobel算子不保證結果在0-255,需要做一次歸一化處理,再乘以255
輸出的數據shape與圖像的shape不一樣,需要一次轉化
效果
代碼
import matplotlib.pyplot as plt # plt 用于顯示圖片
import matplotlib.image as mpimg # mpimg 用于讀取圖片
import numpy as np
import tensorflow as tf
myimg = mpimg.imread('test.jpg') # 讀取和代碼處于同一目錄下的圖片
plt.imshow(myimg) # 顯示圖片
plt.axis('off') # 不顯示坐標軸
plt.show()
print(myimg.shape)
full = np.reshape(myimg, [1, *myimg.shape])
inputfull = tf.Variable(tf.constant(1.0, shape=[1, *myimg.shape]))
filter = tf.Variable(tf.constant([[-1.0, -1.0, -1.0], [0, 0, 0], [1.0, 1.0, 1.0],
[-2.0, -2.0, -2.0], [0, 0, 0], [2.0, 2.0, 2.0],
[-1.0, -1.0, -1.0], [0, 0, 0], [1.0, 1.0, 1.0]],
shape=[3, 3, 3, 1]))
op = tf.nn.conv2d(inputfull, filter, strides=[1, 1, 1, 1], padding='SAME') # 3個通道輸入,生成1個feature ma
o = tf.cast(((op - tf.reduce_min(op)) / (tf.reduce_max(op) - tf.reduce_min(op))) * 255, tf.uint8)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
t, f = sess.run([o, filter], feed_dict={inputfull: full})
print(t.shape) # (1, 512, 512, 1)
t = np.reshape(t, myimg.shape[:2])
print(t.shape) # (512, 512)
plt.imshow(t, cmap='Greys_r') # 顯示圖片
plt.axis('off') # 不顯示坐標軸
plt.show()
轉載至鏈接:https://my.oschina.net/ahaoboy/blog/1923134
總結
以上是生活随笔為你收集整理的java 提取轮廓_TensorFlow 卷积操作模拟sobel算子提取图像轮廓的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 智能机器人建房子后房价走势_深圳建二手房
- 下一篇: java xml注入bean_Sprin