I am doing a study into image processing and motion detection. I am using various filters (ImageDifference, HighpassFilter, Binarize, etc.) to process the image and then ComponentMeasurements to determine the areas of interest. Having found these areas, I want to indicate them on the original image - to do this I have been using the HighlightImage function and it is working. However, with more than one coordinate to highlight, the function is really slow (one point = 0.15s, two or more points = 3s). One way I thought to speed it up was to do the single coordinate update recursively, but my code has a glitch that I can't see. Any help to speed this up would be gratefully received.

HighlightImageFast[image_, points_] := Module[{tempImage},
  tempImage = image;
  Do[tempImage = HighlightImage[tempImage, points[[n]],
     Method -> {"CrossMarkers", 50}, "HighlightColor" -> Blue],
   {n, Length[points]}];

N.b. I'm a home use licensee and currently have version

  • $\begingroup$ Hello, and welcome to Mathematica.SE. While editing your question for readability I have removed your signature, as it is not a custom to sign questions and answers on this site. People can see the authors in the bottom right corner of the respective messages. $\endgroup$ – shrx Sep 16 '15 at 15:49
  • $\begingroup$ I feel like HighlightImage with a list should be faster than you calling HighlightImage n times. If that really isn't the case, I'd replace your Do loop with Fold[HighlightImage[#1,#2,...options...]&,image,points]. Maybe you can provide some data so it's easier to help. $\endgroup$ – N.J.Evans Sep 16 '15 at 15:56
  • $\begingroup$ The timings from the docs for HighlightImage are quite reasonable. Perhaps your images are big ... $\endgroup$ – Dr. belisarius Sep 16 '15 at 16:32
  • $\begingroup$ Thanks guys,(gals). Fold isn't updating the image either, in both cases, I get the original image without the highlights. As for performance, if I have one point, it's 0.15s but with 2 it takes 20 times longer. My original plan was to apply HighlightImage n times where n<20 and then do HighlightImage with the list for n>20. The image I'm using is from a HD video so 1280 × 720. $\endgroup$ – Alan Woodward Sep 16 '15 at 17:41

I would strongly discourage the iterative processing: it seems to be MUCH slower than highlighting all points in one function call. Compare for instance the following:

lena = ExampleData[{"TestImage", "Lena"}];
points = RandomInteger[500, {250, 2}];

HighlightImage[lena, points]; // AbsoluteTiming
(* Out: {0.0555354, Null} *)

Fold[HighlightImage[#1, #2] &, lena, points]; // AbsoluteTiming
(* Out: {11.628, Null} *)

tempImage = lena;
  tempImage = HighlightImage[tempImage, points[[n]]],
  {n, Length[points]}
  ] // AbsoluteTiming
(* Out: {11.5296, Null} *)

As you can see, the iterative process takes roughly 200x longer than adding all highlights at once, so that will certainly NOT speed up your task.

Having said that, I did manage to crash both kernel and front end when trying to highlight even a few points in a much larger image (20M size). I suspect that your problems stem from the size of your images, and not from the fact that you are trying to add multiple highlights at once.

Update: Here is something else I noticed, but can't explain: the size of the markers influences the execution speed quite significantly! Compare the following examples:

fun := RepeatedTiming[HighlightImage[lena, points, Method -> {"CrossMarkers", #}];] &
fun /@ {2, 50, 150}

(*Out: {{0.061, Null}, {0.21, Null}, {1.5, Null}} *)
  • 1
    $\begingroup$ Cool info! Thanks. For reference, the image I was processing is from an HD video, 1280x720. If I use the HighlightImage with a single point {123,456} then the timing is about 100 times faster than a list of lists{{123,456}}, even with the list of lists containing the same single point. In[178]:= HighlightImage[lena, {123, 456}, Method -> {"CrossMarkers", 50}, "HighlightColor" -> Blue]; // AbsoluteTiming Out[178]= {0.010373, Null} In[179]:= HighlightImage[lena, {{123, 456}}, Method -> {"CrossMarkers", 50}, "HighlightColor" -> Blue]; // AbsoluteTiming Out[179]= {0.811417, Null} $\endgroup$ – Alan Woodward Sep 16 '15 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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