My answer to this question involved changing the white in a binary mask to a different color.
I came up with a few ways to do this:
colorizeMask1[im_, color_] := ColorReplace[im, 1 -> color]
colorizeMask2[im_, color_] := Colorize[im, {ColorRules -> {0 -> Black, 1 -> color}}]
colorizeMask3[im_, color_] := ReplacePixelValue[im, PixelValuePositions[im, 1] -> color]
colorizeMask4[im_, color_] := ColorCombine[(List @@ color) * im]
colorizeMask5[im_, color_] := ImageMultiply[im, ConstantImage[color, ImageDimensions[im]]]
I'm not quite satisfied with any of these: they are either slow or seem too round-about.
im = Binarize[RegionImage[Annulus[], RasterSize -> 1024]];
colorizeMask4[im, Green]
First[AbsoluteTiming[#[im, Green];]] & /@
{colorizeMask1, colorizeMask2, colorizeMask3, colorizeMask4, colorizeMask5}
{0.252601, 2.45989, 0.084044, 0.011478, 0.035167}
Is there a faster, built in way to do this?
Colorize
would be my go-to but I accept that 2.5s is too slow! $\endgroup$Outer[]
product of the binary image'sImageData[]
with the RGB triplet of the new color (treated as a vector). $\endgroup$