当前位置:网站首页>Convolution neural network -- a detailed introduction to convolution of 24 bit color images
Convolution neural network -- a detailed introduction to convolution of 24 bit color images
2022-07-27 04:22:00 【Bubble Yi】
Convolution is used in image processing :
Convolution is continuous in function , It can be expressed by integral , In fact, when I first knew integral , We know that the integral is obtained by summing discrete data , This also determines that image processing can also be applied to the principle of convolution . In computer , Image is actually a m*n Matrix ( Color channels are not discussed here ), So for pixels , We can use the principle of convolution , Use another matrix , Remove the low-order features of the image , Preserve and highlight the high-order features of the image , Then according to the subsequent operations , Classify or recognize images .
In the convolution of continuous functions , It uses movable and f(x) Do integral , In the discrete image matrix , Will adopt a “ Convolution kernel ” A special matrix of , Its function is to replace , In the process of translation, multiply and accumulate with the image matrix , So as to achieve the effect of integration in convolution .
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 .



One 、24 Convolution of bit color images
1. Guide pack
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt2. Symbol description :
Input parameters :
im : Single channel image mfilter: filter fm: Number of filter rows fn: Number of filter columns hfm: Filter row radius hfn: Filter column number radius Output parameters :convIm: Single channel convolution result
3. Define the function of convolution operation :
def convolution( im, mfilter, fm, fn, hfm, hfn ):
mI, nI = im.shape
imp = np.pad( im, (hfm, hfn ), 'constant' )
padmI, padnI = imp.shape
convHeight = padmI - fm + 1
convWidth = padnI - fn + 1
convIm = np.zeros( (mI, nI), dtype=np.float )
for i in range( convHeight ):
for j in range( convWidth ):
locIm = imp[ i:i+fm, j:j+fn ]
convIm[i][j] = np.sum( locIm * mfilter )
convIm = convIm.clip( 0, 255 )
convIm = np.rint(convIm).astype('uint8')
return convIm4. Yes rgb Each channel of the three channel picture calls the convolution operation function for convolution , Output the convoluted picture .
def ImageConvolution( image, mfilter ):
mI, nI, cI = np.shape( image )
print( 'Image size:', mI, nI, cI )
[fm, fn] = np.shape( mfilter )
print( 'fileter size:', fm, fn )
hfm = int( fm / 2 )
hfn = int( fn / 2 )
# Adjust and expand the range of the image according to the different parity of the filter length
if fn % 2 == 0:
hfn -= 1
imR = image[ :, :, 0 ]
imG = image[ :, :, 1 ]
imB = image[ :, :, 2 ]
# Successively intercept image blocks with the same size as the filter for inner product operation
convImageR = convolution( imR, mfilter, fm, fn, hfm, hfn )
convImageG = convolution( imG, mfilter, fm, fn, hfm, hfn )
convImageB = convolution( imB, mfilter, fm, fn, hfm, hfn )
convImage = np.stack( ( convImageR, convImageG, convImageB ), 2 )
return convImage
5. The main function is to input the image to be processed , Then call the second function just now to convolute the image , Finally, visualize the image after convolution
def main():
img = np.array( Image.open("C:/Users/bwy/Desktop/ Summer vacation /ImageConvolution_py/1.jpg", 'r') )
plt.figure( 'Image' )
plt.imshow( img )
#plt.axis( 'off' )
# 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 )
plt.axis( 'off' )
filter2 = np.ones( (2, 2) )
filter2 /= filter2.sum()
imageConv = ImageConvolution( img, filter2 )
plt.figure( 'filter2x2' )
plt.imshow( imageConv )
plt.axis( 'off' )
filter2 = np.ones( (3, 3) )
filter2 /= filter2.sum()
imageConv = ImageConvolution( img, filter2 )
plt.figure( 'filter3x3' )
plt.imshow( imageConv )
plt.axis( 'off' )
filter2 = np.ones( (4, 4) )
filter2 /= filter2.sum()
imageConv = ImageConvolution( img, filter2 )
plt.figure( 'filter4x4' )
plt.imshow( imageConv )
plt.axis( 'off' )
if __name__ == '__main__':
main()result : On the left ( Original image ):






