Image processing with Python 100 knocks # 7 Average pooling

Introduction

Hi, I'm Ramu. This time, even in the pooling process, which is a method of dividing the image into grids, we will implement the average pooling with the average value in the area as the representative value.

7th: Average pooling

Pooling is the process of dividing an image into a fixed-length area and making all the values in that area a certain value. By applying this processing, the image becomes a mosaic. In average pooling, the area is filled with the average pixel value in the area.

Average pooling is defined by the following formula: R is an area, for example, if it is an area of 8 x 8 pixels|R|=8×8=64Will be.

v = \frac{1}{|R|}\sum_{i \in R}v_i

Source code

avePooling.py


import numpy as np
import cv2
import matplotlib.pyplot as plt


def avePooling(img,k):
  dst = img.copy()

  w,h,c = img.shape
  #Length from the center pixel to both ends pixels
  size = k // 2

  for x in range(size, w, k):
    for y in range(size, h, k):
      dst[x-size:x+size,y-size:y+size,0] = np.mean(img[x-size:x+size,y-size:y+size,0])
      dst[x-size:x+size,y-size:y+size,1] = np.mean(img[x-size:x+size,y-size:y+size,1])
      dst[x-size:x+size,y-size:y+size,2] = np.mean(img[x-size:x+size,y-size:y+size,2])

  return dst


#Image reading
img = cv2.imread('image.jpg')

#Average pooling
#k is the area size
img = avePooling(img,40)

#Save image
cv2.imwrite('result.jpg', img)
#Image display
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()

ファイル名 ファイル名

The image on the left is the input image, and the image on the right is the output image. You can see that the image looks like a mosaic. It looks like sashimi, but it's sashimi.

ファイル名

The full version of Lena's image, which is often used in image processing, can also be posted on Qiita if average pooling processing is applied.

in conclusion

If you have any questions, please feel free to contact us. imori_imori's Github has the official answer, so please check that as well. .. Also, since python is a beginner, please kindly point out any mistakes.

Recommended Posts

Image processing with Python 100 knocks # 7 Average pooling
Image processing with Python 100 knocks # 8 Max pooling
Image processing with Python 100 knocks # 3 Binarization
Image processing with Python 100 knocks # 2 Grayscale
Image processing with Python
Image processing with Python 100 knocks # 9 Gaussian filter
Image processing with Python (Part 2)
Image processing with Python (Part 1)
Image processing with Python (Part 3)
[Python] Image processing with scikit-image
python image processing
Image processing 100 knocks ①
Basics of binarized image processing with Python
Image processing with Python 100 knock # 10 median filter
Image processing with Python & OpenCV [Tone Curve]
Image processing with Python 100 knock # 12 motion filter
Drawing with Matrix-Reinventor of Python Image Processing-
Light image processing with Python x OpenCV
Image processing with MyHDL
First Python image processing
Image Processing with PIL
Getting started with Python with 100 knocks on language processing
Image processing from scratch with python (5) Fourier transform
Image processing from scratch with python (4) Contour extraction
Image Processing with Python Environment Setup for Windows
Image processing by Python 100 knock # 11 smoothing filter (average filter)
100 Language Processing with Python Knock 2015
Image processing with PIL (Pillow)
"Apple processing" with OpenCV3 + Python3
Notes on HDR and RAW image processing with Python
Image editing with python OpenCV
Acoustic signal processing with Python (2)
100 image processing knocks !! (011 --020) Early game
Acoustic signal processing with Python
[Chapter 5] Introduction to Python with 100 knocks of language processing
Sorting image files with Python (2)
100 image processing knocks !! (001 --010) Carefully and carefully
Tweet with image in Python
Sorting image files with Python
[Chapter 3] Introduction to Python with 100 knocks of language processing
Image processing by python (Pillow)
[Chapter 2] Introduction to Python with 100 knocks of language processing
Connection pooling with Python + MySQL
[Chapter 4] Introduction to Python with 100 knocks of language processing
[Let's play with Python] Image processing to monochrome and dots
Cut out an image with python
Real-time image processing basics with opencv
[Python] Using OpenCV with Python (Image Filtering)
[Python] Easy parallel processing with Joblib
100 Language Processing Knock with Python (Chapter 1)
100 Language Processing Knock with Python (Chapter 3)
Personal notes for python image processing
Image processing 100 knocks Q9, Q10 (filter) speedup
Let's do image scraping with Python
Find image similarity with Python + OpenCV
Send image with python, save with php
Gradation image generation with Python [1] | np.linspace
HTML email with image to send with python
Image processing by Python 100 knock # 1 channel replacement
Create an image processing viewer with PySimpleGUI
Create a dummy image with Python + PIL.