## About this memorandum

Recently, I had a chance to experience deep learning and image/video processing at work, but there were many things I didn't understand about how to touch parameters and amplify data, so I decided to study video processing from scratch.

## Statistics

- Image (pixel value) statistics: used for various image analysis
- Mean value
- Median
- Frequent value
- Variance
- Contrast

- Some can be calculated from histograms

The right image shows a white whiteboard with a black metamon on it.

max:1 (white), min:0.0 (black), and we can see that the contrast is 1.0.

There are many ways to calculate contrast.(see code below)

Statistics calculation code

```
im_files =[file_path_A, file_path_B, file_path_C]
for file in im_files:
im = imread(file)[:,:,:3] # For RGBA, extract only RGB
im = rgb2gray(im)
imshow(im)
plt.show()
print('mean: ', im.mean())
print('std: ', im.std())
print('median: ', np.median(im))
print('max: ', im.max())
print('min: ', im.min())
print('contrast1: ', (im.max() - im.min()) / (im.max() + im.min()) ) # Michelson contrast
print('contrast2: ', im.max() / im.min() if im.min() > 0 else np.nan ) # contrast ratio
print('contrast3: ', im.max() - im.min() ) # contrast difference
print()
```

## Calculate the mean and variance

### Unsuggested code

- Keep the definition formula
- Large computational complexity: 2 double loops

```
im = rgb2gray(imread('file_path'))
h, w = im.shape
mean = 0
for y in range(h):
for x in range(w):
mean += im[y, x]
mean /= h * w
print('mean: ', mean)
var = 0
for y in range(h):
for x in range(w):
var += (im[y, x] - mean)**2
var /= h * w
print('variance: ', var)
print('std: ', np.sqrt(var))
```

### Suggested code

- Transformed formula
- Half the computation: one loop.

```
im = rgb2gray(imread('file_path'))
h, w = im.shape
mean = 0
var = 0
for y in range(h):
for x in range(w):
mean += im[y, x]
var += im[y, x]**2
mean /= h * w
print('mean: ', mean)
var /= h * w
var -= mean**2
print('variance: ', var)
print('std: ', np.sqrt(var))
```

## Top comments (0)