-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_1_code.py
More file actions
65 lines (50 loc) · 2.29 KB
/
task_1_code.py
File metadata and controls
65 lines (50 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import os
import cv2
import numpy as np
INPUT_DIR = "task_1_input"
OUTPUT_DIR = "task_1_output"
os.makedirs(OUTPUT_DIR, exist_ok=True)
target_size = (640, 512)
image_files = os.listdir(INPUT_DIR)
pairs = {}
# Match image pairs
for filename in image_files:
if filename.endswith(".JPG"):
if "_Z.JPG" in filename:
key = filename.replace("_Z.JPG", "")
pairs.setdefault(key, {})["rgb"] = filename
elif "_T.JPG" in filename:
key = filename.replace("_T.JPG", "")
pairs.setdefault(key, {})["thermal"] = filename
for key, pair in pairs.items():
if "rgb" in pair and "thermal" in pair:
rgb_path = os.path.join(INPUT_DIR, pair["rgb"])
thermal_path = os.path.join(INPUT_DIR, pair["thermal"])
rgb_img = cv2.imread(rgb_path)
thermal_img = cv2.imread(thermal_path)
# Resize to 640x512
rgb_resized = cv2.resize(rgb_img, target_size)
thermal_resized = cv2.resize(thermal_img, target_size)
# Convert thermal to grayscale
thermal_gray = cv2.cvtColor(thermal_resized, cv2.COLOR_BGR2GRAY)
# Apply INFERNO colormap
thermal_colored = cv2.applyColorMap(thermal_gray, cv2.COLORMAP_INFERNO)
# Color Boosting (very close to your uploaded sample)
thermal_colored = cv2.convertScaleAbs(thermal_colored, alpha=1.6, beta=25)
# Slight sharpening to enhance edges
sharpen_kernel = np.array([[-1, -1, -1],
[-1, 9, -1],
[-1, -1, -1]])
thermal_colored = cv2.filter2D(thermal_colored, -1, sharpen_kernel)
# Add black border (20px) all around to match your output framing
final_output = cv2.copyMakeBorder(
thermal_colored, 20, 20, 20, 20,
borderType=cv2.BORDER_CONSTANT,
value=[0, 0, 0]
)
# Save outputs
cv2.imwrite(os.path.join(OUTPUT_DIR, f"{key}_Z.JPG"), rgb_resized) # RGB (unaltered)
cv2.imwrite(os.path.join(OUTPUT_DIR, f"{key}_AT.JPG"), final_output) # Thermal-only enhanced
print(f"[✓] Final output generated with perfect color match: {key}")
else:
print(f"[!] Skipped incomplete pair: {key}")