当前位置:网站首页>卷积神经网络——灰度图像的卷积
卷积神经网络——灰度图像的卷积
2022-07-27 03:27:00 【泡泡怡】
一、前提介绍:
卷积在图像处理中经常被用于平滑、模糊、锐化、去噪、边缘取等工作中。图像处理中的卷积操作,其实就是利用卷积核(模板)在目标图像上滑动,将图像上的像素点依次对应到卷积核的中间像素处,每个像素点对齐后将图像上的像素灰度值与卷积核对应位置上的数值相乘,然后将相乘后的所有值相加,相加的结果作为当前像素的灰度值,并最终滑动完所有图像像素点的过程。

如上图所示:最左侧矩阵是一个灰度图像,中间是一个3*3的小矩阵,称为“卷积核”或“过滤器”。
1.导包
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt2. 过程:
#图像的卷积运算
#输入参数
# image:原图像
# mfilter:滤波器
#输出参数
# convImage:卷积后的图像
3.定义卷积函数:
def ImageConvolution( image, mfilter ):
mI, nI = np.shape( image )
[mF, nF] = np.shape( mfilter )
halfHeight = int( mF / 2 )
halfWidht = int( nF / 2 )
convImage = np.zeros( (mI, nI) )#卷积图像
#根据滤波器长度奇偶性的不同调整扩充图像的范围
if mF % 2 == 0:
imData = np.pad( image, (halfWidht, halfWidht-1), 'constant' )
else:
imData = np.pad( image, (halfWidht, halfHeight), 'constant' )
padmI, padnI = imData.shape
convHeight = padmI - mF + 1
convWidth = padnI - nF + 1
#依次截取与滤波器大小相同的图像块进行内积运算
for i in range( convHeight ):
for j in range( convWidth ):
localImage = imData[ i:i+mF, j:j+nF ]
convImage[i][j] = np.sum( localImage * mfilter )
convImage1 = convImage.clip( 0, 255 )
return convImage1
4.根据滤波器对图像进行卷积
filter1 = [ [-1, -2, -1], [0, 0, 0], [1, 2, 1] ]
imageConv = ImageConvolution( img, filter1 )
plt.figure( 'filter1' )
plt.imshow( imageConv, cmap = 'gray' )
plt.axis( 'off' )
filter2 = [ [1, -1], [1, -1] ]
imageConv = ImageConvolution( img, filter2 )
plt.figure( 'filter2' )
plt.imshow( imageConv, cmap = 'gray' )
plt.axis( 'off' )
filter3 = [ [-1, 1, 1, -1], [-1, 1, 1, -1], [-1, 1, 1, -1], [-1, 1, 1, -1] ]
imageConv = ImageConvolution( img, filter3 )
plt.figure( 'filter3' )
plt.imshow( imageConv, cmap = 'gray' )
plt.axis( 'off' )
if __name__ == '__main__':
main()结果:




边栏推荐
- C how to set different text watermarks for each page of word
- Which securities company has the lowest handling charge? Is it safe to open an account on your mobile phone
- Parallel desktop startup virtual machine "operation failed" problem solution
- Eureka-服务注册中心
- xxx is not in the sudoers file.This incident will be reported.的解决方法
- 在Golang结构体中使用tag标签
- 288 page 180000 word intelligent campus overall design directory
- Principle of bean validation --07
- 技术分享 | 需要小心配置的 gtid_mode
- js实现页面跳转与参数获取加载
猜你喜欢

Golang发送邮件库email

Want to get the Apache official domain name mailbox? Exclusive interview with Apache linkis five new committers to tell you how to do it

centos如何安装mysqldump

356页14万字高端商业办公综合楼弱电智能化系统2022版

11.zuul路由网关

【比赛参考】PyTorch常用代码段以及操作合集

Five basic data structures of redis

三种常见的移动底盘运动学模型分析

微服务化解决文库下载业务问题实践

PSINS工具箱中轨迹生成工具详细解析
随机推荐
什么是动画效果?什么是过渡效果?
JVM原理简介
Is VR panoramic production a single weapon in the home decoration industry? Why is this?
centos如何安装mysqldump
How CentOS installs mysqldump
Article main content extraction software [based on NLP technology]
微服务化解决文库下载业务问题实践
php+swoole
leetcode每日一练:将句子排序
Daily question 1: delete continuous nodes with a total value of zero from the linked list
二叉树的坡度
Which securities company has the lowest handling charge? Is it safe to open an account on your mobile phone
利用LCD1602显示超声波测距
Ant JD Sina 10 architects 424 page masterpiece in-depth distributed cache from principle to practice pdf
An online duplicate of a hidden bug
Apachecon Asia preheating live broadcast incubator theme full review
每日一题:从链表中删去总和值为零的连续节点
Rust:axum学习笔记(1) hello world
BigDecimal踩坑总结&最佳实践
整理字符串