Image processing with Python 100 knocks # 8 Max pooling

Introduction

Hi, I'm Ramu. This time, we will implement Max pooling, which uses the maximum value in the area as a representative value, among the pooling processes that divide the image into grids.

8th: Max pooling

Pooling, which was explained last time, is a process that divides an image into a fixed-length area and makes all the values in that area into a certain value. By applying this processing, the image becomes a mosaic. Max pooling fills the area with the maximum pixel value in the area. The only difference between average pooling and Max pooling is whether to use the average value or the maximum value.

Source code

maxPooling.py


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


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

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

  #Pooling process
  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.max(img[x-size:x+size,y-size:y+size,0])
      dst[x-size:x+size,y-size:y+size,1] = np.max(img[x-size:x+size,y-size:y+size,1])
      dst[x-size:x+size,y-size:y+size,2] = np.max(img[x-size:x+size,y-size:y+size,2])

  return dst


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

#Max pooling
#The second argument is the area length
img = maxPooling(img,40)

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

ファイル名 ファイル名 ファイル名

The left image is the input image, the center of the image is the previous average pooling, and the right image is the output image this time. You can see that the image looks like a mosaic. Also, the entire image is brighter than the average pooling because it uses the maximum brightness.

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 # 8 Max pooling
Image processing with Python 100 knocks # 7 Average 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-
Easy image processing in Python with Pillow
Light image processing with Python x OpenCV
Image processing with Python 100 knocks # 4 Binarization of Otsu (discriminant analysis method)
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
100 Language Processing with Python Knock 2015
"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)
Sorting image files with Python (3)
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
Image Processing Collection in Python
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
Real-time image processing basics with opencv
[Python] Using OpenCV with Python (Image Filtering)
100 Language Processing Knock with Python (Chapter 1)
[Python] Using OpenCV with Python (Image transformation)
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