How to | Get Parts of an Image

Getting a rectangular part of an image is a very common and frequently needed image manipulation task. Several ways are presented here, including using the mouse.

Selected examples from the video:

The simplest way to get parts of an image is to use ImageCrop. By default, ImageCrop takes a subregion with the designated pixel width and height from the center of an image. Use ImageCrop to crop an image to pixel width 300 and pixel height 100:

In[2]:=
Click for copyable input
Out[2]=

A specific corner of the original image can be selected by cropping from the opposite corner. You can select the lower-left corner of an image by cropping the top-right portion:

In[3]:=
Click for copyable input
Out[3]=

Use ImageTake if you know the rows and columns you would like to extract from the original image. Take the first 100 rows of the image:

In[4]:=
Click for copyable input
Out[4]=

You can also use specific beginning and ending indices with ImageTake for precise region selection. Take the part of the image that occurs from rows 200 to 299, and columns 100 to 399:

In[5]:=
Click for copyable input
Out[5]=

ImageCrop and ImageTake complement each other, giving you a wide range of functionality. But what if you want to select the region of interest interactively, using a mouse or other pointing device? You can easily do this in a few quick steps.

With the mouse pointer positioned over an image, right-click and select Get Indices. The mouse pointer then changes into a cross with a tooltip next to it that indicates the current row and column position of the pointer.

Left-click the image in two locations, each representing diagonally opposite corners of the desired rectangular region. You can then simply Copy and Paste these coordinates into an input cell.

You can try this on the image here and then paste the coordinates into the next input:

In[10]:=
Click for copyable input
Out[10]=
Click for copyable input

Note that the coordinates are given in form:

In[1]:=
Click for copyable input

Thread or transpose the indices to convert the list to the form required by ImageTake. The sort operation then ensures that the row and column lists are in ascending order, regardless of the order in which you picked the opposing corners of the rectangle:

In[9]:=
Click for copyable input
Out[9]=

Now take the selected region:

In[10]:=
Click for copyable input
Out[10]=
New to Mathematica? Find your learning path »
Have a question? Ask support »