Image processing with Python 100 knocks # 3 Binarization

Introduction

Hi, I'm Ramu. This also implements the familiar binarization in image processing.

Third: Binarization

Binarization is the process of converting an image into a monochrome image with only two colors, black and white. This is usually done for grayscale images. Also, when binarizing, a standard value called a threshold is determined. Pixels below the threshold are replaced with white, and pixels with pixel values above the threshold are replaced with black.

binarization.py


import numpy as np
import cv2
import matplotlib.pyplot as plt
plt.gray()


def binarization(img):
  #Image copy
  dst = img.copy()
  #Grayscale
  gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)

  #Threshold
  th = 128
  #Binarization
  idx = np.where(gray < th)
  gray[idx] = 0
  idx = np.where(gray >= th)
  gray[idx] = 255

  return gray


#Image reading
img = cv2.imread('../assets/imori.jpg')

#Binarization
mono = binarization(img)

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

ファイル名 ファイル名

The image on the left is the input image, and the image on the right is the output image. Like the grayscale image, there is no gray and it is only white and black.

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

Recommended Posts

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 # 8 Max pooling
Image processing with Python 100 knocks # 7 Average pooling
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
Image processing with Python 100 knocks # 4 Binarization of Otsu (discriminant analysis method)
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
Binarization with OpenCV / Python
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
Image processing with PIL (Pillow)
Acoustic signal processing with Python (2)
100 image processing knocks !! (011 --020) Early game
Acoustic signal processing with Python
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
Image processing by python (Pillow)
Image Processing Collection in Python
Notes on HDR and RAW image processing with Python
[Chapter 5] Introduction to Python with 100 knocks of language processing
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Chapter 4] Introduction to Python with 100 knocks of language processing
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)
[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
[Let's play with Python] Image processing to monochrome and dots
Find image similarity with Python + OpenCV
Send image with python, save with php
Gradation image generation with Python [1] | np.linspace
Create an image processing viewer with PySimpleGUI
Create a dummy image with Python + PIL.
Introduction to Python Image Inflating Image inflating with ImageDataGenerator