Tuesday, November 17, 2026

Image Processing-05 Illumination Dominance

လည်ပတ်မှုပုံ ၇၂ ပုံထဲသို့  အလင်းရောင်ပြောင်းလဲမှုပုံ ၁၂ ပုံကို ထပ်ပေါင်းထည့်လိုက်သောအခါ PCA ၏ ထိပ်တန်း Eigen-bear (PC 1) သည် "ဝက်ဝံရုပ် လည်ပတ်မှု" ကို စွန့်လွှတ်ပြီး "မီးအလင်းရောင် ပြောင်းလဲမှု" အဖြစ် ချက်ချင်းပြောင်းလဲသွားပါသည်။ ဤဖြစ်စဉ်သည် Computer Vision နယ်ပယ်၏ အရေးကြီးဆုံး စိန်ခေါ်မှုတစ်ခုဖြစ်သော Illumination Dominance (အလင်းရောင်လွှမ်းမိုးမှု ပြဿနာ) ကို လက်တွေ့ပြသနေခြင်း ဖြစ်ပါသည်။

၁။ သီအိုရီနောက်ခံ (Chapter 27 & Chapter 22)

(က) ပုံရိပ်ဖြစ်ပေါ်မှု သင်္ချာညီမျှခြင်း (Chapter 27, Section 27.2 — Image Formation)

Stuart Russell နှင့် Peter Norvig တို့၏ စာအုပ် Section 27.2 တွင် ဓာတ်ပုံတစ်ပုံပေါ်ရှိ Pixel တစ်ခုချင်းစီ၏ အလင်းတောက်ပမှုတန်ဖိုး $I(x, y)$ သည် အောက်ပါအချက်နှစ်ခု မြှောက်လဒ်အပေါ် မူတည်ကြောင်း ရှင်းပြထားပါသည်—

$$I(x, y) \propto \rho(x, y) \cdot \big(\mathbf{n}(x, y) \cdot \mathbf{s}\big)$$
  • $\rho(x, y)$ (Surface Albedo / Intrinsic Geometry): ဝက်ဝံရုပ်၏ အမွေးအရောင်နှင့် ပင်ကိုယ်မျက်နှာပြင် ဂုဏ်သတ္တိ (လည်ပတ်မှု 32 တွင် ပြောင်းလဲသောအရာ)။

  • $\mathbf{s}$ (Light Source Direction & Intensity): မီးအလင်းရောင် ကျရောက်သည့် ဦးတည်ရာနှင့် အားပြင်းမှု (အလင်းရောင်ပြောင်းလဲမှု 32_i တွင် ပြောင်းလဲသောအရာ)။

(ခ) အဘယ်ကြောင့် အလင်းရောင်ပြောင်းလဲမှု (၁၂ ပုံ) က လည်ပတ်မှု (၇၂ ပုံ) ထက် ပိုမိုလွှမ်းမိုးသွားသနည်း။

Computer Vision သမိုင်းတွင် ထင်ရှားသော သီအိုရီတစ်ခုမှာ "အရာဝတ္ထုတစ်ခုတည်းကို မီးအလင်းရောင် ပြောင်းရိုက်ခြင်းကြောင့် ဖြစ်ပေါ်သော Pixel ကွာခြားချက်သည် အရာဝတ္ထုကို လှည့်လိုက်ခြင်း (သို့မဟုတ် အခြားအရာဝတ္ထုတစ်ခုနှင့် လဲလှယ်ခြင်း) ကြောင့် ဖြစ်ပေါ်သော ကွာခြားချက်ထက် ပိုမိုကြီးမားသည်" ဟူ၍ ဖြစ်ပါသည်—

  1. Global vs. Local Pixel Variance ဝက်ဝံရုပ်ကို $5^\circ$ လှည့်လိုက်သောအခါ ပုံ၏ အနားသတ်မျဉ်း (Contours) နှင့် နားရွက်/လက် အနီးတစ်ဝိုက်ရှိ Pixel အနည်းငယ်သာ ပြောင်းလဲပါသည်။ သို့သော် မီးမောင်းတစ်လုံးကို ဘယ်မှညာသို့ ပြောင်းထိုးလိုက်သောအခါ ဝက်ဝံရုပ် တစ်ကိုယ်လုံးရှိ Pixel ထောင်ပေါင်းများစွာ ၏ တန်ဖိုးများ တစ်ပြိုင်နက်တည်း ပြောင်းလဲသွားပါသည်။

  2. PCA ၏ Variance-Maximization သဘာဝ (Chapter 22, Section 22.7) PCA သည် Unsupervised နည်းလမ်းဖြစ်သဖြင့် "မည်သည်က ဝက်ဝံရုပ်၏ ပုံသဏ္ဌာန်ဖြစ်ပြီး မည်သည်က မီးအလင်းရောင်ဖြစ်သည်" ကို မခွဲခြားတတ်ပါ။ ၎င်းသည် ဒေတာ Matrix တစ်ခုလုံးတွင် Variance (ကိန်းဂဏန်း ပြန့်ကျဲကွာဟမှု) အများဆုံး ဦးတည်ရာ ကိုသာ PC 1 အဖြစ် အမြဲရွေးချယ်ပါသည်။ ထို့ကြောင့် အလင်းရောင်ပြောင်းလဲမှု ၁၂ ပုံ ထည့်လိုက်သည်နှင့် PC 1 သည် မီးခလုတ်တစ်ခုသဖွယ် ပြောင်းလဲသွားခြင်း ဖြစ်ပါသည်။

