Tuesday, November 17, 2026

Image Processing-07 Removing Illumination Dominance

လည်ပတ်မှုပုံ ၇၂ ပုံ နှင့် အလင်းရောင်ပြောင်းလဲမှုပုံ ၁၂ ပုံ၊ စုစုပေါင်း ၈၄ ပုံလုံး အပေါ်တွင် Gaussian Smoothing + Sobel Gradient + L2 Normalization ကြိုတင်ပြင်ဆင်မှုကို အသုံးပြုလိုက်သောအခါ PCA နှင့် Isomap ဂရပ်ဖ်နှစ်ခုလုံး၌ အလင်းရောင်လွှမ်းမိုးမှု (Illumination Dominance) လုံးဝနီးပါး ပျောက်ကွယ်သွားပြီး ဝက်ဝံရုပ်၏ ပင်ကိုယ်လည်ပတ်မှု စက်ဝိုင်းပုံသဏ္ဌာန် (1D Circular Manifold) ပြန်လည်ပေါ်ထွက်လာပါသည်။

၁။ နောက်ခံ သီအိုရီသဘောတရားများနှင့် ချိတ်ဆက်ရှင်းလင်းချက်

ဤပြောင်းလဲမှုဖြစ်စဉ်ကို Artificial Intelligence: A Modern Approach (4th Edition) စာအုပ်မှ အဓိက သီအိုရီ ၄ ခုဖြင့် နားလည်နိုင်ပါသည်။

(က) Feature Engineering & Illumination Invariance (Chapter 19, Sec. 19.9 & Chapter 27, Sec. 27.3)

  • Raw Pixel Space ၏ အားနည်းချက်။ AIMA Section 19.9 (Developing Machine Learning Systems) တွင် Raw Data များကို Algorithm ထဲသို့ တိုက်ရိုက်ထည့်သွင်းခြင်းထက် ပြဿနာနှင့် ကိုက်ညီသော Feature Representation တည်ဆောက်ခြင်းက စွမ်းဆောင်ရည်ကို အဆုံးအဖြတ်ပေးကြောင်း ဖော်ပြထားပါသည်။ မူလ Pixel တန်ဖိုး  သည် မီးအလင်းရောင် ပြင်းအား  နှင့် မျက်နှာပြင်အရောင်  တို့ ရောနှောနေသဖြင့် မီးအလင်းရောင် ပြောင်းလဲမှု ၁၂ ပုံက Data Matrix ၏ Variance အများစုကို လွှမ်းမိုးသွားခြင်း ဖြစ်ပါသည်။

 

  • Spatial Gradients & Normalization (Section 27.3)။ Sobel Edge Magnitude () သည် အလင်းရောင်ညီညာနေသော ကိုယ်ထည်ပြင်ညီများကို သုည () အဖြစ် ဖယ်ရှားပစ်ပြီး အရာဝတ္ထု၏ အနားသတ်ကောက်ကြောင်း (Contours) ကိုသာ ချန်ထားပေးပါသည်။ ထို့နောက် L2 Normalization () ဖြင့် စားလိုက်သောအခါ မီးအလင်းရောင် အားပြင်းမှု () သည် ပိုင်းဝေနှင့် ပိုင်းခြေ နှစ်ဖက်လုံး၌ ကျေပျက်သွားပြီး () အလင်းရောင်ဒဏ်ခံနိုင်သော (Illumination-Invariant) Feature Vector ကို ရရှိပါသည်။

 

(ခ) Disentangling Factors of Variation (Chapter 22, Sec. 22.3 & Sec. 22.7)

  • Nuisance Variables vs. Latent Factors: Section 22.7 တွင် လက်တွေ့ကမ္ဘာဒေတာများ၌ ကျွန်ုပ်တို့ သိလိုသော အဓိကအကြောင်းတရား (Task-relevant Latent Factor — ဝက်ဝံရုပ်၏ လည်ပတ်ထောင့် ) နှင့် မလိုလားအပ်သော အနှောင့်အယှက်အချက်အလက် (Nuisance Factors — မီးအလင်းရောင် ) တို့ ရောထွေးနေတတ်ကြောင်း ရှင်းပြထားပါသည်။

 

  • Hand-crafted Filters မှ Deep CNNs သို့ ကူးပြောင်းပုံ (Section 22.3) ယခုကျွန်ုပ်တို့ ကိုယ်တိုင်ရေးသားထည့်သွင်းလိုက်သော Gaussian + Sobel Filter များသည် Classical Computer Vision ၏ အနှစ်သာရ ဖြစ်ပြီး၊ Deep Learning ရှိ Convolutional Neural Networks (CNNs) များ၏ ပထမဆုံး Layer များက Backpropagation ဖြင့် အလိုအလျောက် သင်ယူလေ့ရှိသော Edge / Gabor Filters များ၏ သင်္ချာပုံစံအတိုင်းပင် ဖြစ်ပါသည်။

 

