Find the average / standard deviation of the brightness values in the image

Purpose

It is a memorandum when calculating the histogram of the brightness value in the image and the average / variance.

code

Find the histogram, mean and standard deviation of the image below lena.png: Test image black.png: Uniformly black image

test.py


import numpy as np
import scipy.stats as sstats
import cv2

def print_stat(fname):

    img_name = fname
    img = cv2.imread(fname,1)
    # Grayscale
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    #One-dimensional array
    img = np.array(img).flatten()
    # img = img[img!=255]

    mean = img.mean()             #Average value
    std = np.std(img)             #standard deviation
    median = np.median(img)       #Median
    mode = sstats.mode(img)[0][0] #Mode

    print("---", img_name)
    print("mean    : ", mean)
    print("stddev  : ", std)
    print("median  : ", median)
    print("mode    : ", mode)

print_stat('lena.png')
print_stat('black.png')

result

$ python test.py
--- lena.png
mean    :  106.4122314453125
stddev  :  45.733566693923045
median  :  107.0
mode    :  44
--- black.png
mean    :  0.0
stddev  :  0.0
median  :  0.0
mode    :  0

Other

If you want to make it a little easier to use from the command line,

test.py


import sys
import numpy as np
import scipy.stats as sstats
import cv2

args = sys.argv

def print_stat(fname):

    img_name = fname
    img = cv2.imread(fname,1)
    # Grayscale
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    #One-dimensional array
    img = np.array(img).flatten()
    # img = img[img!=255]

    mean = img.mean()
    std = np.std(img)
    median = np.median(img)
    mode = sstats.mode(img)[0][0]

    print("name=" + img_name + ",")
    print("mean=" + str(mean) + ",")
    print("stddev=" + str(std) + ",")
    print("median=" + str(median) + ",")
    print("mode=" + str(mode) + ",")

arg1=args[1]
print_stat(arg1)
$ RET=$(python test.py lena.png)
$ echo $RET
name=lena.png , mean=106.4122314453125 , stddev=45.733566693923045 , median=107.0 , mode=44 ,
$ echo $RET | sed -e 's/.*name=//' |  cut -d',' -f 1 | awk '{printf "%-20s\n",$1}'
lena.png
$ echo $RET | sed -e 's/.*mean=//' |  cut -d',' -f 1 | awk '{printf "%-20s\n",$1}'
106.4122314453125

reference

Learn image processing with Python3 & OpenCV [2] ~ Histogram basic experiment for information visualization

Recommended Posts

Find the average / standard deviation of the brightness values in the image
Find the average / standard deviation of the brightness values in the image
Display a histogram of image brightness values in python
Inherit the standard library to find the average value of Queue
Compare the scores of the M-1 Grand Prix in terms of deviation values
Find the number of days in a month
Find the divisor of the value entered in python
Find the solution of the nth-order equation in python
Dispersion with Python's standard library and Pillow Find the RGB standard deviation of the image and determine if it is monochromatic
Calculation of standard deviation and correlation coefficient in Python
Find the sum of unique values with pandas crosstab
I tried to find the entropy of the image with python
How to find the optimal number of clusters in k-means
Maya | Find out the number of polygons in the selected object
Find out the apparent width of a string in python
I tried to find the average of the sequence with TensorFlow
Find the difference in Python
Python --Find out number of groups in the regex expression
Find the eigenvalues of a real symmetric matrix in Python
[Machine learning] Understand from mathematics that standardization results in an average of 0 and a standard deviation of 1.
I compared the calculation time of the moving average written in Python
Read the standard output of a subprocess line by line in Python
[python] Get the rank of the values in List in ascending / descending order
Input / output method of values from standard input in competitive programming, etc.
Find the definition of the value of errno
Detect mosaic points in the image
The story of participating in AtCoder
About the average option in sklearn.metrics.f1_score
The meaning of ".object" in Django
Change the saturation and brightness of color specifications like # ff000 in python 2.5
Find the area of the union of overlapping rectangles
[Understanding in 3 minutes] The beginning of Linux
Check the behavior of destructor in Python
The story of an error in PyOCR
Get the top nth values in Pandas
Put together consecutive values in the list
Visualize the boundary values of the multi-layer perceptron
Implement part of the process in C ++
Cut out A4 print in the image
The result of installing python in Anaconda
Let's claim the possibility of pyenv-virtualenv in 2021
Migemo version of the: find command,: mfind
The basics of running NoxPlayer in Python
Tips: Comparison of the size of three values
In search of the fastest FizzBuzz in Python
Find it in the procession and edit it
Find the coefficients of the least squares polynomial
Extract the color of the object in the image with Mask R-CNN and K-Means clustering
Find the index of items that match the conditions in the pandas data frame / series
Analyze the usage status of the contact confirmation application (COCOA) posted in "Image" / Tesseract
How to find the coefficient of the trendline that passes through the vertices in Python