၂။ Eigen-bears ပြောင်းလဲပုံ စမ်းသပ်ခြင်း

အောက်ပါ Python ကုဒ်သည် မူလ Eigen-bears များနှင့် ပေါင်းစပ်လိုက်သောအခါ ထွက်ပေါ်လာသော Eigen-bears များကို ယှဉ်တွဲပြသပေးမည့်အပြင် PC 1 ကို မီးအလင်းရောင် အတိုးအလျှော့ခလုတ်အဖြစ် စမ်းသပ်ပြသပါမည်။

Python
import os
import re
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from sklearn import decomposition

plt.style.use('ggplot')

# ==============================================================================
# အပိုင်း (၁) - ALOI/32 (72 ပုံ) နှင့် ALOI/32_i (12 ပုံ) တို့ကို ဖတ်ယူခြင်း
# AIMA Ch. 27.2 (Image Formation: Pose Variation vs. Illumination Variation)
# ==============================================================================
dir_32 = os.path.join('Module4', 'Datasets', 'ALOI', '32')
dir_32_i = os.path.join('Module4', 'Datasets', 'ALOI', '32_i')

def extract_number(filename):
    match = re.search(r'(\d+)\.png$', filename, re.IGNORECASE)
    return int(match.group(1)) if match else 0

def load_image_folder(folder_path):
    files = sorted(
        [f for f in os.listdir(folder_path) if f.lower().endswith('.png')],
        key=extract_number
    )
    imgs = []
    shape_2d = None
    for fname in files:
        img = np.array(Image.open(os.path.join(folder_path, fname)).convert('L'), dtype=np.float64)
        # မူရင်း assignment5.py အတိုင်း [::2, ::2] လျှော့ချပြီး [0.0, 1.0] သို့ Normalize လုပ်ခြင်း
        img_small = img[::2, ::2] / 255.0
        if shape_2d is None:
            shape_2d = img_small.shape  # (97, 72)
        imgs.append(img_small.reshape(-1))
    return np.array(imgs), shape_2d

X_rot, img_shape = load_image_folder(dir_32)      # Shape: (72, 6984) -> လည်ပတ်မှုပုံများ
X_illum, _ = load_image_folder(dir_32_i)          # Shape: (12, 6984) -> အလင်းရောင်ပုံများ

# Dataset နှစ်ခုလုံးကို ပေါင်းစပ်ခြင်း -> စုစုပေါင်း 84 ပုံ (84, 6984)
X_combined = np.vstack([X_rot, X_illum])
print(f"Rotation Only: {X_rot.shape} | Illumination Only: {X_illum.shape} | Combined: {X_combined.shape}")


# ==============================================================================
# အပိုင်း (၂) - PCA နှစ်မျိုး တွက်ချက်ခြင်း (Before vs. After Adding Illumination)
# AIMA Ch. 22.7 (PCA Variance Maximization Sensitivity)
# ==============================================================================
pca_rot = decomposition.PCA(n_components=4).fit(X_rot)
pca_comb = decomposition.PCA(n_components=4).fit(X_combined)

eig_rot = pca_rot.components_.reshape((-1, img_shape[0], img_shape[1]))
eig_comb = pca_comb.components_.reshape((-1, img_shape[0], img_shape[1]))

var_rot = pca_rot.explained_variance_ratio_ * 100
var_comb = pca_comb.explained_variance_ratio_ * 100


# ==============================================================================
# ဂရပ်ဖ် (၁) - Eigen-bears နှိုင်းယှဉ်ချက် (အပေါ်တန်း: 32 သီးသန့် | အောက်တန်း: 32 + 32_i)
# လေ့လာရန် - အောက်တန်းရှိ PC 1 နှင့် PC 2 တို့သည် ဝက်ဝံရုပ်၏ အနားသတ်များအစား
# တစ်ကိုယ်လုံး အလင်း/အမှောင် ကျရောက်မှု (Illumination Masks) ဖြစ်သွားပုံကို ကြည့်ပါ။
# ==============================================================================
fig1, axes1 = plt.subplots(2, 4, figsize=(15, 8))