(ဂ) Manifold Collapse in Isomap (Chapter 22, Sec. 22.7 & Chapter 19, Sec. 19.7)

  • မပြုပြင်မီ (Raw Pixels) 32_i ပုံ ၁၂ ပုံသည် Euclidean Distance အရ 32 စက်ဝိုင်းကွင်းနှင့် ဝေးကွာနေသဖြင့် Isomap ၏ -NN Graph (n_neighbors=6) တွင် မူလစက်ဝိုင်းကွင်း၏ တစ်နေရာမှ အပြင်ဘက်သို့ ထိုးထွက်နေသော အကိုင်းအခက် (Protruding Branch/Tail) တစ်ခု ဖြစ်ပေါ်ခဲ့ပါသည်။

 

  • ပြုပြင်ပြီးနောက် (Sobel + L2) 32_i ပုံ ၁၂ ပုံလုံးသည် ဝက်ဝံရုပ်ကို မလှည့်ဘဲ ရှေ့တည့်တည့် () အနေအထား၌ မီးအလင်းရောင်သာ ပြောင်းရိုက်ထားသော ပုံများ ဖြစ်ပါသည်။ Edge + L2 Normalization လုပ်လိုက်သောအခါ ထို ၁၂ ပုံလုံး၏ Feature Vector များသည် ALOI/32 ထဲမှ  ဝန်းကျင်ပုံများနှင့် ထပ်တူနီးပါး တူညီသွားသဖြင့် Isomap ၏ အကိုင်းအခက် လုံးဝပျောက်ကွယ်သွားပြီး စက်ဝိုင်းကွင်း၏  နေရာပေါ်သို့ တိုက်ရိုက်ပေါင်းစည်း (Collapse onto the Manifold) သွားပါသည်။

 ၂။ စုစုပေါင်း ၈၄ ပုံလုံးကို နှိုင်းယှဉ်တွက်ချက်သည့် Python ကုဒ်

အောက်ပါကုဒ်သည် မူလ Raw Pixels (၈၄ ပုံ) နှင့် Preprocessed Edge Features (၈၄ ပုံ) တို့အပေါ်တွင် PCA နှင့် Isomap ရလဒ်များကို တစ်ပြိုင်နက် ယှဉ်တွဲပြသပေးမည့်အပြင်၊ အလင်းရောင်လွှမ်းမိုးမှု လျော့ကျသွားမှုကို ကိန်းဂဏန်းဖြင့်ပါ တိုင်းတာပြသပါမည်။

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

plt.style.use('ggplot')

# ==============================================================================
# အပိုင်း (၁) - Preprocessing Function နှင့် ၈၄ ပုံလုံး ဖတ်ယူခြင်း
# AIMA Ch. 19.9 (Feature Engineering) & Ch. 27.3 (Sobel Gradients + L2 Norm)
# ==============================================================================
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 extract_raw_and_edge(folder_path, sigma=1.0):
    files = sorted(
        [f for f in os.listdir(folder_path) if f.lower().endswith('.png')],
        key=extract_number
    )
    raw_list = []
    edge_list = []
    angles = []
    img_shape = None
    
    for fname in files:
        img_path = os.path.join(folder_path, fname)
        img = np.array(Image.open(img_path).convert('L'), dtype=np.float64)
        
        # 1. Raw Downsampled & Normalized [0, 1] -> (97, 72)
        small = img[::2, ::2] / 255.0
        if img_shape is None:
            img_shape = small.shape
        raw_list.append(small.reshape(-1))
        
        # 2. Gaussian Smoothing -> Sobel Gradient Magnitude -> L2 Normalization
        smoothed = ndimage.gaussian_filter(small, sigma=sigma)
        gx = ndimage.sobel(smoothed, axis=1)
        gy = ndimage.sobel(smoothed, axis=0)
        grad_mag = np.hypot(gx, gy).reshape(-1)
        edge_norm = grad_mag / (np.linalg.norm(grad_mag) + 1e-8)
        edge_list.append(edge_norm)
        
        angles.append(extract_number(fname))
        
    return np.array(raw_list), np.array(edge_list), np.array(angles), img_shape

