当前位置:网站首页>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 plt

2. 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 :

 

 

原网站

版权声明
本文为[Bubble Yi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/208/202207270327317441.html