Image Processing using PyTorch
Array Creation
Simulate a 2D grayscale image (size: 256×256) with random pixel values in the range [0, 255] using
PyTorch tensors.
Problem 1: Image Brightness and Contrast Adjustment
1. Increase the brightness by adding a constant scalar value to all pixels.
2. Adjust the contrast using a linear transformation: new_pixel = a × old_pixel + b where a (gain)
and b (bias) are user-defined parameters.
3. Clip all resulting values to the range [0, 255].
4. Display the original and transformed images side by side using matplotlib.
5. Use [Link]() and perform all operations on tensors without converting to NumPy.
Problem 2: Image Thresholding and Masking
Use PyTorch logical operations to create a binary mask of all pixels above a chosen intensity
threshold. Apply this mask to isolate the brighter regions of the image and set all other pixels to
zero.
Tasks:
1. Define a threshold intensity value (e.g., 120).
2. Generate a binary mask using PyTorch’s vectorized comparisons.
3. Multiply the mask with the image to get the segmented output.
4. Visualize the mask and the masked image using matplotlib.
Problem 3: Sustainability-Linked Image Indexing
Imagine a remote sensing image (PyTorch 2D tensor) representing a green cover index across an
agricultural area. You need to compute sustainability metrics using PyTorch operations.
Tasks:
1. Normalize the image values to [0, 1].
2. Classify each pixel into categories:
- High vegetation (> 0.7)
- Medium vegetation (0.4–0.7)
- Low vegetation (< 0.4)
3. Compute the percentage area covered by each category.
4. Print and visualize the category map using different grayscale or color codes.