卷积实现+对一幅图片的操作 ==sobel算子

import matplotlib.pyplot as plt
import matplotlib.image as mping
import numpy as np
import tensorflow as tf
mying=mping.imread('img.jpg')
plt.imshow(mying)
plt.axis('off')
plt.show()
print(mying.shape)
full=np.reshape(mying,[1,768,1024,3])
inputfull=tf.Variable(tf.constant(1.0,shape=[1,768,1024,3]))
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')
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})
    t=np.reshape(t,[768,1024])
    plt.imshow(t,cmap='Greys_r')
    plt.axis('off')
    plt.show()

原图:

卷积之后:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一份简单的 MATLAB 代码实现,用于读取一幅图像并使用梯度算子、Roberts算子Sobel算子、Prewitt算子进行边缘特征提取: ```matlab % 读取图像 img = imread('lena.png'); % 显示原图像 subplot(2, 3, 1); imshow(img); title('Original Image'); % 使用梯度算子提取边缘特征 % 定义梯度算子 grad_op = [-1, 0, 1; -2, 0, 2; -1, 0, 1]; % 对图像进行卷积操作 grad_img = conv2(img, grad_op, 'same'); % 显示边缘特征 subplot(2, 3, 2); imshow(grad_img, []); title('Gradient Operator'); % 使用Roberts算子提取边缘特征 % 定义Roberts算子 roberts_op1 = [1, 0; 0, -1]; roberts_op2 = [0, 1; -1, 0]; % 对图像进行卷积操作 roberts_img1 = conv2(img, roberts_op1, 'same'); roberts_img2 = conv2(img, roberts_op2, 'same'); roberts_img = sqrt(roberts_img1.^2 + roberts_img2.^2); % 显示边缘特征 subplot(2, 3, 3); imshow(roberts_img, []); title('Roberts Operator'); % 使用Sobel算子提取边缘特征 % 定义Sobel算子 sobel_op1 = [-1, 0, 1; -2, 0, 2; -1, 0, 1]; sobel_op2 = [-1, -2, -1; 0, 0, 0; 1, 2, 1]; % 对图像进行卷积操作 sobel_img1 = conv2(img, sobel_op1, 'same'); sobel_img2 = conv2(img, sobel_op2, 'same'); sobel_img = sqrt(sobel_img1.^2 + sobel_img2.^2); % 显示边缘特征 subplot(2, 3, 4); imshow(sobel_img, []); title('Sobel Operator'); % 使用Prewitt算子提取边缘特征 % 定义Prewitt算子 prewitt_op1 = [-1, 0, 1; -1, 0, 1; -1, 0, 1]; prewitt_op2 = [-1, -1, -1; 0, 0, 0; 1, 1, 1]; % 对图像进行卷积操作 prewitt_img1 = conv2(img, prewitt_op1, 'same'); prewitt_img2 = conv2(img, prewitt_op2, 'same'); prewitt_img = sqrt(prewitt_img1.^2 + prewitt_img2.^2); % 显示边缘特征 subplot(2, 3, 5); imshow(prewitt_img, []); title('Prewitt Operator'); ``` 其中,`lena.png` 是一张经典的测试图像,可以在网络上下载。运行代码后,将会显示原图像以及使用梯度算子、Roberts算子Sobel算子、Prewitt算子提取的边缘特征图像。你可以根据需要对代码进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值