Grayscale by matrix-Reinventor of Python image processing-

A story about image processing and grayscale conversion only by matrix calculation without relying on the image processing library. Also possible with Pythonista

** Click here for basics **

What is a "reinventor"?

Instead of relying on Open CV or Pillow, I will actually write various image processing using numpy and matplotlib. It's a combination that can also be used with the iOS app Pythonista.

import numpy as np
import matplotlib.pyplot as plt

In addition, the following functions are convenient for displaying images. (For details, Basics)

def img_show(img : np.ndarray, cmap = 'gray', vmin = 0, vmax = 255, interpolation = 'none') -> None:
    '''np.Display an image with array as an argument.'''
    
    #Set dtype to uint8
    img = np.clip(img,,vmin,vmax).astype(np.uint8)
    
    #Display image
    plt.imshow(img, cmap = cmap, vmin = vmin, vmax = vmax, interpolation = interpolation)
    plt.show()
    plt.close()

Grayscale

Grayscale is a method of calculating the black and white value Y from the RGB values assigned to each pixel. Here, various grayscale methods that were not dealt with in Basics ) Also try. See the link for a detailed explanation. They are treated in the same order.

The image used is'tiger.jpeg' tiger.jpeg

img = plt.imread('tiger.jpeg')
R, G, B = img[...,0], img[...,1], img[...,2]

A function for comparing and arranging color and black and white is defined here.

def align_show(img_gray):
    #img_gray to N*M*Convert to 3
    img_pseudogray = np.einsum('ij,k->ijk',img_gray,[1,1,1])

    #Display side by side
    img_show(np.concatenate((img,img_pseudogray), axis = 1))

Median method

$ \ rm Y = \ frac {\ max (R, G, B) + \ min (R, G, B)} {2} $. It is a method. In short, the average of the maximum and minimum values. In the actual calculation, the order of calculation is partially changed from the above formula to avoid overflow.

img_mid_v = np.max(img, axis = 2)/2 +np.min(img, axis = 2)/2
img_show(img,img_mid_v)

mid_v.png

There is no problem at first glance, but the button (?) In the middle of the maze is hard to see.

Weighted average method by NTSC coefficient

$ \ rm Y = (0.298912 R + 0.586611 G + 0.114478 B) $. These coefficients are the result of taking into account the effect of each RGB on the human eye (psychological weighting).

img_ntsc = (0.298912 * R + 0.586611 * G + 0.114478 * B)
align_show(img_ntsc)

ntsc.png

Goodness

Weighted average and correction by HDTV coefficient

$ \ rm Y = ((0.222015 R) ^ X + (0.706655 G) ^ X + (0.071330 B) ^ X) ^ {1 / X} $. This also incorporates psychological weighting.

X = 2.2
img_hdtv = ((0.222015*R)**X + (0.706655*G)**X + (0.071330*B)**X)**(1/X)
align_show(img_hdtv)

hdtv.png

It's so different that I can't tell the difference from the NTSC method.

Simple averaging method

This is a method of averaging $ \ rm Y = \ frac {R + G + B} {3} $. Probably the most intuitive way. It may be said that it is the NTSC method before taking the weighted average.

img_mean = np.mean(img)
align_show(img_mean)

mean.png

G channel method

How to extract only the G channel $ \ rm Y = G $. It seems to be the fastest.

img_g_channel = G
align_show(img_g_channel)

g_channel.png

I wonder if the red part is a little too dark ...

Median method

How to retrieve the median $ \ rm Y = median (R, G, B) $. I feel that the idea is similar to the median method.

img_median = np.median(img, axis = 2)
align_show(img_median)

median.png

As with the median method, the green is too dark.

Summary

Median NTSC HDTV
The original image
Simple average G channel Median

sumup.png

Recommended Posts

Grayscale by matrix-Reinventor of Python image processing-
Drawing with Matrix-Reinventor of Python Image Processing-
Image processing by python (Pillow)
Image processing by matrix Basics & Table of Contents-Reinventor of Python image processing-
Image processing with Python 100 knocks # 2 Grayscale
python image processing
Basics of binarized image processing with Python
Image processing by Python 100 knock # 1 channel replacement
100 image processing by Python Knock # 6 Color reduction processing
Analysis of X-ray microtomography image by Python
Matrix Convolution Filtering-Reinventor of Python Image Processing-
Straight line drawing by matrix-Inventor's original research of Python image processing-
Communication processing by Python
First Python image processing
Image processing with Python
Various processing of Python
Image processing? The story of starting Python for
Image processing by Python 100 knock # 11 smoothing filter (average filter)
Image processing with Python (Part 2)
[Language processing 100 knocks 2020] Summary of answer examples by Python
Affine transformation by matrix (scaling, rotation, shearing, movement) -Reinventor of Python image processing-
Image processing with Python (Part 1)
Image processing with Python (Part 3)
Post processing of python (NG)
Image Processing Collection in Python
[Python] Image processing with scikit-image
Error divided by 0 Processing of ZeroDivisionError 2
Image capture of firefox using python
Expansion by argument of python dictionary
Personal notes for python image processing
Image processing with Python 100 knocks # 3 Binarization
Behavior of python3 by Sakura's server
100 Language Processing Knock Chapter 1 by Python
Story of power approximation by Python
Image processing with Python 100 knocks # 4 Binarization of Otsu (discriminant analysis method)
Explanation of production optimization model by Python
Image processing with Python 100 knock # 10 median filter
Python: Basics of image recognition using CNN
Socket communication and multi-thread processing by Python
[Learning memo] Basics of class by python
(Java, JavaScript, Python) Comparison of string processing
Image processing with Python 100 knocks # 8 Max pooling
Conditional branching of Python learned by chemoinformatics
[Python] Calculation of image similarity (Dice coefficient)
Example of 3D skeleton analysis by Python
Image processing with Python & OpenCV [Tone Curve]
Image processing with Python 100 knock # 12 motion filter
Change processing by user input of dialog
Pandas of the beginner, by the beginner, for the beginner [Python]
Easy image processing in Python with Pillow
Image processing with Python 100 knocks # 7 Average pooling
The story of blackjack A processing (python)
Light image processing with Python x OpenCV
Status of each Python processing system in 2020
Image processing with Python 100 knocks # 9 Gaussian filter
[Image processing] Posterization
Introduction of Python
Extract dominant color of image by k-means clustering
Color page judgment of scanned image with python
Image processing from scratch with python (5) Fourier transform
View the result of geometry processing in Python