Wolfram Computation Meets Knowledge

Image Effects in Mathematica

Mathematica 9 has just been released with many new or enhanced capabilities for image processing. You can perform morphological operations, color manipulation, segmentation analysis, feature detection, and more, most of which can be applied to the new Image3D object as well.

A byproduct of this whole ecosystem is that now it is easier than ever to use Mathematica to create and apply effects to your images.

Defining the new image

Two Mathematica super functions that can be used to apply transformations directly to an image are ImageApply, which is a pixel operator, and ImageFilter, which considers the pixel as well as a neighborhood of pixels around it and works as a local filter.

For example, you can remove the blue channel and perform a gamma correction only on the green channel by doing the following:

myfunction[{r_, g_, b_}] := {r, g^1.5, 0} ImageApply[myfunction, image]

Image with the blue channel removed and a gamma correction only on the green channel

Or, design a custom filter to average the values in the blue channel only over a specified neighborhood range:

Designing a custom image filter

Using custom myfilter image filter

Filtered image

Some common functions are already included with an internally optimized version; such is the case for the mean filter, which is named, not unexpectedly, MeanFilter.

Using this in combination with the duo ColorSeparate/ColorCombine, we can write a much more readable and efficient version of the filter above.

{r, g, b} = ColorSeparate[image];

ColorCombine[{r, g, MeanFilter[b, 10]}]

New version of filtered image

Here is a subset of the other filters present in Mathematica 9.

Filters present in Mathematica 9

And here are Mathematica 9’s embedded image effects.

Embedded image effects in Mathematica 9

Mathematica‘s image processing functionality allows you to apply masks, for example, to create vignette effects. First we need to generate the mask. I am going for a simple shading at the corners, obtainable using a Gaussian mask.

gaussmask = ImageAdjust@Image[GaussianMatrix[{100, 150}]]

Gaussian mask

Now we only need to adjust the mask dimensions to the ones of our original image so that it is possible to combine them.

mymask = ImageResize[gaussmask, ImageDimensions[image]]; ImageMultiply[image, mymask]

Filtered image

We can even change the mask’s color.

redmask = ImageMultiply[mymask, Red]; ImageMultiply[image, redmask]

Filtered image with red mask

This procedure is very straightforward, but a bit rough. What if we want to perform some fine tuning of our color mixing?

The general idea would be to implement an expression like: (1 – α) mask + α color.

Or, if we want to maintain the pure black at the corners, simply: mask + α color.

We can do the above using image arithmetic functions such as ImageSubtract and ImageMultiply that are now updated to work with a list of channel values.

Using Manipulate to control the blend level

With a little more effort, it is possible to put many more effects together in a single, interactive panel.

Using the luminosity as parameter, I implemented a blending effect limited to the dark or light areas. With ImageAdjust we can fix contrast, brightness, and gamma, and ImageMultiply can easily handle applying textures.

ImageAdjust

As a final effort, I would like to create something totally different from what we have just seen: a filter that transforms my picture into something similar to a comic, with only black and white and a uniform filling pattern for the gray area.

I don’t want too much black in the image, so I try to convert our image into black and white with a low threshold. Then we need the part in the middle, the gray area; this we can get with Binarize too.

Using Manipulate to filter the image

Using Manipulate to filter the image

To generate the repetitive pattern that we will use to fill the gray area, we use the Mod function.

Using the Mod function to generate a repetitive pattern

As a final step, we use an AlphaChannel to make gray pixels fully transparent so that the pattern can be seen through the mask.

Show[  GrayMask[Reverse@ImageDimensions@image, 2],  SetAlphaChannel[black, gray]]

Filtered image

The beautiful thing in Mathematica is to be able to play interactively with your code. Here is a CDF where you can see the effect of a parameter change in real time. I also added a control to change the background pattern and an option to emphasize the edges in the image.

Parameter change

We have just seen how to create several basic image effects with Mathematica. Using these as building blocks, it is possible to reproduce the filters embedded in popular applications on the market like Instagram or Hipstamatic with a little more effort. The big difference with Mathematica is that you know exactly what’s going on behind the curtains, and amazing results can be achieved with only few lines of simple code.

Download this post as a Computable Document Format (CDF) file.

Comments

Join the discussion

!Please enter your comment (at least 5 characters).

!Please enter your name.

!Please enter a valid email address.

1 comment

  1. This is a very good topic and as well as elaborate description for beginners very well. Also step by step description help me for my knowledge and i shall share other.

    Reply