---
title: "ImageApply"
language: "en"
type: "Symbol"
summary: "ImageApply[f, image] applies the function f to the list of channel values for each pixel in image. ImageApply[f, {image1, image2, ...}] applies f to the sequence of corresponding pixel values taken from each imagei."
keywords: 
- image apply
- image transform
- intensity transform
- color transform
- point processing
- point operator
- color processing
- pixel
- pixel operator
- region of interest
- roi
- image thread
- image map thread
- stack processing
- pixelwise operation
- pixel-wise operation
- uintlut
canonical_url: "https://reference.wolfram.com/language/ref/ImageApply.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "Basic Image Manipulation"
    link: "https://reference.wolfram.com/language/guide/BasicImageManipulation.en.md"
  - 
    title: "Image Composition"
    link: "https://reference.wolfram.com/language/guide/ImageComposition.en.md"
  - 
    title: "Color Processing"
    link: "https://reference.wolfram.com/language/guide/ColorProcessing.en.md"
  - 
    title: "3D Images"
    link: "https://reference.wolfram.com/language/guide/3DImages.en.md"
  - 
    title: "Image Computation: Update History"
    link: "https://reference.wolfram.com/language/guide/ImageComputation-UpdateHistory.en.md"
related_functions: 
  - 
    title: "ImageApplyIndexed"
    link: "https://reference.wolfram.com/language/ref/ImageApplyIndexed.en.md"
  - 
    title: "ImageScan"
    link: "https://reference.wolfram.com/language/ref/ImageScan.en.md"
  - 
    title: "ImageAdd"
    link: "https://reference.wolfram.com/language/ref/ImageAdd.en.md"
  - 
    title: "ImageMultiply"
    link: "https://reference.wolfram.com/language/ref/ImageMultiply.en.md"
  - 
    title: "ImageFilter"
    link: "https://reference.wolfram.com/language/ref/ImageFilter.en.md"
  - 
    title: "ColorNegate"
    link: "https://reference.wolfram.com/language/ref/ColorNegate.en.md"
  - 
    title: "ColorConvert"
    link: "https://reference.wolfram.com/language/ref/ColorConvert.en.md"
  - 
    title: "Map"
    link: "https://reference.wolfram.com/language/ref/Map.en.md"
  - 
    title: "MapThread"
    link: "https://reference.wolfram.com/language/ref/MapThread.en.md"
related_tutorials: 
  - 
    title: "Image Processing"
    link: "https://reference.wolfram.com/language/tutorial/ImageProcessing.en.md"
---
# ImageApply

ImageApply[f, image] applies the function f to the list of channel values for each pixel in image.

ImageApply[f, {image1, image2, …}] applies f to the sequence of corresponding pixel values taken from each imagei.

## Details and Options

* ``ImageApply[f, image]`` implements an image point operator, where the same function is applied to every pixel of an image. Most color operations such as color conversion and quantization are point operators.

[image]

* ``ImageApply`` works with 2D and 3D images with any number of channels.

