当前位置:网站首页>Convolution neural network -- convolution of gray image
Convolution neural network -- convolution of gray image
2022-07-27 04:22:00 【Bubble Yi】
One 、 Premise introduction :
Convolution is often used for smoothing in image processing 、 Fuzzy 、 sharpening 、 Denoise 、 Edge extraction and other work . Convolution operation in image processing , It's actually using convolution kernel ( Templates ) Slide on the target image , Map the pixels on the image to the middle pixels of the convolution kernel in turn , After each pixel is aligned, multiply the pixel gray value on the image by the value on the corresponding position of the convolution kernel , Then add all the multiplied values , The result of the addition is the gray value of the current pixel , And finally slide all image pixels .

As shown in the figure above : The leftmost matrix is a grayscale image , In the middle is a 3*3 The small matrix of , be called “ Convolution kernel ” or “ filter ”.
1. Guide pack
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt2. The process :
# Convolution operation of image
# Input parameters
# image: Original image
# mfilter: filter
# Output parameters
# convImage: Convoluted image
3. Define the convolution function :
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) )# Convolution image
# Adjust and expand the range of the image according to the different parity of the filter length
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
# Successively intercept image blocks with the same size as the filter for inner product operation
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. Convolute the image according to the filter
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()result :




边栏推荐
猜你喜欢

Rust:axum学习笔记(1) hello world

H. 265 web player easyplayer's method of opening video to the public

Ribbon-负载均衡原理及部分源码

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

Remember the major performance problems caused by a TCP packet loss

2022年危险化学品经营单位主要负责人复训题库及答案

一张图看懂KingbaseES V9

List Simulation Implementation

Ant JD Sina 10 architects 424 page masterpiece in-depth distributed cache from principle to practice pdf

JMeter学习笔记004-CSV文件行数控制循环次数
随机推荐
HEAD detached from origin/...导致push失败
C get UUID
Which securities company has the lowest handling charge? Is it safe to open an account on your mobile phone
Want to get the Apache official domain name mailbox? Exclusive interview with Apache linkis five new committers to tell you how to do it
A. Round Down the Price
Knowledge atlas: knowledge representation
JVM原理简介
Big talk · book sharing | lean product development: principles, methods and Implementation
Okaleido生态核心权益OKA,尽在聚变Mining模式
leetcode每日一练:将句子排序
Nacos启动与登录
从根到叶的二进制数之和
List Simulation Implementation
卷积神经网络——24位彩色图像的卷积的详细介绍
深度剖析 —— 动态内存管理
BigDecimal pit summary & Best Practices
Leetcode:433. minimal genetic change
JVM调优中的一些常见指令
【软件工程期末复习】知识点+大题详解(E-R图、数据流图、N-S盒图、状态图、活动图、用例图....)
数据库泰斗王珊:努力创新,精心打磨优质的数据库产品