Skip to content

Instantly share code, notes, and snippets.

@hughescr
Created October 13, 2016 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hughescr/d53f74651edc55dccc24f06d0c724edb to your computer and use it in GitHub Desktop.
Save hughescr/d53f74651edc55dccc24f06d0c724edb to your computer and use it in GitHub Desktop.
/*
A Core Image kernel routine that computes RGB<->HSV.
H range is 0-6, S and V 0-1
Alpha is preserved unchanged; actually we ignore it since we're only dealing with camera images which don't have any alpha
*/
vec4 HSVtoRGB(vec4 HSV)
{
vec4 hue;
HSV.x = HSV.x / 60.0;
hue.x = abs(HSV.x - 3.0) - 1.0;
hue.y = 2.0 - abs(HSV.x - 2.0);
hue.z = 2.0 - abs(HSV.x - 4.0);
return ((clamp(hue,0.0,1.0) - 1.0) * HSV.y + 1.0) * HSV.z;
}
vec4 RGBtoHSV(vec4 RGB)
{
vec4 HSV;
float maxV = max(RGB.r, max(RGB.g, RGB.b));
float C = maxV - min(RGB.r, min(RGB.g, RGB.b));
float D = step(0.0, -C);
HSV.z = maxV;
HSV.y = C / (maxV + D);
vec4 Delta = (maxV - RGB) / (C + D);
Delta -= Delta.brga;
Delta += vec4(2.0,4.0,6.0,0.0);
Delta *= step(maxV, RGB.gbra);
HSV.x = fract(max(Delta.r, max(Delta.g, Delta.b)) / 6.0)*360.0;
return HSV;
}
kernel vec4 visualizeScoreOreFinderFilter(__sample i, float centerAngle, float angleWidth, float saturationFloor)
{
vec4 o;
vec4 hsv;
hsv = RGBtoHSV(i);
// Difference between hue and chosen hue
float delta = abs(hsv.x-centerAngle);
// If delta > 180 then delta = 360-delta
delta = compare(180.0-delta, 360.0-delta, delta);
// If delta < angleWidth then if hsv.y > saturationFloor then yes else no
float shouldProcess = compare(delta-angleWidth, compare(saturationFloor-hsv.y, -1.0, 1.0), 1.0);
o.r = compare(shouldProcess, clamp(2.0*hsv.y,0.0,1.0), 0.0);
o.g = o.b = 0.0;
o.a = 1.0;
return o;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment