227k views
2 votes
Given:

- A 512 x 512 image stored in the variable `image`.
- Break the image into 8 x 8 chunks.
- Convert each chunk to the DCT basis.
- Drop frequencies with a contribution below 10% of the largest absolute value in each chunk.
- Convert back to the standard basis.
- Clip resulting pixel values between 0 and 255.

Instructions:
Write a code snippet that:
1. Defines the function `compress_chunk(chunk)` taking an 8 x 8 chunk, returning the compressed chunk.
2. Ensures the compressed chunk contains values between 0 and 255.
3. Uses the function to create the variable `compressed`, representing the compressed image.

Setup Code:
```python
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt

plt.figure
plt.imshow(image, cmap='gray')
compressed = image.copy() # Add your code here
plt.figure
plt.imshow(compressed, cmap='gray')
```

User Clyfe
by
7.3k points

1 Answer

3 votes

Final answer:

The provided code snippet defines the function 'compress_chunk' to compress an 8x8 chunk of an image using DCT compression and clips pixel values between 0 and 255. It then uses the function to compress the whole image in 8x8 blocks.

Step-by-step explanation:

The student is dealing with a compression task in digital image processing. Specifically, the task is to perform discrete cosine transform (DCT) compression on an image and clip the pixel values to fall between 0 and 255. The compression technique described involves taking an 8x8 pixel chunk of an image, converting it to the DCT basis, pruning frequencies that are below 10% of the largest frequency, converting back to standard basis, and ensuring pixel values are within the acceptable range. Here's a Python code snippet that follows the student's instructions:

import numpy as np

def compress_chunk(chunk):
dct_chunk = np.fft.fft2(chunk)
max_val = np.max(np.abs(dct_chunk))
threshold = 0.1 * max_val
dct_chunk[np.abs(dct_chunk) < threshold] = 0
compressed_chunk = np.fft.ifft2(dct_chunk).real
compressed_chunk = np.clip(compressed_chunk, 0, 255)
return compressed_chunk

# Assuming `image` is a numpy array of shape (512, 512)
compressed = np.zeros_like(image)
for i in range(0, image.shape[0], 8):
for j in range(0, image.shape[1], 8):
chunk = image[i:i+8, j:j+8]
compressed[i:i+8, j:j+8] = compress_chunk(chunk)

This code includes the compress_chunk function that compresses an 8x8 chunk using DCT while dropping low frequencies. It ensures that pixel values are normalized between 0 and 255. It also iterates over the entire image in 8x8 blocks to apply the compression.

User Tomos Williams
by
8.0k points