# ALOI/32 (72 ပုံ) နှင့် ALOI/32_i (12 ပုံ) ကို ဖတ်ယူပေါင်းစပ်ခြင်း (စုစုပေါင်း 84 ပုံ)
X_raw_rot, X_edge_rot, angles_rot, img_shape = extract_raw_and_edge(dir_32)
X_raw_ill, X_edge_ill, _, _ = extract_raw_and_edge(dir_32_i)

X_raw_84 = np.vstack([X_raw_rot, X_raw_ill])     # Shape: (84, 6984)
X_edge_84 = np.vstack([X_edge_rot, X_edge_ill])  # Shape: (84, 6984)


# ==============================================================================
# အပိုင်း (၂) - PCA နှင့် Isomap တို့ကို Before vs. After တွက်ချက်ခြင်း
# AIMA Ch. 22.7 (Unsupervised Learning & Manifold Learning)
# ==============================================================================
# 1. PCA (Linear)
pca_raw = decomposition.PCA(n_components=4).fit(X_raw_84)
T_pca_raw = pca_raw.transform(X_raw_84)

pca_edge = decomposition.PCA(n_components=4).fit(X_edge_84)
T_pca_edge = pca_edge.transform(X_edge_84)

# 2. Isomap (Non-linear, K=6)
iso_raw = manifold.Isomap(n_neighbors=6, n_components=2).fit(X_raw_84)
T_iso_raw = iso_raw.transform(X_raw_84)

iso_edge = manifold.Isomap(n_neighbors=6, n_components=2).fit(X_edge_84)
T_iso_edge = iso_edge.transform(X_edge_84)

# အလင်းရောင်ပုံ ၁၂ ပုံ (32_i) နှင့် လည်ပတ်မှုပုံ ၇၂ ပုံ (32) တို့၏ PC1 ပြန့်ကျဲမှု အချိုးအစား (Spread Ratio)
raw_spread_ratio = np.std(T_pca_raw[72:, 0]) / np.std(T_pca_raw[:72, 0])
edge_spread_ratio = np.std(T_pca_edge[72:, 0]) / np.std(T_pca_edge[:72, 0])

print("=== Quantitative Verification (84 Images Combined) ===")
print(f"PC1 Illumination-to-Rotation Spread Ratio [Before / Raw] : {raw_spread_ratio:.2f}x (Illumination Dominates!)")
print(f"PC1 Illumination-to-Rotation Spread Ratio [After / Edge] : {edge_spread_ratio:.2f}x (Illumination Suppressed!)")
print(f"Isomap Reconstruction Error [Before / Raw] : {iso_raw.reconstruction_error():.4f}")
print(f"Isomap Reconstruction Error [After / Edge] : {iso_edge.reconstruction_error():.4f}")


# ==============================================================================
# ဂရပ်ဖ် (၁) - 2x2 နှိုင်းယှဉ်ချက် (PCA & Isomap: Raw Pixels vs. Sobel+L2 Edges)
# မှတ်ချက် - စက်ဝိုင်းအစက် ၇၂ စက်ကို လည်ပတ်ထောင့် (0°-355°) အလိုက် hsv အရောင်စဉ်ဖြင့်ပြပြီး
# အလင်းရောင်ပုံ ၁၂ ပုံကို အနီရောင်ကြယ်ပွင့် (*) များဖြင့် ထပ်ပြထားပါသည်။
# ==============================================================================
fig1, axes = plt.subplots(2, 2, figsize=(15, 11))

plots_info = [
    (axes[0, 0], T_pca_raw, f'1. PCA — BEFORE (Raw Pixels)\nPC1 Hijacked by Illumination ({pca_raw.explained_variance_ratio_[0]*100:.1f}% Var)'),
    (axes[0, 1], T_pca_edge, f'2. PCA — AFTER (Sobel Edge + L2 Norm)\nRotation Loop Recovered! ({sum(pca_edge.explained_variance_ratio_[:2])*100:.1f}% Var)'),
    (axes[1, 0], T_iso_raw, '3. Isomap (K=6) — BEFORE (Raw Pixels)\nIllumination Forms a Protruding Branch/Tail'),
    (axes[1, 1], T_iso_edge, '4. Isomap (K=6) — AFTER (Sobel Edge + L2 Norm)\nClean 1D Circular Manifold (Branch Collapsed!)')
]

for ax, T_data, title in plots_info:
    # လည်ပတ်မှုပုံ ၇၂ ပုံ (0° -> 355°)
    sc = ax.scatter(T_data[:72, 0], T_data[:72, 1], c=angles_rot, cmap='hsv',
                    s=60, alpha=0.9, edgecolors='k', label='ALOI/32 (72 Rotations)')
    # အလင်းရောင်ပြောင်းလဲမှုပုံ ၁၂ ပုံ (32_i)
    ax.scatter(T_data[72:, 0], T_data[72:, 1], c='crimson', marker='*',
               s=180, edgecolors='k', zorder=5, label='ALOI/32_i (12 Illuminations)')
    
    # 0° အမှတ်ကို ညွှန်ပြခြင်း
    ax.annotate('0° (Front)', (T_data[0, 0], T_data[0, 1]), fontsize=9, fontweight='bold',
                xytext=(6, 6), textcoords='offset points')
    ax.set_title(title, fontsize=11, fontweight='bold')
    ax.set_xlabel('Component 1')
    ax.set_ylabel('Component 2')
    ax.legend(loc='best', fontsize=8.5)

cbar = fig1.colorbar(sc, ax=axes.ravel().tolist(), shrink=0.85, pad=0.02)
cbar.set_label('Rotation Angle (0° → 355°)')
plt.suptitle('AIMA Ch. 27.3 & Ch. 22.7: Eliminating Illumination Dominance Across All 84 Images',
             fontsize=14, fontweight='bold')
plt.show()


# ==============================================================================
# ဂရပ်ဖ် (၂) - ပြန်လည်ရရှိလာသော "Edge Eigen-bears" (84 ပုံလုံးမှ ထွက်ပေါ်လာသော PC 1–4)
# သီအိုရီ - 32_i ပုံ ၁၂ ပုံ ပါဝင်နေသော်လည်း PC 1 နှင့် PC 2 တို့သည် မီးအလင်းရောင်
# အကွက်များ မဟုတ်တော့ဘဲ ဝက်ဝံရုပ်၏ ဘယ်/ညာ၊ ရှေ့/နောက် ကောက်ကြောင်းများ ပြန်ဖြစ်သွားပုံ
# ==============================================================================
eig_edge_bears = pca_edge.components_.reshape((-1, img_shape[0], img_shape[1]))

fig2, axes2 = plt.subplots(1, 4, figsize=(15, 4.2))
for i in range(4):
    vmax = np.max(np.abs(eig_edge_bears[i]))
    axes2[i].imshow(eig_edge_bears[i], cmap='RdBu_r', vmin=-vmax, vmax=vmax)
    axes2[i].set_title(f'[84 Images] Edge Eigen-bear PC {i+1}\nVar: {pca_edge.explained_variance_ratio_[i]*100:.2f}%',
                       fontsize=10, fontweight='bold')
    axes2[i].axis('off')

plt.suptitle('Reclaimed Intrinsic Pose Basis: Top 4 Edge Eigen-bears (Combined 84 Images)',
             fontsize=13, fontweight='bold')
plt.tight_layout()
plt.show()


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

  1. PCA နှိုင်းယှဉ်ချက် (အပေါ်တန်း ဂရပ်ဖ် ၁ နှင့် ၂)
    • BEFORE (Raw Pixels): အနီရောင်ကြယ်ပွင့် ၁၂ ပွင့် (32_i) သည် အလျားလိုက် PC 1 ဝင်ရိုးတစ်လျှောက် ကျယ်ပြန့်စွာ ပြန့်ကားနေပြီး (Spread Ratio မြင့်မားနေပြီး) လည်ပတ်မှုပုံ ၇၂ ပုံကို အလယ်တွင် ဖိညှပ်ထားပါသည်။
    • AFTER (Sobel Edge + L2 Norm): အလင်းရောင်လွှမ်းမိုးမှု လုံးဝပျောက်ကွယ်သွားပြီး လည်ပတ်မှုပုံ ၇၂ ပုံသည် အရောင်စဉ်အတိုင်း အဝိုင်းပတ်လမ်းကြောင်း ပြန်လည်ဖြစ်ပေါ်လာပါသည်။ ထို့ပြင် အနီရောင်ကြယ်ပွင့် ၁၂ ပွင့်လုံးသည် PC 1 တစ်လျှောက် မပြန့်ကျဲတော့ဘဲ စက်ဝိုင်းကွင်း၏ 0° (Front) နေရာတစ်ဝိုက်တွင် စုစည်းသွားသည် ကို တွေ့ရပါမည်။ အကြောင်းမှာ 32_i ပုံများသည် ရှေ့တည့်တည့် () ဝက်ဝံရုပ်ကို မီးအလင်းရောင်ပြောင်းရိုက်ထားခြင်း ဖြစ်သောကြောင့် ဖြစ်ပါသည်။
  2. Isomap နှိုင်းယှဉ်ချက် (အောက်တန်း ဂရပ်ဖ် ၃ နှင့် ၄)

    • BEFORE (Raw Pixels): မူလ စက်ဝိုင်းကွင်း၏ 0° နေရာမှ အပြင်ဘက်သို့ အနီရောင်ကြယ်ပွင့်များ တန်းစီထိုးထွက်နေသော အမြီးတန်း/အကိုင်းအခက် (Protruding Tail/Branch) တစ်ခု ဖြစ်ပေါ်နေပါသည်။
    • AFTER (Sobel Edge + L2 Norm): ထိုအပြင်ဘက်သို့ ထိုးထွက်နေသော အကိုင်းအခက် လုံးဝပျောက်ကွယ်သွားပြီး အနီရောင်ကြယ်ပွင့် ၁၂ ပွင့်လုံးသည် မူလ စက်ဝိုင်းကွင်း၏ 0° အနီရောင်အစက်များပေါ်သို့ တိုက်ရိုက်ထပ်တူကျ ပေါင်းစည်းသွားပါသည်။ ဤသည်မှာ High-Dimensional Space ထဲမှ အနှောင့်အယှက် Dimension (Illumination) ကို ဖယ်ရှားပြီး စစ်မှန်သော 1D Intrinsic Topology ကို ပြန်လည်ရရှိခြင်း ဖြစ်ပါသည်။ 
  1. Reclaimed Edge Eigen-bears (ဂရပ်ဖ် ၂)
    • စုစုပေါင်း ၈၄ ပုံလုံးကို ပေါင်းတွက်ထားသော်လည်း PC 1 နှင့် PC 2 ပုံရိပ်များသည် တစ်ပြင်လုံး အနီ/အပြာ မီးအလင်းရောင်အကွက်များ မဟုတ်တော့ဘဲ ဝက်ဝံရုပ်၏ ဘယ်/ညာ နှင့် ရှေ့/နောက် အနားသတ်ကောက်ကြောင်းများ အဖြစ် ပြန်လည်ပြောင်းလဲသွားသည်ကို အထင်အရှား မြင်တွေ့ရပါမည်။

 ဤကဲ့သို့ Raw Pixels မှ Spatial Gradient Edge Features သို့ ပြောင်းလဲခြင်းသည် AIMA Chapter 19, 22 နှင့် 27 တို့တွင် ဆွေးနွေးထားသော မှန်ကန်သော Representation ကို ရွေးချယ်နိုင်ပါက ရိုးရှင်းသော Algorithm များပင်လျှင် ရှုပ်ထွေးသော ပြင်ပအနှောင့်အယှက်များကို ကျော်လွှားနိုင်သည် ဟူသော အခြေခံမူကို လက်တွေ့သက်သေပြလိုက်ခြင်း ဖြစ်ပါသည်။

No comments:

Post a Comment

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