代码拉取完成,页面将自动刷新
# import tensorflow as tf
#
# sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
import tensorflow as tf
import numpy as np
import cv2
image = cv2.imread('./images/1.png') # 图像读取
size = 2
image = cv2.resize(image, (int(image.shape[1] / size), int(image.shape[0] / size)), interpolation=cv2.INTER_AREA)
gray = tf.cast(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY), dtype="float32") # 转灰度图并转换为tensor型
image_x = tf.reshape(gray, [1, 222, 395, 1]) # 变换维度(batch,height,weight,channel)
kernal = tf.constant([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]], dtype="float32") # sobel算子
input_kernal = tf.reshape(kernal, [3, 3, 1, 1]) # [weight, height, channel, out_channel]
conv = tf.nn.conv2d(image_x, input_kernal, strides=[1, 1, 1, 1], padding='VALID') # 卷积
with tf.Session() as sess: # 建图
y = sess.run(conv) # run
y_1 = sess.run(tf.transpose(y, [3, 0, 1, 2])) # 反卷积,即维度转置
cv2.imshow("sobel", y_1[0][0])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。