for i in range(4):
    # အပေါ်တန်း - Rotation Only (32)
    vmax_r = np.max(np.abs(eig_rot[i]))
    axes1[0, i].imshow(eig_rot[i], cmap='RdBu_r', vmin=-vmax_r, vmax=vmax_r)
    axes1[0, i].set_title(f'[32 Only] PC {i+1}\nVar: {var_rot[i]:.1f}%', fontsize=10, fontweight='bold')
    axes1[0, i].axis('off')
    
    # အောက်တန်း - Combined (32 + 32_i)
    vmax_c = np.max(np.abs(eig_comb[i]))
    axes1[1, i].imshow(eig_comb[i], cmap='RdBu_r', vmin=-vmax_c, vmax=vmax_c)
    axes1[1, i].set_title(f'[32 + 32_i] PC {i+1}\nVar: {var_comb[i]:.1f}%', fontsize=10, fontweight='bold', color='darkred')
    axes1[1, i].axis('off')

plt.suptitle('AIMA Ch. 27.2 & 22.7: How Illumination (32_i) Hijacks Top Eigen-bears',
             fontsize=14, fontweight='bold')
plt.tight_layout()
plt.show()


# ==============================================================================
# ဂရပ်ဖ် (၂) - Combined PCA 2D Space တွင် အလင်းရောင်ပုံ ၁၂ ပုံ၏ နေရာယူမှု
# သီအိုရီ - 12 ပုံတည်းသာ ရှိသော်လည်း PC 1 ဝင်ရိုးတစ်လျှောက် ကျယ်ပြန့်စွာ ဆန့်ထွက်နေပြီး
# လည်ပတ်မှု 72 ပုံကို အလယ်တွင် ကျဉ်းမြောင်းစွာ ဖိညှပ်ထားပုံကို မြင်တွေ့ရမည်။
# ==============================================================================
T_comb = pca_comb.transform(X_combined)
T_rot_proj = T_comb[:72]    # ပထမ ၇၂ ပုံ (Rotation)
T_illum_proj = T_comb[72:]  # နောက်ဆုံး ၁၂ ပုံ (Illumination)

plt.figure(figsize=(9, 6))
plt.scatter(T_rot_proj[:, 0], T_rot_proj[:, 1], c='royalblue', marker='o', s=55,
            alpha=0.8, edgecolors='k', label='ALOI/32: 360° Rotation (72 images)')
plt.scatter(T_illum_proj[:, 0], T_illum_proj[:, 1], c='crimson', marker='*', s=180,
            edgecolors='k', label='ALOI/32_i: Illumination Variations (12 images)')

plt.title('Combined PCA Space (PC 1 vs. PC 2): Illumination Dominates PC 1 Axis', fontsize=12, fontweight='bold')
plt.xlabel(f'PC 1 — Primarily Illumination Intensity/Direction ({var_comb[0]:.1f}% Variance)')
plt.ylabel(f'PC 2 — Side Lighting / Pose Mixture ({var_comb[1]:.1f}% Variance)')
plt.legend(loc='best')
plt.grid(True)
plt.show()


# ==============================================================================
# ဂရပ်ဖ် (၃) - PC 1 ကို "Virtual Light Switch" အဖြစ် အသုံးပြု၍ အလင်းအမှောင် ပြောင်းကြည့်ခြင်း
# မူလ Mean Bear ပေါ်သို့ Combined PC 1 ကို အနုတ် (-) မှ အပေါင်း (+) အထိ ပေါင်းထည့်ကြည့်ခြင်း
# ==============================================================================
mean_comb = pca_comb.mean_.reshape(img_shape)
pc1_pattern = eig_comb[0]

# PC 1 Score (z_1) တန်ဖိုး အနိမ့်ဆုံးမှ အမြင့်ဆုံးအထိ ၅ ဆင့် ယူခြင်း
z1_min, z1_max = T_comb[:, 0].min(), T_comb[:, 0].max()
z1_steps = np.linspace(z1_min, z1_max, 5)

fig3, axes3 = plt.subplots(1, 5, figsize=(15, 4))
for idx, z1 in enumerate(z1_steps):
    synthetic_bear = mean_comb + z1 * pc1_pattern
    synthetic_bear = np.clip(synthetic_bear, 0.0, 1.0)  # Pixel တန်ဖိုး [0, 1] အတွင်း ထိန်းထားခြင်း
    
    axes3[idx].imshow(synthetic_bear, cmap='gray', vmin=0, vmax=1)
    axes3[idx].set_title(f'z₁ = {z1:.1f}\n(Virtual Lighting)', fontsize=10)
    axes3[idx].axis('off')

