VBA function to convert RGB values into OpenCV HSV values.
Note: These bits of code are mostly for my own reference, but if anyone else finds them useful all the better.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
Function RGB_to_HSV(R As Integer, G As Integer, B As Integer, ChannelToReturn As Integer) As Integer Dim ReturnValue As Integer Dim H, S, V As Integer Dim TempH, TempS, TempV As Double Dim TempR, TempG, TempB As Double Dim MinRGBInteger, MaxRGBInteger As Integer Dim MinRGBDouble, MaxRGBDouble As Double TempR = R / 255 TempG = G / 255 TempB = B / 255 MinRGBInteger = Application.WorksheetFunction.Min(R, G, B) MaxRGBInteger = Application.WorksheetFunction.Max(R, G, B) MinRGBDouble = MinRGBInteger / 255 MaxRGBDouble = MaxRGBInteger / 255 V = MaxRGBInteger TempV = V / 255 If (V <> 0) Then TempS = (TempV - MinRGBDouble) / TempV Else TempS = 0 End If If (MinRGBInteger <> MaxRGBInteger) Then If (V = R) Then TempH = ((60 * (TempG - TempB)) / (TempV - MinRGBDouble)) End If If (V = G) Then TempH = 120 + (60 * (TempB - TempR) / (TempV - MinRGBDouble)) End If If (V = B) Then TempH = 240 + (60 * (TempR - TempG) / (TempV - MinRGBDouble)) End If If (TempH < 0) Then TempH = TempH + 360 End If Else TempH = 0 End If H = TempH / 2 S = 255 * TempS Select Case ChannelToReturn Case 1 ReturnValue = H Case 2 ReturnValue = S Case 3 ReturnValue = V End Select RGB_to_HSV = ReturnValue End Function |
Thanks! Note that the two additional functions could be avoided.
Instead of
GetRGBMinimum(r,g,b)
useApplication.WorksheetFunction.Min(r,g,b)
.Instead of
GetRGBMaximum(r,g,b)
useApplication.WorksheetFunction.Max(r,g,b)
Thanks!
Hello
do you have developed by any chance some VBA code for HSV to RGB conversion ?
Thanks
JP