2
$\begingroup$

I want to find the mean color of an image's region defined by a polygon, and I tried this:

lena = Import["lena.tif"]
poly = Polygon[{{10, 10}, {120, 10}, {100, 100}}];

imgDim = ImageDimensions[lena];
imgBounds = Transpose[{{0, 0}, imgDim}];
mask = Image[Graphics[{White, poly}, Background -> Black, 
             ImageSize -> imgDim, PlotRange -> imgBounds]]

ImageMeasurements[lena, Masking -> mask, "Mean"]

But when I run this I get an error

ImageMeasurements: Masking -> [the mask image] is not a known image measurements property.

ImageMeasurements takes the Masking option, so I expected there was something wrong with my masking image. But changing to Masking -> All still gives the same error. Is the documentation wrong?
(I'm on version 11.2)

$\endgroup$
4
$\begingroup$

The following works well:

poly = Polygon[{{10, 10}, {120, 10}, {100, 100}}];

ImageMeasurements[lena, "Mean", Masking -> poly]

Masking requires the graphical primitive instead your image. The sequence of arguments in ImageMeasurements have to be [image, measured value, options]. It does not work otherwise.

$\endgroup$
  • $\begingroup$ OMG, now you make me look so dumb :-). Thanks for the tip. $\endgroup$ – stevenvh Nov 6 '19 at 5:28

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.