Function Repository Resource:

ImageLocatorPaneWithZoom

Source Notebook

Create an interface to accurately select points on an image

Contributed by: Sjoerd Smit

ResourceFunction["ImageLocatorPaneWithZoom"][Dynamic[pts], back]

creates a LocatorPane with a zoom area around the mouse.

Details and Options

ResourceFunction["ImageLocatorPaneWithZoom"] takes the same arguments and options as LocatorPane, but only accepts static images and Graphics as the second argument. Dynamic elements inside of Graphics are allowed as long as they do not change the size of the background.
In addition, the following options can be specified:
"ShowZoomControls"Truewhether the additional controls are visible when the interface is created
"ZoomLevel"2starting value of the magnification level of the zoom area
"ZoomSize"0.15starting value of the scaled zoom area size (relative to the size of the image)
"MarkerFunction"Automaticfunction that takes a color and returns Graphics[] object to show the selected points
"MarkerSize"0.02size of the point markers (relative to the size of the image)
"MarkerColor"Blackcolor of the selected points
"ShowCoordinates"Truewhether to show coordinates in the zoom area
The "MarkerFunction" option can be given any function that returns a Graphics object. The custom marker should be centered on the coordinate {0, 0}.
For images, the coordinates of the points will be determined by the Graphics object returned by Show[img]. Note that this system can depend on the ImageResolution of the image. Use Image[img,ImageResolutionAutomatic] to make the coordinates fun from 0 to the ImageDimensions of the image.
When the zoom level is <=1 and the coordinates have been disabled, the zoom box will not be shown.
The LocatorPane option AppearanceAutomatic can be used to draw the default locators, but this is not recommended since the locators will overlap with the zoom area.

Examples

Basic Examples (2) 

Get an image:

In[1]:=
img = ExampleData[{"TestImage", "Apples"}]
Out[1]=

Create an interface that allows you to select a point:

In[2]:=
Clear[pts];
ResourceFunction["ImageLocatorPaneWithZoom"][
 Dynamic[pts],
 Image[img, ImageSize -> Full]
 ]

Scope (2) 

Points can be added or deleted with + click:

In[3]:=
img = ExampleData[{"TestImage", "Apples"}];
pts = {{250, 327}, {430, 257}, {505, 134}};
ResourceFunction["ImageLocatorPaneWithZoom"][Dynamic[pts], Image[img, ImageSize -> Full],
  LocatorAutoCreate -> True,
 "ShowZoomControls" -> False, "ZoomLevel" -> 3,
 "ZoomSize" -> 0.2, "MarkerFunction" -> Function[
   Graphics[{#, PointSize[20], Point[{0, 0}]}, PlotRange -> All]],
 "MarkerSize" -> 0.02, "MarkerColor" -> Red, "ShowCoordinates" -> False
 ]
Out[5]=

Use Graphics with Dynamic elements to add extra content to the image:

In[6]:=
img = ExampleData[{"TestImage", "Apples"}];
pts = {};
ResourceFunction["ImageLocatorPaneWithZoom"][
 Dynamic[pts],
 Show[
  Image[img, ImageSize -> Full],
  Graphics[{EdgeForm[Red], FaceForm[None], Dynamic[Polygon[pts]]}]
  ],
 "ShowZoomControls" -> False,
  LocatorAutoCreate -> All
 ]
Out[7]=

Possible Issues (2) 

If you turn on the default locators with AppearanceAutomatic, the locators will be drawn in the frame of the main image, so they will not appear in the correct position in the zoom area:


The coordinates of the points are not always on a scale natural to the image. Consider the ImageDimensions of this example image:

In[8]:=
img = ExampleData[{"TestImage", "Apples"}];
ImageDimensions[img]
Out[9]=

When converted to Graphics, the coordinates run over a different range:

In[10]:=
AbsoluteOptions[Show[img], PlotRange]
Out[10]=

To get coordinates corresponding to pixels, use ImageResolutionAutomatic:

In[11]:=
AbsoluteOptions[
 Show[Image[img, ImageResolution -> Automatic]], PlotRange]
Out[11]=

Publisher

Sjoerd Smit

Version History

  • 1.0.0 – 27 June 2022

License Information