plt.suptitle('Traversing Combined PC 1: Acting as a Virtual Illumination Controller',
             fontsize=13, fontweight='bold')
plt.tight_layout()
plt.show()

၃။ ထွက်ပေါ်လာသော ဂရပ်ဖ် ၃ ခုကို အသေးစိတ် ခွဲခြမ်းစိတ်ဖြာချက်

  1. Eigen-bears နှိုင်းယှဉ်ချက် (ဂရပ်ဖ် ၁)

    • အပေါ်တန်း (32 Only) PC 1 နှင့် PC 2 တို့သည် ဝက်ဝံရုပ်၏ ဘယ်/ညာ အနားသတ်များကို ခွဲခြားထားပြီး လည်ပတ်မှုထောင့်ကို ဖမ်းယူထားပါသည်။

    • အောက်တန်း (32 + 32_i) PC 1 ၏ ပုံသဏ္ဌာန်သည် လုံးဝပြောင်းလဲသွားပြီး ဝက်ဝံရုပ် တစ်ကိုယ်လုံးကို တစ်ရောင်တည်း (တစ်ပြင်လုံး အနီ သို့မဟုတ် တစ်ပြင်လုံး အပြာ) ခြယ်ထားသကဲ့သို့ ဖြစ်သွားသည်ကို တွေ့ရပါမည်။ အကြောင်းမှာ 32_i ထဲတွင် မီးလုံးအရေအတွက် အနည်းအများ (Overall Brightness) ပြောင်းရိုက်ထားသော ပုံများ ပါဝင်သောကြောင့် ဖြစ်ပါသည်။ ထို့ပြင် PC 2 တွင်လည်း ဝက်ဝံရုပ်၏ မျက်နှာနှင့် ကိုယ်ထည်တစ်ခြမ်းလုံးကို ဘယ်/ညာ မီးထိုးထားသည့် Directional Shading (အရိပ်ကျရောက်မှု) ပုံစံ ပေါ်ထွက်လာပါမည်။

  2. Combined PCA 2D Scatter Plot (ဂရပ်ဖ် ၂)

    • အနီရောင် ကြယ်ပွင့် ၁၂ ပွင့် (32_i) သည် အလျားလိုက် PC 1 ဝင်ရိုးတစ်လျှောက် ဘယ်အစွန်း (အမှောင်ဆုံးပုံ) မှ ညာအစွန်း (အလင်းဆုံးပုံ) အထိ ကျယ်ပြန့်စွာ ပြန့်ကားနေသည်ကို တွေ့ရပါမည်။

    • တစ်ချိန်တည်းမှာပင် အပြာရောင် အစက် ၇၂ စက် (32 လည်ပတ်မှုပုံများ) သည် အလယ်တစ်နေရာတွင် စုပြုံဖိညှပ်ခံလိုက်ရပါသည်။ ဤအချက်က Linear PCA သည် အလင်းရောင်ပြောင်းလဲမှု ပါဝင်လာသည်နှင့် အရာဝတ္ထု၏ Pose/Shape ကို ခွဲခြားနိုင်စွမ်း ကျဆင်းသွားကြောင်း တိကျစွာ သက်သေပြနေပါသည်။

  3. Virtual Lighting Controller (ဂရပ်ဖ် ၃)

    • Mean Bear ပေါ်သို့ PC 1 ကို အနုတ်တန်ဖိုးမှ အပေါင်းတန်ဖိုးအထိ ပြောင်းလဲပေါင်းထည့်ကြည့်သောအခါ ဝက်ဝံရုပ်သည် လုံးဝ မလှည့်ဘဲ မှောင်မည်းနေရာမှ တဖြည်းဖြည်း မီးအလင်းရောင် လင်းထိန်လာသည်ကို မြင်တွေ့ရပါမည်။ ဤသည်မှာ Unsupervised Learning က ဒေတာထဲမှ "မီးခလုတ် (Latent Illumination Factor)" ကို အလိုအလျောက် ရှာဖွေတွေ့ရှိသွားခြင်းပင် ဖြစ်ပါသည်။

နောက်တဆင့်မှာ-
Computer Vision စနစ်များတွင် ဤကဲ့သို့ အလင်းရောင်လွှမ်းမိုးမှု ပြဿနာကို ဖြေရှင်းရန် Section 27.3 — Edge Detection / Image Gradients နှင့် Chapter 22 (Convolutional Neural Networks) တို့တွင် အသုံးပြုသော Spatial Gradient / Edge Filtering (Sobel Filter) နည်းလမ်းနဲ့ ပုံများကို ကြိုတင်ပြင်ဆင်ပြီး ပြန်စမ်းသပ်ကြည့်ပါမယ်။

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.