實作色彩系統轉換程式 (VB 6.0)
實作色彩系統轉換程式(VB 6.0)¶
介紹¶
RGB轉HSI¶
HSI轉RGB¶
當0°≦H<120°時,B值最小,因此如下式:
當120°<H≦240°時。
CMYK轉RGB¶
tCMYK = {C,M,Y,K}
轉換成三分色
tCMY = {C',M',Y'} = {C(1 - K) + K,M(1 - K) + K,Y(1 - K) + K}
然後再轉換成RGB
tRGB = {R,G,B} = {1 - C',1 - M',1 - Y'}
同理結果如下:
tRGB = {1 - (C(1 - K) + K),1 - (M(1 - K) + K),1 - (Y(1 - K) + K)} = {1 - C(1 - K) - K,1 - M(1 - K) - K,1 - Y(1 - K) - K}
RGB轉CMYK¶
tRGB = {R,G,B}
先轉換成三分色
tCMY = {C',M',Y'} = {1 - R,1 - G,1 - B}
if min{C',M',Y'} = 1
tCMYK = {0,0,0,1}
再轉換成四分色
K = min{C',M',Y'}
RGB轉YUV¶
Y=0.299R+0.587G+0.114B
U=0.493(B-Y)=0.439(-0.29R-0.587G+0.886B)
V=0.877(0.701R-0.587G-0.114B)
YUV與RGB關係如下:
RGB與YUV關係如下:
將其正規化,可以將RGB和YUV都在0到255之間轉換。所以可以採用下列的程式:
R = Y + (1.4075 * (V - 128)) G = Y - (0.3455 * (U - 128) - (0.7169 * (V - 128)) B = Y + (1.7790 * (U - 128) Y = R * .299 + G * .587 + B * .114 U = R * -.169 + G * -.332 + B * .500 + 128 V = R * .500 + G * -.419 + B * -.0813 + 128
YIQ與RGB之間的關係如下:
RGB與YIQ之間的關係如下:
YCbCr與RGB的關係如下:
RGB與YCbCr關係如下:
環境¶
作業系統:Microsoft Windows 7 (64 bit)
開發工具:Microsoft Visual Basic 6.0
用法¶
程式碼¶
'設定所有變數必須宣告才能使用 Option Explicit Private Sub Form_Load() '載入影像 Picture1.Picture = LoadPicture(App.Path & "\Lai, Tai-Yu.bmp") '設定picture1計算單位為像素 Picture1.ScaleMode = 3 '設定picture1自動重繪有效 Picture1.AutoRedraw = True End Sub 'Picture1滑鼠移動事件 Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) '宣告RGB變數 Dim intR, intG, intB '獲取RGB值 intR = (Picture1.Point(X, Y) And &HFF) intG = (Picture1.Point(X, Y) \ &H100) And &HFF intB = (Picture1.Point(X, Y) \ &H10000) And &HFF '顯示RGB值 labRGB_R.Caption = "R = " & intR labRGB_G.Caption = "G = " & intG labRGB_B.Caption = "B = " & intB '宣告轉換至HSV所需變數 Dim intS Dim dobH, dobS, dobV Dim intMax, intMin, intD '開始轉換HSV intMin = min(intR, intG, intB) intMax = max(intR, intG, intB) dobV = intMax / 255 intD = intMax - intMin If (intR = intG) And (intG = intB) Then dobH = 0 Else dobH = Arccos((0.5 * ((intR - intG) + (intR - intB))) / _ (Sqr(((intR - intG) * (intR - intG) + (intR - intB) * (intG - intB))))) End If If intB > intG Then dobH = 2 * 3.1415 - dobH End If If intMax > 0 Then dobS = intD / intMax Else dobS = 0 End If '顯示HSV數值 labHSV_H.Caption = "H = " & Format(dobH, "0.##") labHSV_S.Caption = "S = " & Format(dobS, "0.##") labHSV_V.Caption = "V = " & Format(dobV, "0.##") '宣告YUV變數 Dim YUV_Y, YUV_U, YUV_V '開始轉換,矩陣相乘 YUV_Y = 0.299 * intR + 0.587 * intG + 0.114 * intB YUV_U = 0.439 * (-0.29 * intR - 0.587 * intG + 0.886 * intB) YUV_V = 0.877 * (0.701 * intR - 0.587 * intG - 0.114 * intB) '顯示YUV數值 labYUV_Y.Caption = "Y = " & Format(YUV_Y, "0.##") labYUV_U.Caption = "U = " & Format(YUV_U, "0.##") labYUV_V.Caption = "V = " & Format(YUV_V, "0.##") '宣告YIQ變數 Dim YIQ_Y, YIQ_I, YIQ_Q '開始轉換,矩陣相乘 YIQ_Y = 0.299 * intR + 0.587 * intG + 0.114 * intB YIQ_I = 0.596 * intR + (-0.275 * intG) + (-0.321 * intB) YIQ_Q = 0.212 * intR + (-0.523 * intG) + 0.311 * intB '顯示YIQ數值 labYIQ_Y.Caption = "Y = " & Format(YIQ_Y, "0.##") labYIQ_I.Caption = "I = " & Format(YIQ_I, "0.##") labYIQ_Q.Caption = "Q = " & Format(YIQ_Q, "0.##") '宣告YCbCr變數 Dim YCbCr_Y, YCbCr_Cb, YCbCr_Cr '開始轉換,矩陣相乘 YCbCr_Y = 0.299 * intR + 0.587 * intG + 0.114 * intB YCbCr_Cb = (-0.1687 * intR) + (-0.3313 * intG) + 0.5 * intB + 128 YCbCr_Cr = 0.5 * intR + (-0.4187 * intG) + (-0.0813 * intB) + 128 '顯示YCbCr數值 labYCbCr_Y.Caption = "Y = " & Format(YCbCr_Y, "0.##") labYCbCr_Cb.Caption = "Cb = " & Format(YCbCr_Cb, "0.##") labYCbCr_Cr.Caption = "Cr = " & Format(YCbCr_Cr, "0.##") '宣告CMYK變數 Dim CMYK_C, CMYK_M, CMYK_Y, CMYK_K '開始轉換CMYK CMYK_C = 255 - intR CMYK_M = 255 - intG CMYK_Y = 255 - intB If CMYK_C < CMYK_M Then CMYK_K = CMYK_C Else CMYK_K = CMYK_M End If If CMYK_Y < CMYK_K Then CMYK_K = CMYK_Y End If If CMYK_K > 0 Then CMYK_C = CMYK_C - CMYK_K CMYK_M = CMYK_M - CMYK_K CMYK_Y = CMYK_Y - CMYK_K End If '顯示CMYK數值 labCMYK_C.Caption = "C = " & Format(CMYK_C, "0.##") labCMYK_M.Caption = "M = " & Format(CMYK_M, "0.##") labCMYK_Y.Caption = "Y = " & Format(CMYK_Y, "0.##") labCMYK_K.Caption = "K = " & Format(CMYK_K, "0.##") End Sub '尋找最小值函式 Private Function min(intA As Variant, intB As Variant, intC As Variant) '宣告最小值變數 Dim intMin As Integer 'A若小於等於B If intA <= intB Then '最小值為A intMin = intA '反之 Else '最小值為B intMin = intB End If '最小值變數大於C If intMin > intC Then '最小值設定為C intMin = intC End If '回傳最小值 min = intMin '結束函式 End Function '搜尋最大值函式 Private Function max(intA As Variant, intB As Variant, intC As Variant) '宣告最大值變數 Dim intMax As Integer '當A值大於等於B值 If intA >= intB Then '最大值為A intMax = intA '反之 Else '最大值為B intMax = intB End If '當最大值變數小於C If intMax < intC Then '則最大值變數設定為C intMax = intC End If '回傳最大值 max = intMax '結束函式 End Function ' Inverse Cosine (反餘弦)函式 Private Function Arccos(X) '避免除以零的情況 On Error Resume Next ' Inverse Cosine (反餘弦)函式 Arccos = Atn(-X / Sqr(-X * X + 1)) + 2 * Atn(1) '結束函式 End Function
例外¶
如果執行檔不能執行,請先安裝vbrun60sp6.exe。
參考¶
致謝¶
感謝 (維基自由百科) 提供的知識。