4
$\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$
  • $\begingroup$ First time know this.Which version start to support this? $\endgroup$ – yode Oct 14 '16 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 '18 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 '18 at 7:23
3
+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 -> 300, 
     PlotLabel -> {{"Coordinate", "Pixel"}, {Grid[pt, Frame -> All], 
     Grid[{Reverse[h - pt[[All, 2]]], pt[[All, 1]]},  Frame -> All]}}]}}]
, {{pt, {{0, 0}, {h, w}/2}}, Locator}]

enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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