DEV Community

haratena9
haratena9

Posted on

8 2

Image Processing #1 Pixels, Quantization, and Sampling

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)


Enter fullscreen mode Exit fullscreen mode

original image



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


Enter fullscreen mode Exit fullscreen mode

selected area1



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


Enter fullscreen mode Exit fullscreen mode

selected area2

summary

summary

Grayscale image



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


Enter fullscreen mode Exit fullscreen mode

Image Grayscale

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

Image Result

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


Enter fullscreen mode Exit fullscreen mode

Image Sampling

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



Enter fullscreen mode Exit fullscreen mode

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

Image quantization

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay