Unity 3D : LSC 基本原理

版权声明:本文为博主 ( 黃彥霖 ) 原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38884324/article/details/81200850

前言 :

LSC ( Lens Shading Correction ) 鏡頭暗角校正。鏡頭可以看做是一個凸透鏡,光線均勻進入時,成像面會產生中間亮且四周暗的情況,例如下圖 :

这里写图片描述

而我們在拍攝照片時,是不希望產生暗角的,所以需要透過軟件算法校正的,而以下這張圖是我由上圖校正過後的結果 :

这里写图片描述

整個平均有沒有 !?

好啦,我們差不多要來看程式碼了

這個程式碼主要只是講 LSC 的算法重點,而非LSC最佳化,如果要把 LSC 調到好,那你還需要做二線性差值才行。

二線性差值我們下次再講,今天先講校正的原理

原理 : 基本上就是先取得中間最亮的顏色,然後再根據每個顏色與中間顏色的 “比值”,再將每個顏色乘以這個校正的 比值,就能平均輸出顏色了。

程式碼 :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LensShadingCorrection : MonoBehaviour
{

    public Texture2D imputImg;

    public RawImage outputImg;

    void Start()
    {
        Texture2D t = new Texture2D(imputImg.width, imputImg.height);

        // 取得原圖中間點顏色 (圖片中間最亮部分)
        Color centerColor = imputImg.GetPixel(imputImg.width / 2, imputImg.height / 2);

        for (int y = 0; y < t.height; y++)
        {
            for (int x = 0; x < t.width; x++)
            {
                Color inputPixelColor = imputImg.GetPixel(x, y);

                float n = centerColor.grayscale / inputPixelColor.grayscale; // 得到校正值 n // 當前像素亮度與中間點亮度的比例

                Color LSC_Color = inputPixelColor * n; // 將當前顏色乘以校正值 n ,得到校正後的顏色

                t.SetPixel(x, y, LSC_Color);
            }
        }

        t.Apply();

        outputImg.texture = t;

        System.IO.File.WriteAllBytes("C:/A/LSC-1-校正前.png", imputImg.EncodeToPNG());
        System.IO.File.WriteAllBytes("C:/A/LSC-2-校正後.png", t.EncodeToPNG());
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_38884324/article/details/81200850
LSC