* In ``ImageApply[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 of ``image``. The function ``f`` is assumed to return channel values that are normally in the range 0 to 1.

* ``ImageApply[f, {image1, …, imagen}]`` implements a pixelwise image operator. The function ``f`` is supplied with a sequence of ``n`` arguments. »

* ``ImageApply[f, {image1, image2, …}]`` works with any number of commensurate images and gives an image with the largest type of ``imagei``, clipping or truncating values if necessary.

* The following options can be given:

|               |      |                                                                                      |
| ------------- | ---- | ------------------------------------------------------------------------------------ |
| Interleaving  | True | whether to apply f to the list of channel values or separately to each channel value |
| Masking       | All  | region of interest to be processed                                                   |

* Evaluations of ``f`` may be cached to focus on speed of getting a result.  »

* ``ImageApply````[f, image]`` returns an image of the same type as ``image``.

---

## Examples (34)

### Basic Examples (2)

Replace each pixel with the maximum of the color channels:

```wl
In[1]:= ImageApply[Max, [image]]

Out[1]= [image]
```

---

Compute the average of three color images:

```wl
In[1]:=
f[pixel1_, pixel2_, pixel3_] := (pixel1 + pixel2 + pixel3/3);
ImageApply[f, {[image], [image], [image]}]

Out[1]= [image]
```

### Scope (8)

Color negate a grayscale image:

```wl
In[1]:= ImageApply[1 - #&, [image]]

Out[1]= [image]
```

---

Color negate an RGB image:

```wl
In[1]:= ImageApply[1 - #&, [image]]

Out[1]= [image]
```

---

Reverse the elements of each channel vector:

```wl
In[1]:=
ImageApply[(⁠|   |   |   |
| - | - | - |
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |⁠).#&, [image]]

Out[1]= [image]
```

---

Replace each RGB pixel by the maximum of all channel values:

```wl
In[1]:= ImageApply[Max, [image]]

Out[1]= [image]
```

---

Compute the maximum of corresponding pixel values in a list of grayscale images:

```wl
In[1]:= ImageApply[Max, {[image], [image], [image], [image], [image], [image]}]

Out[1]= [image]
```

---

Apply a pure function to a sequence of pixel values in a list of RGB-color images:

```wl
In[1]:= ImageApply[#1 + 1.25 * #2 + #3&, {[image], [image], [image]}]

Out[1]= [image]
```

---

Apply a function to pixels of a 3D RGB image:

```wl
In[1]:= ImageApply[RandomInteger[]#&, [image]]

Out[1]= [image]
```

---

Combine two 3D images:

```wl
In[1]:= ImageApply[Max, {[image], [image]}]

Out[1]= [image]
```

### Options (4)

#### Interleaving (2)

Use ``Interleaving -> False`` when the function has to be applied to each color channel value separately:

```wl
In[1]:=
f[x_Real] := Piecewise[{{2x, x <= .5}, {2 - 2x, x > .5}}]
ImageApply[f, [image], Interleaving -> False]

Out[1]= [image]
```

---

With ``Interleaving -> False``, all images should have the same number of channels:

```wl
In[1]:= ImageApply[Max, {[image], [image]}, Interleaving -> False]
```

ImageApply::itlvimgs: With Interleaving -> False, all images should have the same number of channels.

```wl
Out[1]= ImageApply[Max, {[image], [image]}, Interleaving -> False]
```

Convert the single-channel image to an RGB image:

```wl
In[2]:= ImageApply[Max, {ColorConvert[[image], "RGB"], [image]}, Interleaving -> False]

Out[2]= [image]
```

#### Masking (2)

Negate pixels in the specified region of interest:

```wl
In[1]:= ImageApply[1 - #&, [image], Masking -> Graphics[Disk[]]]

Out[1]= [image]
```

---

Region-of-interest processing is not supported when applying a function to a list of images:

```wl
In[1]:= ImageApply[{##}&, {[image], [image], [image]}, Masking -> [image]]
```

ImageApply::imgsroi: The value of option Masking ->  should be All when the second argument is a list of images.

```wl
Out[1]= ImageApply[{##1}&, {[image], [image], [image]}, Masking -> [image]]
```

Include the mask as one of the images in the list:

```wl
In[2]:= ImageApply[If[Mean[#4] > 0, Most[{##}], ConstantArray[#1, 3]]&, Flatten@{[image], [image], [image], [image]}]

Out[2]= [image]
```

### Applications (11)

Apply a gamma correction:

```wl
In[1]:=
img = [image];
ImageApply[# ^ 1.6&, img]

Out[1]= [image]
```

Color negate the image:

```wl
In[2]:= ImageApply[1 - #&, img]

Out[2]= [image]
```

Covert to a single-channel image:

```wl
In[3]:= ImageApply[Mean, img]

Out[3]= [image]
```

---

Display a selected channel:

```wl
In[1]:= Manipulate[ImageApply[#[[channel]]&, [image]], {{channel, 1}, {1 -> "R", 2 -> "G", 3 -> "B"}}]

Out[1]= DynamicModule[«8»]
```

---

Use built-in color gradients to add color to an image:

```wl
In[1]:= ImageApply[List@@ColorData["AlpineColors", #]&, [image]]

Out[1]= [image]
```

---

Add an alpha channel to a grayscale image:

```wl
In[1]:= ImageApply[{#, Boole[# > .2]}&, [image]]

Out[1]= [image]

In[2]:= ImageChannels[%]

Out[2]= 2
```

---

Highlight a region in an image:

```wl
In[1]:= ImageApply[{1, 0, 0}&, [image], Masking -> [image]]

Out[1]= [image]
```

---

Compute the elementwise inverse of an image:

```wl
In[1]:= ImageApply[1 / #&, [image]]//ImageAdjust

Out[1]= [image]
```

---

Pixel-wise division of two images:

```wl
In[1]:= ImageApply[#1 / #2&, {[image], [image]}]//ImageAdjust//ColorNegate

Out[1]= [image]
```

---

Compute max intensity projection (MIP) on 3D CT data:

```wl
In[1]:= Import["ExampleData/CThead.tiff", "Image3D"]

Out[1]= [image]

In[2]:= ImageApply[Max, Import["ExampleData/CThead.tiff"]]

Out[2]= [image]
```

---

Create a saturated composite image:

```wl
In[1]:= ImageApply[Rescale[{##}]&, {[image], [image], [image]}]

Out[1]= [image]
```

---

Colorize the gradient's magnitude and orientation of an image:

```wl
In[1]:=
img = [image];
m = ImageAdjust@GradientFilter[img, 3];
o = ImageAdjust[GradientOrientationFilter[img, 3], {0, 0, 1}, {-Pi / 2, Pi / 2}];
Image[ImageApply[{#2, 1, #1}&, {m, o}], ColorSpace -> "HSB"]

Out[1]= [image]
```

---

Compute the so-called Robert's gradient image:

```wl
In[1]:=
i = [image];
ImageApply[Sqrt[#1^2 + #2^2]&, {ImageConvolve[i, (⁠|   |    |
| - | -- |
| 1 | 0  |
| 0 | -1 |⁠)], ImageConvolve[i, (⁠|    |   |
| -- | - |
| 0  | 1 |
| -1 | 0 |⁠)]}]

Out[1]= [image]
```

### Properties & Relations (2)

``ImageApply`` returns an image of the same data type as input:

```wl
In[1]:= i = Image[{{100}}, "Byte"]

Out[1]= [image]

In[2]:= ImageApply[1 + #&, i]//ImageType

Out[2]= "Byte"
```

Clipping and truncation will happen when necessary:

```wl
In[3]:= ImageApply[1 + #&, i]//ImageData

Out[3]= {{1.}}
```

Convert to a real type to avoid clipping and truncating:

```wl
In[4]:= ImageApply[1 + #&, Image[i, "Real"]]//ImageData

Out[4]= {{1.39216}}
```

---

In ``ImageApply[f, image]``, evaluations of ``f`` may be cached to focus on speed of getting a result:

```wl
In[1]:= gray = [image];
```

Single channel ``"Byte"`` images use cached evaluations:

```wl
In[2]:= ImageApply[$$x\unicode{f4a1}\text{Sqrt}[x]$$, gray]

Out[2]= [image]
```

This may affect the result if the specified function can give different results when applied to same inputs:

```wl
In[3]:= ImageApply[$$x\unicode{f4a1}\text{RandomReal}[]$$, gray]

Out[3]= [image]
```

Multichannel ``"Byte"`` images use cached evaluations with ``Interleaving -> False`` :

```wl
In[4]:= image = Image[RandomImage[1, {1024, 1024}, ColorSpace -> "RGB"], "Byte"];

In[5]:= res1 = ImageApply[x \[Function] Sqrt[x], image, Interleaving -> False];//RepeatedTiming

Out[5]= {0.013, Null}
```

Compare with the timing for an interleaved image when no caching is used:

```wl
In[6]:= res2 = ImageApply[x \[Function] Sqrt[x], image, Interleaving -> True];//RepeatedTiming

Out[6]= {0.11, Null}

In[7]:= res1 == res2

Out[7]= True
```

Multichannel ``"Byte"`` images use cached evaluations with ``Listable`` functions:

```wl
In[8]:= ImageApply[Function[x, Sqrt[x], Listable], image, Interleaving -> True];//RepeatedTiming

Out[8]= {0.014, Null}
```

### Possible Issues (3)

Functions that accept lists as their input cannot be applied on single-channel images:

```wl
In[1]:= ImageApply[Reverse, [image]]
```

ImageApply::bdf: Applying Reverse to 0.43529411764705883\` does not yield a number or list of numbers.

```wl
Out[1]= ImageApply[Reverse, [image]]
```

---

Functions should return a number or a list of numbers for every pixel in the image:

```wl
In[1]:= ImageApply[1 / #&, Image[{{2, 0, 2}}]]
```

ImageApply::bdf: Applying 1/\#1& to 0.\` does not yield a number or list of numbers.

```wl
Out[1]= ImageApply[(1/#1)&, [image]]
```

The same inverse function can be applied to an image with nonzero pixels:

```wl
In[2]:= ImageApply[1 / #&, Image[{{2, 1, 2}}]]

Out[2]= [image]
```

---

``ImageApply`` preserves the image data type, clipping or truncating values if necessary:

```wl
In[1]:= image = [image];

In[2]:= ImageApply[x \[Function] x + 0.4, Image[image, "Bit"]]

Out[2]= [image]

In[3]:= % == image

Out[3]= True
```

Apply the function to a ``"Real32"`` image instead:

```wl
In[4]:= ImageApply[x \[Function] x + 0.4, Image[image, "Real32"]]

Out[4]= [image]
```

### Neat Examples (4)

Rotate color channels:

```wl
In[1]:= ImageApply[RotateLeft, [image]]

Out[1]= [image]
```

---

Convert an image to grayscale while preserving the red color:

```wl
In[1]:= ImageApply[If[#[[1]] > #[[2]] + #[[3]], #, ConstantArray[Mean[#], 3]]&, [image]]

Out[1]= [image]
```

---

Apply any coloring function to an image:

```wl
In[1]:= ImageApply[TriangleWave, [image]]

Out[1]= [image]
```

---

Colorize a grayscale image:

```wl
In[1]:= ImageApply[ {Sin[Pi # * 4], Sin[Pi #], Sin[Pi# * 3]}&, [image]]

Out[1]= [image]
```

## See Also

* [`ImageApplyIndexed`](https://reference.wolfram.com/language/ref/ImageApplyIndexed.en.md)
* [`ImageScan`](https://reference.wolfram.com/language/ref/ImageScan.en.md)
* [`ImageAdd`](https://reference.wolfram.com/language/ref/ImageAdd.en.md)
* [`ImageMultiply`](https://reference.wolfram.com/language/ref/ImageMultiply.en.md)
* [`ImageFilter`](https://reference.wolfram.com/language/ref/ImageFilter.en.md)
* [`ColorNegate`](https://reference.wolfram.com/language/ref/ColorNegate.en.md)
* [`ColorConvert`](https://reference.wolfram.com/language/ref/ColorConvert.en.md)
* [`Map`](https://reference.wolfram.com/language/ref/Map.en.md)
* [`MapThread`](https://reference.wolfram.com/language/ref/MapThread.en.md)

## Tech Notes

* [Image Processing](https://reference.wolfram.com/language/tutorial/ImageProcessing.en.md)

## Related Guides

* [Basic Image Manipulation](https://reference.wolfram.com/language/guide/BasicImageManipulation.en.md)
* [Image Composition](https://reference.wolfram.com/language/guide/ImageComposition.en.md)
* [Color Processing](https://reference.wolfram.com/language/guide/ColorProcessing.en.md)
* [3D Images](https://reference.wolfram.com/language/guide/3DImages.en.md)
* [Image Computation: Update History](https://reference.wolfram.com/language/guide/ImageComputation-UpdateHistory.en.md)

## History

* [Introduced in 2008 (7.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn70.en.md) \| [Updated in 2012 (9.0)](https://reference.wolfram.com/language/guide/SummaryOfNewFeaturesIn90.en.md)