Pytorch 结合Sobel滤波的卷积操作来提取图像的轮廓图!
在pytorch中实现将sobel算子和卷积层结合来提取图像中物体的边缘轮廓图,如下代码是卷积执行soble边缘检测算子的实现:
import torch
import numpy as np
from torch import nn
from PIL import Image
from torch.autograd import Variable
import torch.nn.functional as F
import cv2def edge_conv2d(im):# 用nn.Conv2d定义卷积操作conv_op = nn.Conv2d(3, 3, kernel_size=3, padding=1, bias=False)# 定义sobel算子参数,所有值除以3个人觉得出来的图更好些sobel_kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]], dtype='float32') / 3# 将sobel算子转换为适配卷积操作的卷积核sobel_kernel = sobel_kernel.reshape((1, 1, 3, 3))# 卷积输出通道,这里我设置为3sobel_kernel = np.repeat(sobel_kernel, 3, axis=1)# 输入图的通道,这里我设置为3sobel_kernel = np.repeat(sobel_kernel, 3, axis=0)conv_op.weight.data = torch.from_numpy(sobel_kernel)# print(conv_op.weight.size())# print(conv_op, '\n')edge_detect = conv_op(im)print(torch.max(edge_detect))# 将输出转换为图片格式edge_detect = edge_detect.squeeze().detach().numpy()return edge_detectdef edge_extraction():im = cv2.imread('./cat.jpg', flags=1)im = np.transpose(im, (2, 0, 1))# 添加一个维度,对应于pytorch模型张量(B, N, H, W)中的batch_sizeim = im[np.newaxis, :]im = torch.Tensor(im)edge_detect = edge_conv2d(im)edge_detect = np.transpose(edge_detect, (1, 2, 0))# cv2.imshow('edge.jpg', edge_detect)# cv2.waitKey(0)cv2.imwrite('edge-2.jpg', edge_detect)if __name__ == "__main__":edge_extraction()
结果如下图所示:
Pytorch 结合Sobel滤波的卷积操作来提取图像的轮廓图!相关推荐
- 【论文分享】联合形态学滤波和卷积稀疏编码的图像去雨
[论文分享]联合形态学滤波和卷积稀疏编码的图像去雨
- 【深度学习入门-1】透彻理解卷积的三层含义:从“卷积”、到“图像卷积操作”、再到“卷积神经网络”的含义(学习笔记)
一.写在前面 笔者在进行卷积神经网络入门的时候花了很多功夫,理解的也不够透彻,基础不牢,地动山摇.在查阅了很多资料后,发现了大佬up"王木头学科学"讲的卷积神经网络的理解,茅塞顿开 ...
- 一文详细介绍深度学习的各种卷积操作
关注上方"深度学习技术前沿",选择"星标公众号", 资源干货,第一时间送达! 转自: 机器之心 我们都知道卷积的重要性,但你知道深度学习领域的卷积究竟是什么,又 ...
- 图卷积神经网络1-谱域图卷积:拉普拉斯变换到谱域图卷积
知乎主页 备注:本篇博客摘自某培训机构上的图神经网络讲解的视频内容,该视频关于图神经网络入门讲解.经典算法的引入和优缺点的介绍比较详细,逻辑主线也比较清晰.因此记录分享下. 目录: 图卷积近年发展 图 ...
- 【Pytorch神经网络实战案例】09 使用卷积提取图片的轮廓信息(手动模拟Sobel算子)
1 载入图片并显示 import matplotlib.pyplot as plt import matplotlib.image as mpimg import torch import torch ...
- pytorch中的卷积操作详解
首先说下pytorch中的Tensor通道排列顺序是:[batch, channel, height, width] 我们常用的卷积(Conv2d)在pytorch中对应的函数是: torch.nn. ...
- pytorch卷积操作nn.Conv中的groups参数用法解释
MobileNetV1<MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications> ...
- OpenCV的滤波与卷积
目录 预备知识 滤波.核和卷积 边界外推和边界处理 阈值化操作 Otsu算法 自适应阈值 平滑 简单模糊和方框型滤波器 中值滤波器 高斯滤波器 双边滤波器 导数和梯度 索贝尔导数 Scharr滤波器 ...
- 《学习OpenCV3》第10章 滤波与卷积
<学习OpenCV3>第10章 滤波与卷积 1. 预备知识 1.1 滤波.核和卷积 1.2 边界外推和边界处理 1.2.1 自定义边框 1.2.2 自定义外推 2. 阈值化操作 2.1 t ...
最新文章
- mysql master-slave_mysql 同步 master-slave
- Oracle学习:序列
- 【Floyed】小萨的烦恼(ssl 1624)
- 搭建GitLab+Jenkins持续集成环境图文教程
- 入门机器学习(十七)--编程作业-K-means和PCA(Python实现)
- 4 linux编辑器
- 小米11真机现身:稳了!
- 十大排序算法(动画图解)
- TP-LINK无线网卡免驱版 安装使用问题整理
- java digester_Apache Commons Digester 一 (基础内容、核心API)
- 标题栏设置及自定义标题栏
- 一个DDD指导下的实体类设计案例
- 宝健中国用产品缔造口碑,深受消费者信赖
- Windows和Ubuntu搭建局域网中共享文件夹
- oracle 临时表空间语句,oracle的临时表空间
- VC加载jpeg, png图片的方法
- Hackthebox Three
- 微粒化运营:升级内容产业消费体验(附视频版)
- 2016年Visual C++推荐图书125本
- 运用黑白调整层抠图技法(勾头发)