fun[i_Image, rect_List] :=
ImageMeasurements[i, "MeanIntensity",
Masking -> SparseArray[{# -> 1}, ImageDimensions[i]]] & /@
Catenate@CoordinateBoundingBoxArray[rect]
Usage:
i = ExampleData[{"TestImage", "Lena"}];
fun[i, {{2, 3}, {4, 5}}]
{0.635294, 0.631373, 0.635294, 0.635294, 0.631373, 0.635294,
0.635294, 0.631373, 0.635294}
Update:
I'm very sorry I make a mistake in the Image Coordinate Systems,And the code above serve a Index Coordinates.So we cann't use SparseArray
as a mask unless you have a need in Index Coordinates.But this time let's face up to a Pixel-aligned Image Coordinates
When your object is a list of pixel:
fun1[i_Image, pos_List] :=
ImageMeasurements[i, "MeanIntensity",
Masking ->
ReplacePixelValue[
ConstantImage[0, ImageDimensions[i]], {# -> 1}]] & /@ pos
Usage:
fun1[i, {{1, 1}(*your test case*), {5, 6}, {10, 10}}]
fun1[ig, {{1, 1}(*your test case*), {5, 6}, {10, 10}}]
{0.172549, 0.211765, 0.247059}
{0.172549, 0.211765, 0.247059}
If your object is a region of pixel rectange:
fun2[i_Image, rect_List] :=
ImageMeasurements[i, "MeanIntensity",
Masking ->
ReplacePixelValue[
ConstantImage[0, ImageDimensions[i]], {# -> 1}]] & /@
Catenate@CoordinateBoundingBoxArray[rect]
Usage:
fun2[i, {{1, 1}, {5, 8}}]
{0.172549, 0.172549, 0.168627, 0.180392, 0.211765, 0.188235,
0.184314, 0.188235, 0.172549, 0.172549, 0.168627, 0.180392, 0.211765,
0.188235, 0.184314, 0.188235, 0.215686, 0.215686, 0.196078, 0.196078,
0.188235, 0.223529, 0.215686, 0.207843, 0.2, 0.2, 0.184314, 0.196078,
0.184314, 0.203922, 0.207843, 0.192157, 0.211765, 0.211765, 0.211765,
0.203922, 0.207843, 0.211765, 0.196078, 0.207843}
Span
specification to aTake
specification! Working on this right now. $\endgroup$ColorConvert
got updated in v10.f[i_Image, c_List] := ImageTake[i, Sequence @@ (Replace[ c, {h___, n_Integer, t___} :> {h, {n}, t}] /. Span -> List)] // ColorConvert[#, "Grayscale"] & // ImageData
I'm afraid, taking a span specification forces me to use a very clumsy replacement rule. $\endgroup$Take
kind of part specification looks like this:f[i_, c__] := ImageData@ColorConvert[ImageTake[i, c], "Grayscale"]
$\endgroup$