# Undesired resizing with ImageMultiply and Masking

I want to systematically measure image intensity around each of the points in this 256x256 image.

i0:

I already have centroid locating code to figure out the centers of each of these points. To measure the intensity around each point within a set radius I was planning on using ImageMeasurements with a systematically created Mask using Graphics.

The masking graphic for a particular point:

mask = Graphics[{Black, Rectangle[{0, 0}, {256, 256}], White,
Disk[Abs[{0, 256} - pos], 7]}];


Where pos is the position of the particular point of interest, in this case {66.6664, 8.74333}. The Abs[{0, 256} - pos] is to get the disk in the disk in the correct position due to different image indexing and graphic indexing.

i1:

Next I planned to use

ImageMeasurements[i0,"Mean",Masking->i1]


However to check whether the mask was measuring what I thought it was I took a look at ImageMultiply. What worries me is this: when I do ImageMultiply[i0,i1] the result is:

i0 * i1

I found a work around by using

ImageMultiply[i0,Image[Show[i0,i1]]]


i0 * Image[Show[i0,i1]]

And indeed the two masks: i1 and Image[Show[i0,i1]] give different results with ImageMeasurements. I'm inclined to think the later mask gives the right answer. But I am wondering why ImageMultiply[i0,i1] produces such a noticeably undesired result. I found this article to be interesting, as it discusses how ImageSize can possibly mess things up because one function displays in "printer pixel" sizes (72 ppi) and the other in "screen resolution" pixel sizes.

Does anybody know what options I am missing with ImageMultiply[i0,i1]? Why it does not work correctly? Or know a better approach to my problem?

Much thanks

• please provide traj or modify your code so that it is not needed – chris May 2 '15 at 7:28
• The answer is simple: i1 // ImageDimensions is not the same as i0 // ImageDimensions – chris May 2 '15 at 7:35

The problem is caused by padding that is added around the Graphics scene by default.

You can disable it by setting PlotRangePadding to 0 or None:

mask = Graphics[{Black, Rectangle[{0, 0}, {256, 256}], White,
Disk[Abs[{0, 256} - pos], 7]}, PlotRangePadding -> 0]

• @DarrenM if an answer solves your problem you should mark it as "accepted" it by clicking the gray check mark (✓) on the left side of the answer. – shrx May 3 '15 at 8:35
mask2 = mask // ImageResize[#, ImageDimensions[img]] &;


(* {256,256} *)

ImageMultiply[img, mask2]


ImageMeasurements[img, "Mean", Masking -> mask2]


(* {0.281368,0.281368,0.281368} *)

• Thank you Chris. But, that doesn't work for me for some reason. When I use the i1 graphics mask I get 0.281 as you did. And when I use the i0 * Image[Show[i0,i1]] mask I get 0.339. – DarrenM May 2 '15 at 8:12
• Thank you Chris. But, that doesn't work for me for some reason. When I use the i1 graphics mask I get 0.281 as you did. And when I use the i0 * Image[Show[i0,i1]] mask I get 0.339. When I use the resize function you described I get 0.281, and when I use the ImageMultiply[i0, "resized" image], I get the same weird border like in the i0 * i1 image I posted above. It seems odd that the code gave you the correct mask (which it didn't for me) but the same ImageMeasurement as the incorrect mask (i1). Thoughts? – DarrenM May 2 '15 at 8:18