6

Mathematica now includes support for fairly involved interactive manipulation of displayed graphics, which includes cropping (ctrl-drag). Is there some way to get the crop dimensions set by the user, so that they can be read out dynamically?

Alternatively, is there a way to disable the Drawing Tools cropping and replace this ctrl-drag cropping behavior with a custom implementation? What I'm after are the actual dispayed pixel coordinates in image space.

The following code allows region-of-interest selection using row/column selection:

DynamicModule[{img, w, h},
 img = ExampleData[{"TestImage", "Lena"}];
 {w, h} = ImageDimensions@img; 
 Manipulate[
  Labeled[ImageAdjust@ImageTake[img, {row1, row2}, {col1, col2}], 
   ToString[{{row1, row2}, {col1, col2}}], Top], {{row1, 1}, 1, row2, 
   1}, {{row2, w}, row1 + 1, w, 1}, {col1, 1, col2, 1}, {{col2, h}, 
   col1 + 1, h, 1}]]

enter image description here

It would be nice to be able to read in the crop coordinates here and make the display behave consistently when ctrl-drag is used to crop the image drawn by Manipulate.

Possibly helpful related question: 5568

dionys
  • 4,321
  • 1
  • 19
  • 46
  • First time know this.Which version start to support this? – yode Oct 14 '16 at 13:44
  • I can't seem to get cropping to work. Documentation doesn't help much as it tells me to "Null a handle" which suggests a bug in the documentation creation. Anyway: Mac/v11.2. [I did try ctrl-drag and cmd-drag but, nope] – fairflow Jul 01 '18 at 23:48
  • @fairflow - You're better off posting a new question with more detail about what isn't working and what you've tried if the answers here and on related questions aren't helping you. – dionys Jul 09 '18 at 07:23

1 Answers1

5

Something like this

img = ExampleData[{"TestImage", "Lena"}];
{w, h} = ImageDimensions@img;

Manipulate[ Grid[{{Show[img, Graphics[{Opacity[0.5], Rectangle @@ pt}], ImageSize -> 300], Show[ImageTake[img, Reverse[h - pt[[All, 2]]], pt[[All, 1]]], ImageSize -> UpTo[300], PlotLabel -> {{"Coordinate", "Pixel"}, {Grid[pt, Frame -> All], Grid[{Reverse[h - pt[[All, 2]]], pt[[All, 1]]}, Frame -> All]}}]}}] , {{pt, {{0, 0}, {w, h}/2}}, Locator}]

Note: Be aware that this code uses an oriented rectangle, so the cropped image can be flipped and/or mirrored.

enter image description here

John
  • 2,429
  • 3
  • 17
  • 16
Sumit
  • 15,912
  • 2
  • 31
  • 73