6
$\begingroup$

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

$\endgroup$
3
  • $\begingroup$ First time know this.Which version start to support this? $\endgroup$
    – yode
    Oct 14, 2016 at 13:44
  • $\begingroup$ 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] $\endgroup$
    – fairflow
    Jul 1, 2018 at 23:48
  • $\begingroup$ @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. $\endgroup$
    – dionys
    Jul 9, 2018 at 7:23

1 Answer 1

5
+50
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.