Skip to main content
cleanup / performance of min-check and hue diff
Source Link
flinty
  • 25.9k
  • 2
  • 22
  • 92

This technique comparing the pixel hues in HSB to a list of known hues seems to produce much better masks than my ColorQuantize approach:

img = Import["https://i.sstatic.net/q9Q5A.png"] // ImageAdjust;
himg = ColorConvert[img, "HSB"];
cols = ColorConvert[{Red, Yellow, Green, Blue}, "HSB"];
diffAngle[a1_diffHue[c1_, a2_]c2_] := .5 - Abs[Abs[a1Abs[Abs[c1[[1]] - a2]c2[[1]]] - .5]
nearestcolpos[c_] := 
 First[FirstPosition[cols, 
   First[MinimalBy[colsFirst@OrderingBy[cols, diffAngle[c[[1]]diffHue[c, #[[1]]]#] &]]]]&]
saturationThreshold = .1;
brightnessThreshold = .15;
isblack[c_] := 
  c[[3]] < brightnessThreshold || c[[2]] < saturationThreshold;
hpalette = Map[If[isblack[#], 0, nearestcolpos[#]] &, ImageData[himg], {2}];
masks = Table[Image[hpalette /. (x_ /; x != i -> 0)], {i, Length[cols]}]

better masks

This technique comparing the pixel hues in HSB to a list of known hues seems to produce much better masks than my ColorQuantize approach:

img = Import["https://i.sstatic.net/q9Q5A.png"] // ImageAdjust;
himg = ColorConvert[img, "HSB"];
cols = ColorConvert[{Red, Yellow, Green, Blue}, "HSB"];
diffAngle[a1_, a2_] := .5 - Abs[Abs[a1 - a2] - .5]
nearestcolpos[c_] := 
 First[FirstPosition[cols, 
   First[MinimalBy[cols, diffAngle[c[[1]], #[[1]]] &]]]]
saturationThreshold = .1;
brightnessThreshold = .15;
isblack[c_] := 
  c[[3]] < brightnessThreshold || c[[2]] < saturationThreshold;
hpalette = Map[If[isblack[#], 0, nearestcolpos[#]] &, ImageData[himg], {2}];
masks = Table[Image[hpalette /. (x_ /; x != i -> 0)], {i, Length[cols]}]

better masks

This technique comparing the pixel hues in HSB to a list of known hues seems to produce much better masks than my ColorQuantize approach:

img = Import["https://i.sstatic.net/q9Q5A.png"] // ImageAdjust;
himg = ColorConvert[img, "HSB"];
cols = ColorConvert[{Red, Yellow, Green, Blue}, "HSB"];
diffHue[c1_, c2_] := .5 - Abs[Abs[c1[[1]] - c2[[1]]] - .5]
nearestcolpos[c_] := First@OrderingBy[cols, diffHue[c, #] &]
saturationThreshold = .1;
brightnessThreshold = .15;
isblack[c_] := c[[3]] < brightnessThreshold || c[[2]] < saturationThreshold;
hpalette = Map[If[isblack[#], 0, nearestcolpos[#]] &, ImageData[himg], {2}];
masks = Table[Image[hpalette /. (x_ /; x != i -> 0)], {i, Length[cols]}]

better masks

Source Link
flinty
  • 25.9k
  • 2
  • 22
  • 92

This technique comparing the pixel hues in HSB to a list of known hues seems to produce much better masks than my ColorQuantize approach:

img = Import["https://i.sstatic.net/q9Q5A.png"] // ImageAdjust;
himg = ColorConvert[img, "HSB"];
cols = ColorConvert[{Red, Yellow, Green, Blue}, "HSB"];
diffAngle[a1_, a2_] := .5 - Abs[Abs[a1 - a2] - .5]
nearestcolpos[c_] := 
 First[FirstPosition[cols, 
   First[MinimalBy[cols, diffAngle[c[[1]], #[[1]]] &]]]]
saturationThreshold = .1;
brightnessThreshold = .15;
isblack[c_] := 
  c[[3]] < brightnessThreshold || c[[2]] < saturationThreshold;
hpalette = Map[If[isblack[#], 0, nearestcolpos[#]] &, ImageData[himg], {2}];
masks = Table[Image[hpalette /. (x_ /; x != i -> 0)], {i, Length[cols]}]

better masks