Python opencv image mean filtering, Gaussian filtering and median filtering

First, the purpose of the experiment

Master opencv how mean filtering image, median filter and Gaussian filter.

Second, the experiment content

1. Title Description

Picture test.png mean filter image, Gaussian filtering and median filtering, edge detection as well as Gaussian, the following is test.png original image.

 

The following need to achieve results:

 

 

 

 

 

 

 

 

 

 

 

 

2. implementation process

Code sent by the teacher to analyze, and then analyzed on Baidu search, as follows:

Import CV2  
 Import numpy AS NP  
 from the PIL Import Image, ImageDraw, ImageFont 

__author__ = " sunjingjing " 
# mean filter 
DEF Blur (Source): 
    
    IMG = cv2.blur (Source, (10,10 )) 

    cv2img = cv2.cvtColor (IMG, cv2.COLOR_BGR2RGB) # CV2 and stored in hex code sequence PIL different colors 
    pilimg = Image.fromarray (cv2img) 

    Draw = ImageDraw.Draw (pilimg) # print image 
    font = ImageFont.truetype ( " simhei.ttf " , 20 is , encoding = " UTF-. 8" ) # Parameter 1: font file path, two parameters: the font size 
    draw.text ((0, 0), " mean filter " , (255, 0, 0), font = font) # Parameter 1: print coordinates, the parameters 2: text, 3 parameters: font color, four parameters: Font 

    # the PIL image transfer cv2 image 
    cv2charimg = cv2.cvtColor (np.array (pilimg), cv2.COLOR_RGB2BGR) 
    cv2.imshow ( " Blur " , cv2charimg)
 # value filtering 
DEF medianBlur (Source): 
    IMG = cv2.medianBlur (Source,. 3 ) 
    cv2img = cv2.cvtColor (IMG, cv2.COLOR_BGR2RGB) # CV2 and stored in hex code sequence PIL different colors 
    pilimg = Image.fromarray (cv2img) 

    drawImageDraw.Draw = (pilimg) # print pictures 
    font = ImageFont.truetype ( " simhei.ttf " , 20, encoding = " UTF-8 " ) # Parameter 1: path to the font file, parameters 2: Font Size 
    draw.text ( (0, 0), " median filtering " , (255, 0, 0), font = font) # parameter 1: print coordinates, parameter 2: text, parameter 3: font color, parameter 4: Font 
    # the PIL image transfer cv2 image 
    cv2charimg = cv2.cvtColor (np.array (pilimg), cv2.COLOR_RGB2BGR) 
    cv2.imshow ( " medianBlur " , cv2charimg)
 # block filter 
DEF BoxFilter (Source): 

    IMGCv2.boxFilter = (Source, -1, (5, 5), the normalize =. 1 ) 
    cv2img = cv2.cvtColor (IMG, cv2.COLOR_BGR2RGB) # storage order and cv2 PIL in hex code different colors 
    pilimg = Image.fromarray (cv2img) 

    Draw = ImageDraw.Draw (pilimg) # print pictures 
    font = ImageFont.truetype ( " simhei.ttf " , 20, encoding = " UTF-8 " ) # parameter 1: path to the font file, parameters 2: Font size 
    draw.text ((0, 0), " block filter " , (255, 0, 0), font = font) # parameter 1: print coordinates, parameter 2: text, 3 parameters: font color, four parameters: Font 
    # PIL pictures turn cv2 picture 
    cv2charimg =cv2.cvtColor (np.array (pilimg), cv2.COLOR_RGB2BGR) 
    cv2.imshow ( " boxFilter " , cv2charimg)
 # Gaussian 
DEF the Gaussian Blur (Source): 
    IMG = cv2.GaussianBlur (Source, (3,3 ), 0) 
    cv2img = cv2.cvtColor (IMG, cv2.COLOR_BGR2RGB) # CV2 and stored in hex code sequence PIL different colors 
    pilimg = Image.fromarray (cv2img) 

    Draw = ImageDraw.Draw (pilimg) # print image 
    font = ImageFont.truetype ( " simhei.ttf " , 20 is, encoding = " UTF-. 8 " ) # parameter 1: font file path, two parameters: the font size
    draw.text ((0, 0), " Gaussian " , (255, 0, 0), font = font) # Parameter 1: print coordinates, parameter 2: text, 3 parameters: font color, four parameters: Font 
    # image transfer images PIL cv2 
    cv2charimg = cv2.cvtColor (np.array (pilimg), cv2.COLOR_RGB2BGR) 
    cv2.imshow ( " the Gaussian Blur " , cv2charimg) 

# Gaussian edge detection 
DEF Gaussian (Source): 
    sobelX = cv2.Sobel (Source, cv2.CV_64F, 1,0) # gradient in the x direction 
    sobelY = cv2.Sobel (Source, cv2.CV_64F, 0,1) # gradient direction y 
    
    sobelX = np.uint8 (np.absolute (sobelX)) # x-direction the absolute value of the gradient
    = np.uint8 sobelY (np.absolute (sobelY)) # Y direction absolute value of the gradient 

    IMG = cv2.bitwise_or (sobelX, sobelY) #
     cv2img = cv2.cvtColor (IMG, cv2.COLOR_BGR2RGB) # CV2 in color and PIL different hex code sequence stored 
    pilimg = Image.fromarray (cv2img) 

    Draw = ImageDraw.Draw (pilimg) # print image 
    font = ImageFont.truetype ( " simhei.ttf " , 20 is, encoding = " UTF-. 8 " ) # parameter 1: font file path, two parameters: the font size 
    draw.text ((0, 0), " Gauss edge detection " , " Green " , font = font) #Parameter 1: print coordinates, parameter 2: text, 3 parameters: font color, four parameters: Font 

    # the PIL image transfer cv2 image 
    cv2charimg = cv2.cvtColor (np.array (pilimg), cv2.COLOR_RGB2BGR) 
    cv2.imshow ( " the Gaussian Blur " , cv2charimg) 

IF  __name__ == " __main__ " :
     # load picture 
    img = cv2.imread ( " test2.png " ) 
    cv2.namedWindow ( " the INPUT Image " , cv2.WINDOW_AUTOSIZE) 
    cv2.imshow ( " the INPUT Image " , img ) 
    Blur (img) 
    medianBlur (img) 
    the Gaussian Blur (img)
    # Gaussian(img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

3. Run results

 

 

 

 

 

 

 

 

 

4. Problems and Solutions

Problem : there is no way the Chinese written on the picture, there is a Chinese garbled on

Solution: search on Baidu, the Chinese discovered to write, to set the font and color coordinates kanji characters will stick in the picture. That is, the following lines of code

 

Third, test summary

Through this work, I found opencv really no xavis easy to use, xavis just a few lines of code, but in python or C language would need a lot of lines, but opencv is open source, more convenient. As well as network and promoting social progress, on the Internet I can find what they need, you can go to learn their own machine vision. In short a deeper understanding of machine vision.

Guess you like

Origin www.cnblogs.com/sunblingbling/p/12596051.html