({{, (RefLink[ImageApplyIndexed, paclet:ref/ImageApplyIndexed])[f,image] 
applies the function f to the list of channel values for each pixel in image, giving the row and column index of each pixel as a second argument to f.}, {, (RefLink[ImageApplyIndexed, paclet:ref/ImageApplyIndexed])[f,{(Subscript[image, 1]),(Subscript[image, 2]),…}]
applies f to the sequence of corresponding pixel values taken from each (Subscript[image, i]), giving the corresponding row and column index of pixels as the last argument to f.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[ImageApplyIndexed, paclet:ref/ImageApplyIndexed])[f,image] replaces the list of channel values for each pixel by the result of applying the function f to the list.

In 2D, the part specification of each pixel is passed to f as {row,column}.

In 3D, the part specifications are specified with {slice,row,column}.

(RefLink[ImageApplyIndexed, paclet:ref/ImageApplyIndexed]) works on images with any number of channels.

(RefLink[ImageApplyIndexed, paclet:ref/ImageApplyIndexed])[f,image] returns an image of the same type as image.

In (RefLink[ImageApplyIndexed, paclet:ref/ImageApplyIndexed])[f,image], the function f can return a number or a list of any length.

The channel values supplied to f are normally in the range 0 to 1, regardless of the underlying type used in the (RefLink[Image, paclet:ref/Image]) object. The function f is assumed to return channel values that are normally in the range 0 to 1.

In (RefLink[ImageApplyIndexed, paclet:ref/ImageApplyIndexed])[f,{(Subscript[image, 1]),(Subscript[image, 2]),…,(Subscript[image, n])}], f is supplied with n+1 arguments.

(RefLink[ImageApplyIndexed, paclet:ref/ImageApplyIndexed])[f,{(Subscript[image, 1]),(Subscript[image, 2]),…}] works with any number of commensurate images.

(RefLink[ImageApplyIndexed, paclet:ref/ImageApplyIndexed])[f,{(Subscript[image, 1]),(Subscript[image, 2]),…}] gives an image with the largest type of (Subscript[image, i]), clipping or truncating values if necessary.

(RefLink[ImageApplyIndexed, paclet:ref/ImageApplyIndexed]) has the option (RefLink[Interleaving, paclet:ref/Interleaving]), which specifies whether f should be applied to individual channel values or to the complete list of channel values for a given pixel. The default setting is (RefLink[Interleaving, paclet:ref/Interleaving])->(RefLink[True, paclet:ref/True]).

(RefLink[ImageApplyIndexed, paclet:ref/ImageApplyIndexed]) works with (RefLink[Image3D, paclet:ref/Image3D]) objects.

OpenersPrimaryExamplesSection(Spacer1)Examples  (15)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Apply a pure function defined on the pixel's value and position:

ImageApplyIndexed[#1+Max[#2]/300&,]

Apply a function depending on the pixel's values and position to a list of images:

ImageApplyIndexed[Function[{pixel1,pixel2, pixel3,pixel4,pos},
Which[
Max[pos]<100,pixel1+.2,
Max[pos]>150,(pixel1+pixel2+pixel3)/3,
EuclideanDistance[pos,{125,125}]<20,.75-pixel4,
True,1-pixel3]],
{,,,Graphics@Disk[]}]

OpenersExampleSection1(Spacer1)Scope  (3)

Darken each pixel in an image irrespective of its position:

ImageApplyIndexed[#1-0.2&,]

Define luminance based on the pixel positions irrespective of their values:

ImageApplyIndexed[Max[#2]/100&,]

Negate pixels of the third slice of a 3D image:

ImageApplyIndexed[If[#2[[1]]==3,1-#1,#1]&,]

OpenersExampleSection2(Spacer1)Options  (3)

OpenersExampleSubsection0(Spacer1)Interleaving  (2)

Use (RefLink[Interleaving, paclet:ref/Interleaving])->(RefLink[False, paclet:ref/False]) when the function should be applied to color channels separately:

f[x_Real,pos_]:=Piecewise[{{2x,x<=.5},{2-2x,x>.5}}]*Total[pos]/200
ImageApplyIndexed[f,,Interleaving->False]

Use (RefLink[Interleaving, paclet:ref/Interleaving])->(RefLink[False, paclet:ref/False]) and a list of images when the function expects separate channel values:

f[x1_Real,x2_Real,pos_]:=With[{x=Max[x1,x2]},Piecewise[{{2x,x<=.5},{2-2x,x>.5}}]*Total[pos]/200]
ImageApplyIndexed[f,{,},Interleaving->False]

OpenersExampleSubsection1(Spacer1)Masking  (1)

Apply the function to the region of interest specified by a mask:

ImageApplyIndexed[If[OddQ[Max@#2],1-#1,#1]&,,Masking->Graphics[Disk[]]]

OpenersExampleSection3(Spacer1)Applications  (6)

Create a five-channel image where each resulting pixel represents a combination of RGB color and position:

img=;

ImageApplyIndexed[Flatten[{#1,#2}]&,img]

Normalize positions to be between 0 and 1 in each dimension:

ImageApplyIndexed[Flatten[{#1,#2/ImageDimensions[img]}]&,img]

Use pixel indices to add a 2D cosine wave to an image:

ImageApplyIndexed[#1+Total[Cos[#2 2π/30]]/5&,]

Use pixel positions to process each image quadrant differently:

img=;
{w,h}=ImageDimensions[img];
f[pixel_,pos:{row_,column_}]:=
Which[
1<=row<h/2&&1≤column<w/2,pixel,
row≥h/2&&1≤column<w/2,pixel-.2,
1<=row<h/2&&column≥w/2,pixel+.4,
True,1-pixel
];

ImageApplyIndexed[f,img]

Convert the image to grayscale as gradually moving from the center to the corners:

image=;

f=Compile[{{pixel,_Real,1},{pos,_Integer,1},{dim,_Integer,1},{γ,_Real}},
Module[{ρ,α},
ρ=Norm[pos-Reverse[dim]/2]/(Sqrt[Plus@@(dim^2)]/2);
α=ρ^γ;
(1-α)*pixel+α*Mean[pixel]
]
];
ImageApplyIndexed[f[#1,#2,ImageDimensions[image],1]&,image]

Vary the exponential rate of grayscale conversion:

Table[ImageApplyIndexed[f[#1,#2,ImageDimensions[image],γ]&,image],{γ,{.5,1,1.5}}]

Colorize an image where hue and saturation depend on the pixel's position and the brightness depends on the pixel value:

Image[ImageApplyIndexed[{First[#2]/256,Last[#2]/256,#1}&,],ColorSpace->"HSB"]

Create a filled gradient image by interpolating colors at the corners:

colors=List@@@{Red,Green,Blue,Yellow};

funcs=Table[
Interpolation[N@MapThread[{#1,#2}&,{{{1,1},{1,200},{200,1},{200,200}},colors[[All,i]]}],InterpolationOrder->1],{i,3}];
Table[ImageApplyIndexed[f[Sequence@@#2]#1&,],{f,funcs}]//ColorCombine

OpenersExampleSection4(Spacer1)Possible Issues  (1)

With (RefLink[Interleaving, paclet:ref/Interleaving])->(RefLink[False, paclet:ref/False]), all images should have the same number of channels:

ImageApplyIndexed[Max[{#1,#2}]&,{,},Interleaving->False]

Convert the single-channel image to an RGB image:

ImageApplyIndexed[Max[{#1,#2}]&,{,},Interleaving->True]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ImageApply, paclet:ref/ImageApply])  ▪  (RefLink[MapIndexed, paclet:ref/MapIndexed])  ▪  (RefLink[Map, paclet:ref/Map])  ▪  (RefLink[MapAt, paclet:ref/MapAt])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Basic Image Manipulation, paclet:guide/BasicImageManipulation])}})}})