haratena9

Posted on

# Image Processing #1 Pixels, Quantization, and Sampling

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)
``````

``````im_eye = im[70:100, 120:150]
imshow(im_eye)
``````

``````im_eye2 = im[80:90, 125:140]
imshow(im_eye2)
``````

## Grayscale image

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

## Notes:accessing the image array

• 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.]]
``````

## 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))
``````

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