從 HSI 轉回 RGB
選讀・IP 06・色彩影像處理 的補充資料(第 2/4 則)。主線不依賴這則補充;引用編號對應本頁底部的參考文獻。
導覽:本頁與本系列: 導覽:本頁與本系列
這則補充給出 HSI 一節中 RGB 轉 HSI 的反運算。它要看 落在哪一個 扇區。在 RG 扇區():
其中 是以度為單位的色相, 是飽和度, 是強度。在 GB 扇區(),先把 減去 ,再套用同樣三條公式,但把 換成 。在 BR 扇區(),減去 ,並把 換成 。每個扇區中,與色相「相對」的那個通道得到最小值 。
程式碼 · Python · 18 行
import numpy as np
# rgb_to_hsi is the function from the HSI section of the main text
def hsi_to_rgb(H, S, I):
"""H in degrees, S and I in [0, 1]. Inverse of rgb_to_hsi."""
H = np.mod(H, 360.0)
out = np.zeros(H.shape + (3,))
# (sector start, index of the 'big' channel, of the smallest, of the rest)
for lo, (a, b, c) in ((0, (0, 2, 1)), (120, (1, 0, 2)), (240, (2, 1, 0))):
m = (H >= lo) & (H < lo + 120)
h = np.radians(H[m] - lo)
x = I[m] * (1 - S[m])
y = I[m] * (1 + S[m] * np.cos(h) / np.cos(np.radians(60) - h))
out[m, a], out[m, b], out[m, c] = y, x, 3 * I[m] - (x + y)
return out
x = np.random.default_rng(0).random((1000, 3))
print(np.abs(hsi_to_rgb(*rgb_to_hsi(x)) - x).max()) # about 1e-9來回轉換 hsi_to_rgb(*rgb_to_hsi(x)) 對隨機 RGB 輸入的誤差約在 以內。
參考文獻
- R. C. Gonzalez and R. E. Woods, Digital Image Processing, 4th ed., Pearson, 2018, Ch. 6. publisher page
- C. Wyman, P.-P. Sloan, and P. Shirley, “Simple Analytic Approximations to the CIE XYZ Color Matching Functions,” Journal of Computer Graphics Techniques, vol. 2, no. 2, 2013. jcgt.org
- IEC 61966-2-1:1999, “Multimedia systems and equipment – Colour measurement and management – Part 2-1: Colour management – Default RGB colour space – sRGB,” IEC, 1999. IEC webstore
- Recommendation ITU-R BT.709-6, “Parameter values for the HDTV standards for production and international programme exchange,” ITU, 2015. ITU
- ISO/CIE 11664-4:2019, “Colorimetry – Part 4: CIE 1976 L*a*b* colour space,” ISO/CIE, 2019. standard page
- Recommendation ITU-R BT.601-7, “Studio encoding parameters of digital television for standard 4:3 and wide screen 16:9 aspect ratios,” ITU, 2011. ITU
- P. Kovesi, “Good Colour Maps: How to Design Them,” arXiv:1509.03700, 2015. arXiv
- S. van der Walt and N. Smith, viridis colormap design notes (BIDS project page; presented at SciPy 2015), 2015. project page
- F. Crameri, G. E. Shephard, and P. J. Heron, “The misuse of colour in science communication,” Nature Communications, vol. 11, art. 5444, 2020. PMC
- G. Buchsbaum, “A spatial processor model for object colour perception,” Journal of the Franklin Institute, vol. 310, no. 1, pp. 1–26, 1980. DOI
- J. van de Weijer, T. Gevers, and A. Gijsenij, “Edge-Based Color Constancy,” IEEE Transactions on Image Processing, vol. 16, no. 9, pp. 2207–2214, 2007. DOI
- R. Lukac, B. Smolka, K. Martin, K. N. Plataniotis, and A. N. Venetsanopoulos, “Vector Filtering for Color Imaging,” IEEE Signal Processing Magazine, vol. 22, no. 1, pp. 74–86, Jan. 2005. PDF
- S. Di Zenzo, “A note on the gradient of a multi-image,” Computer Vision, Graphics, and Image Processing, vol. 33, pp. 116–125, 1986. DOI
- A. Gijsenij, T. Gevers, and J. van de Weijer, “Computational Color Constancy: Survey and Experiments,” IEEE Transactions on Image Processing, vol. 20, no. 9, pp. 2475–2489, 2011. DOI
- J. T. Barron and Y.-T. Tsai, “Fast Fourier Color Constancy,” CVPR, 2017. arXiv
- Y. Hu, B. Wang, and S. Lin, “FC4: Fully Convolutional Color Constancy with Confidence-Weighted Pooling,” CVPR, 2017. paper
- M. Afifi and M. S. Brown, “Deep White-Balance Editing,” CVPR, 2020. arXiv
- R. Zhang, P. Isola, and A. A. Efros, “Colorful Image Colorization,” arXiv:1603.08511, 2016. arXiv
- S. Anwar, M. Tahir, C. Li, A. Mian, F. S. Khan, and A. W. Muzaffar, “Image Colorization: A Survey and Dataset,” arXiv:2008.10774, 2020. arXiv
- X. Kang, T. Yang, W. Ouyang, P. Ren, L. Li, and X. Xie, “DDColor: Towards Photo-Realistic Image Colorization via Dual Decoders,” ICCV, 2023. arXiv