11/8/25, 9:09 PM Untitled38.
ipynb - Colab
# importing pytorch and matplotlib
import torch
import [Link] as plt
# making a fake 2D grayscale image (256x256) with pixel values 0–255
img = [Link](0, 256, (256, 256), dtype=torch.float32)
# showing original image
[Link](img, cmap='gray')
[Link]("Original Random Image")
[Link]('off')
[Link]()
# Problem 1 – Brightness and Contrast Adjustment
# adding constant value to increase brightness
bright_val = 35
bright_img = img + bright_val
# adjusting contrast using formula new = a*old + b
a = 1.15 # gain
b = 25 # bias
contrast_img = a * img + b
# clipping values in range [0, 255]
bright_img = [Link](bright_img, 0, 255)
contrast_img = [Link](contrast_img, 0, 255)
# showing all three images
[Link](figsize=(12,4))
[Link](1,3,1)
[Link](img, cmap='gray'); [Link]("Original"); [Link]('off')
[Link](1,3,2)
[Link](bright_img, cmap='gray'); [Link]("Brighter"); [Link]('off')
[Link](1,3,3)
[Link](contrast_img, cmap='gray'); [Link]("Contrast Adjusted"); [Link]('off')
[Link]()
# Problem 2 – Thresholding and Masking
# defining threshold value
th = 120
# making a mask where pixel > threshold
mask = img > th
# multiplying mask with image (bright areas remain)
masked_img = img * mask
# plotting mask and masked image
[Link](figsize=(10,4))
[Link](1,2,1)
[Link](mask, cmap='gray'); [Link]("Binary Mask"); [Link]('off')
[Link](1,2,2)
[Link](masked_img, cmap='gray'); [Link]("Masked Image"); [Link]('off')
[Link]()
# Problem 3 – Sustainability Linked Image Indexing
# normalize to [0, 1]
norm_img = img / 255.0
# categorize vegetation
high = norm_img > 0.7
medium = (norm_img > 0.4) & (norm_img <= 0.7)
low = norm_img <= 0.4
# calculate percentage area of each category
total = [Link]()
high_pct = ([Link]().item() / total) * 100
med_pct = ([Link]().item() / total) * 100
low_pct = ([Link]().item() / total) * 100
print("High vegetation :", round(high_pct,2), "%")
print("Medium vegetation :", round(med_pct,2), "%")
print("Low vegetation :", round(low_pct,2), "%")
# create a category map for display (different gray levels)
cat map = torch zeros like(img)
[Link] 1/3
11/8/25, 9:09 PM [Link] - Colab
cat_map = torch.zeros_like(img)
cat_map[low] = 60
cat_map[medium] = 150
cat_map[high] = 250
[Link](cat_map, cmap='viridis')
[Link]("Vegetation Category Map")
[Link]('off')
[Link]()
[Link] 2/3
11/8/25, 9:09 PM [Link] - Colab
[Link] 3/3