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]

enter image description here

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?

  • $\begingroup$ 0.01s seems pretty quick to me, what is your time goal? Colorize would be my go-to but I accept that 2.5s is too slow! $\endgroup$ – Carl Lange Oct 12 '18 at 18:11
  • $\begingroup$ @CarlLange perhaps my question is not well enough defined. My fast solutions seemed roundabout and less obvious to attempt. The obvious looking solutions are slow. I thought perhaps I was overlooking something. $\endgroup$ – Chip Hurst Oct 12 '18 at 18:13
  • $\begingroup$ Can't test right now, but I remember doing this by taking the Outer[] product of the binary image's ImageData[] with the RGB triplet of the new color (treated as a vector). $\endgroup$ – J. M. is computer-less Oct 12 '18 at 18:29
ImageMultiply[im, Green] // AbsoluteTiming 

enter image description here

versus 0.031786 for colorizeMask4 (fastest of the five in OP).

  • $\begingroup$ Thank you. I had tried im * Green thinking * would be equivalent to ImageMultiply in this case! $\endgroup$ – Chip Hurst Oct 12 '18 at 19:04
  • $\begingroup$ @ChipHurst, my pleasure. Thank you for the accept. $\endgroup$ – kglr Oct 12 '18 at 19:05
  • $\begingroup$ Also note that this is a 'Basic Example' in the ImageMultiply docs! $\endgroup$ – Chip Hurst Oct 12 '18 at 19:07
  • $\begingroup$ @Chip, although I almost always use this form, somehow today it was the last one I tried:) $\endgroup$ – kglr Oct 12 '18 at 19:09
  • 2
    $\begingroup$ It also works with ImageSubtract: ImageSubtract[im, RGBColor[1, 0, 1]] $\endgroup$ – C. E. Oct 12 '18 at 19:15

Alternatively, ColorCombine the mask with an all-black image:

im = Binarize[RegionImage[Annulus[], RasterSize -> 1024]];
black = ImageMultiply[im, 0];
ColorCombine[{black, im, black}] // AbsoluteTiming

this takes 0.001806 vs 0.019607 for colorizeMask4


Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.