I have a point in an image which I can locate by right clicking and using my mouse. Call this point $p = (100,100)$. I'd like to use ImageTake to grab an image, centered on this spot $p$ with $k = 10$ pixels above, below, to the right, and to the left of $p$. Unfortunately, it seems like ImageTake is switching coordinates on me somehow.

How might I accomplish this task?


For example, to get the eye of the mandrill example image:

image = ExampleData[{"TestImage", "Mandrill"}]
indices = {56, 176};
k = 50;
ImageTake[image, {indices[[1]] - k, 
  indices[[1]] + k}, {indices[[2]] - k, indices[[2]] + k}]

Indices were obtained by right clicking on the image and choosing "Get Indices".

Regarding how the coordinate system works, the difference between coordinates and indices are that the indice origin is in the top left corner, whereas the coordinate origin is in the bottom left corner.


So in order to use coordinates, we can do this:

 image, {
   } - {cor[[2]] + k, cor[[2]] - k}, {cor[[1]] - k, 
  cor[[1]] + k}]

This code manually transforms the coordinates into indices. But ImageTake says that if the row number is negative, it starts from the bottom. That's exactly how coordinates work, so we can simply do this:

ImageTake[image, -{cor[[2]] + k, 
   cor[[2]] - k}, {cor[[1]] - k, cor[[1]] + k}]
| improve this answer | |
  • $\begingroup$ Is there an automatic command to pull the indices? $\endgroup$ – B.H. Jul 1 '13 at 0:38
  • $\begingroup$ Given the coordinates? $\endgroup$ – C. E. Jul 1 '13 at 0:39
  • $\begingroup$ Yes, if that's possible. $\endgroup$ – B.H. Jul 1 '13 at 0:40
  • $\begingroup$ Say I gave you the coordinates {333,458}... could you get me the eye in the right orientation with a box of side length 2k+1? $\endgroup$ – B.H. Jul 1 '13 at 0:41
  • $\begingroup$ Updated with code that works for coordinates. $\endgroup$ – C. E. Jul 1 '13 at 0:54

Don't use ImageTake if your coordinates are (x;y) image coordinates and not (row;col) positions. ImageTrim[image, {{x,y}}, 10] works as expected.

You may want to check out the section "Coordinate Systems" from this tutorial.

| improve this answer | |

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.