The complete code is as follows :
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
# Convolution
# Input parameters :
# im : Single channel image
# mfilter: filter
# fm: Number of filter rows
# fn: Number of filter columns
# hfm: Filter row radius
# hfn: Filter column number radius
# Output parameters :
# convIm: Single channel convolution result
def convolution( im, mfilter, fm, fn, hfm, hfn ):
mI, nI = im.shape
imp = np.pad( im, (hfm, hfn ), 'constant' )
padmI, padnI = imp.shape
convHeight = padmI - fm + 1
convWidth = padnI - fn + 1
convIm = np.zeros( (mI, nI), dtype=np.float )
for i in range( convHeight ):
for j in range( convWidth ):
locIm = imp[ i:i+fm, j:j+fn ]
convIm[i][j] = np.sum( locIm * mfilter )
convIm = convIm.clip( 0, 255 )
convIm = np.rint(convIm).astype('uint8')
return convIm
# Convolution operation of image
# Input parameters
# image: Original image
# mfilter: filter
# Output parameters
# convImage: Convoluted image
def ImageConvolution( image, mfilter ):
mI, nI, cI = np.shape( image )
print( 'Image size:', mI, nI, cI )
[fm, fn] = np.shape( mfilter )
print( 'fileter size:', fm, fn )
hfm = int( fm / 2 )
hfn = int( fn / 2 )
# Adjust and expand the range of the image according to the different parity of the filter length
if fn % 2 == 0:
hfn -= 1
imR = image[ :, :, 0 ]
imG = image[ :, :, 1 ]
imB = image[ :, :, 2 ]
# Successively intercept image blocks with the same size as the filter for inner product operation
convImageR = convolution( imR, mfilter, fm, fn, hfm, hfn )
convImageG = convolution( imG, mfilter, fm, fn, hfm, hfn )
convImageB = convolution( imB, mfilter, fm, fn, hfm, hfn )
convImage = np.stack( ( convImageR, convImageG, convImageB ), 2 )
return convImage
def main():
img = np.array( Image.open("C:/Users/bwy/Desktop/ Summer vacation /ImageConvolution_py/1.jpg", 'r') )
plt.figure( 'Image' )
plt.imshow( img )
#plt.axis( 'off' )
# 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 )
plt.axis( 'off' )
filter2 = np.ones( (2, 2) )
filter2 /= filter2.sum()
imageConv = ImageConvolution( img, filter2 )
plt.figure( 'filter2x2' )
plt.imshow( imageConv )
plt.axis( 'off' )
filter2 = np.ones( (3, 3) )
filter2 /= filter2.sum()
imageConv = ImageConvolution( img, filter2 )
plt.figure( 'filter3x3' )
plt.imshow( imageConv )
plt.axis( 'off' )
filter2 = np.ones( (4, 4) )
filter2 /= filter2.sum()
imageConv = ImageConvolution( img, filter2 )
plt.figure( 'filter4x4' )
plt.imshow( imageConv )
plt.axis( 'off' )
if __name__ == '__main__':
main()The blogger received a tree “ Xingyun ”, Everyone who reads this article will also be super lucky , Good luck !!!
边栏推荐
- Leetcode daily exercise: sort sentences
- 356 pages, 140000 words, weak current intelligent system of high-end commercial office complex, 2022 Edition
- ISG指数显示,亚太区IT和商业服务市场在第二季度出现大幅下滑
- Brightcove appoints Dan Freund as chief revenue Officer
- 技术分享 | 需要小心配置的 gtid_mode
- 二叉树的坡度
- leetcode每日一练:将句子排序
- Session&Cookie&token
- Golang JWT cross domain authentication
- spicy之evt接口定义文件
猜你喜欢

11. Zuul routing gateway

Elastic开源社区:开发者招募

Framework learning journey: init process startup process
![[Code] sword finger offer 04 search in two-dimensional array](/img/7d/a6693bfd24af9d9587539dda458d27.png)
[Code] sword finger offer 04 search in two-dimensional array

数据分析师岗位分析

大咖说·图书分享|精益产品开发:原则、方法与实施

网工知识角|只需四个步骤,教会你使用SecureCRT连接到eNSP,常用工具操作指南必看

scala 不可变Map 、 可变Map 、Map转换为其他数据类型

VR panorama gold rush "careful machine" (Part 1)

People don't talk much, engineers don't talk much
随机推荐
Apachecon Asia preheating live broadcast incubator theme full review
微信小程序轮播图
e.target与e.currentTarget的区别
452 pages, 130000 words, the overall solution of modern smart Township Xueliang project 2022 Edition
leetcode每日一练:将句子排序
2022危险化学品生产单位安全生产管理人员考试题模拟考试题库模拟考试平台操作
JMeter download and installation
链表内指定区间反转
centos如何安装mysqldump
F - Pre-order and In-order(Atcoder 255)
Elastic认证考试:30天必过速通学习指南
Practice of microservice in solving Library Download business problems
一张图看懂KingbaseES V9
Maximum nesting depth of parentheses
人很话不多,工程师不耍嘴皮子
Internet of things smart home project - Smart bedroom
Interview question 02.05. sum of linked list
Redis面试题(2022)
Learning route from junior programmer to architect + complete version of supporting learning resources
Okaleido生态核心权益OKA,尽在聚变Mining模式