Image Processing #1 Pixels, Quantization, and Sampling
Tags: python, imageprocessing, formyownuse
Published: 2022-02-12 05:31:47

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

## Digital image (an image is a collection of pixels)
- An image is made up of pixels.
- Pixel value
    - unit8:an integer value from 0 to 255 (0:white => black:255)
    - float:real number between 0 and 1.
- Pixel size
    - unit8:1byte/pixel (8bit) 
    - float32:4bytes/pixel (32bit)
- Prayscale image
    - The pixel value represents only the brightness of the image.
- Represented as a two-dimensional array in the program.

```
im = imread('[file path]')
imshow(im)
```
![original image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7l73yqupzo7n9j775o9t.png)
 
```
im_eye = im[70:100, 120:150]
imshow(im_eye)
```
![selected area1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lmjt6sucst49lkvvenyi.png)

```
im_eye2 = im[80:90, 125:140]
imshow(im_eye2)
```
![selected area2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swovk0tyd9x705vhklix.png)
 
### summary
![summary](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3qnw7uk9lr4chkzy1ffa.png)
 
## Grayscale image

```
im_gray = cv2.imread('[file_path]', cv2.IMREAD_GRAYSCALE)
imshow(im_gray)
```

![Image Grayscale ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3srgf44moep0zc94wx2.png)

## Notes:accessing the image array
- Order of access to arrays
 - Rows, columns
- Access order to pixels
 - Vertical, horizontal
 - y, x
- If looping, outside is y, inside is x
 - The second index in the array is the contiguous memory area.

__In other words, it is the opposite of the general sense (x, y), so it is a hotbed of bugs during implementation.__

Code

```
im = np.zeros((5, 5)) # image with width 3✕ height 2 (array)
im[2, 3] = 255 # Access pixels with (x,y)=(3,2)
print(im)

imshow(im)
plt.axis('off')
plt.show()
```

Result

```
[[  0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.]
 [  0.   0.   0. 255.   0.]
 [  0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.]]
``` 

![Image Result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7fgcq703u5v24ymohm98.png)

## Sampling and Quantization
- Signals are sampled and quantized
- Sampling: spatial discretization
- Quantization: Discretization of values
 - Normally, 8bit 256 steps
 - For special applications, such as medical use, 10-bit and 12-bit are also available.

### Sampling 
```
image_downscaled1 = downscale_local_mean(im_gray, (1, 1))
image_downscaled2 = downscale_local_mean(im_gray, (10, 10))
image_downscaled3 = downscale_local_mean(im_gray, (20, 20))
image_downscaled4 = downscale_local_mean(im_gray, (50, 50))
```
![Image Sampling ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nnqdfmniffib5vso0scf.png)

### Quantization
```
# 32bit quantization
bins = np.linspace(0, im.max(), 2**5)
digi_image1 = np.digitize(im, bins)
digi_image1 = (np.vectorize(bins.tolist().__getitem__)(digi_image1-1).astype(int))

# 16bit quantization
bins = np.linspace(0, im.max(), 2**4)
digi_image2 = np.digitize(im, bins)
digi_image2 = (np.vectorize(bins.tolist().__getitem__)(digi_image2-1).astype(int))

# 8bit quantization
bins = np.linspace(0, im.max(), 2**3)
digi_image3 = np.digitize(im, bins)
digi_image3 = (np.vectorize(bins.tolist().__getitem__)(digi_image3-1).astype(int))

# 4bit quantization
bins = np.linspace(0, im.max(), 2**2)
digi_image4 = np.digitize(im, bins)
digi_image4 = (np.vectorize(bins.tolist().__getitem__)(digi_image4-1).astype(int))

``` 
※If the quantization is made too coarse, non-existent contours __"pseudo contours"__ will be generated.


![Image quantization ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ybv0o4ghrjpd9svxa56.png)