Finding cropped region coordinates

6

1

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

Posted 2016-10-14T09:39:33.600

Reputation: 4 128

First time know this.Which version start to support this? – yode – 2016-10-14T13:44:06.337

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 – 2018-07-01T23:48:23.833

@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 – 2018-07-09T07:23:21.877

Answers

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

Sumit

Posted 2016-10-14T09:39:33.600

Reputation: 14 846