VisionCpp  0.0.1
OP_RGBToHSV.hpp
Go to the documentation of this file.
1 // This file is part of VisionCpp, a lightweight C++ template library
2 // for computer vision and image processing.
3 //
4 // Copyright (C) 2016 Codeplay Software Limited. All Rights Reserved.
5 //
6 // Contact: visioncpp@codeplay.com
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 
20 /// \file OP_RGBToHSV.hpp
21 /// \brief it converts RGB pixel to HSV pixel
22 
23 namespace visioncpp {
24 /// \brief Functor converts RGB ( R: 0.0f..1.0f, G: 0.0f..1.0f, B: 0.0f..1.0f)
25 /// to HSV ( H: 0.0f..360.f, S: 0.0f..1.0f V: 0.0f..1.0f ) color space
26 struct OP_RGBToHSV {
27  /// \param inRGB
28  /// \return F32C3
30  // Convert from RGB to HSV, using float ranges 0.0 to 1.0.
31  float fR = inRGB[0], fG = inRGB[1], fB = inRGB[2];
32  float fH = 0.0f, fS = 0.0f, fV = 0.0f;
33 
34  float fDelta;
35  float fMin, fMax;
36  // Get the min and max, but use integer comparisons for slight speedup.
37  if (inRGB[2] < inRGB[1]) {
38  if (inRGB[2] < inRGB[0]) {
39  fMin = fB;
40  if (inRGB[0] > inRGB[1]) {
41  fMax = fR;
42  } else {
43  fMax = fG;
44  }
45  } else {
46  fMin = fR;
47  fMax = fG;
48  }
49  } else {
50  if (inRGB[1] < inRGB[0]) {
51  fMin = fG;
52  if (inRGB[2] > inRGB[0]) {
53  fMax = fB;
54  } else {
55  fMax = fR;
56  }
57  } else {
58  fMin = fR;
59  fMax = fB;
60  }
61  }
62  fDelta = fMax - fMin;
63  fV = fMax; // Value (Brightness).
64  if (fMax != 0.0f) { // Make sure it's not pure black.
65  fS = fDelta / fMax; // Saturation.
66  float ANGLE_TO_UNIT =
67  1.0f /
68  (6.0f * fDelta); // Make the Hues between 0.0 to 1.0 instead of 6.0
69  if (fDelta == 0.0f) {
70  fH = 0.0f; // undefined hue
71  } else if (fMax == inRGB[0]) { // between yellow and magenta.
72  fH = (fG - fB) * ANGLE_TO_UNIT;
73  } else if (fMax == inRGB[1]) { // between cyan and yellow.
74  fH = (2.0f / 6.0f) + (fB - fR) * ANGLE_TO_UNIT;
75  } else { // between magenta and cyan.
76  fH = (4.0f / 6.0f) + (fR - fG) * ANGLE_TO_UNIT;
77  }
78  // Wrap outlier Hues around the circle.
79  if (fH < 0.0f) fH += 1.0f;
80  if (fH >= 1.0f) fH -= 1.0f;
81  } else {
82  // color is pure Black.
83  fS = 0.0f;
84  fH = 0.0f; // undefined hue
85  }
86  return visioncpp::pixel::F32C3(fH, fS, fV);
87  }
88 };
89 }
Storage< float, 3 > F32C3
Definition: pixel.hpp:116
VisionCpp namespace.
Definition: sycl/device.hpp:24
Functor converts RGB ( R: 0.0f..1.0f, G: 0.0f..1.0f, B: 0.0f..1.0f) to HSV ( H: 0....
Definition: OP_RGBToHSV.hpp:26
visioncpp::pixel::F32C3 operator()(visioncpp::pixel::F32C3 inRGB)
Definition: OP_RGBToHSV.hpp:29