Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Provided a coordinate $c_k$ in a two-dimensional image, how can I return all of the pixels in a bounding disk or rectangle centered on $c_k$ of some specified dimension $d$?

Update: Hat tip to Kuba for recommending PixelValue and Sjoerd C. de Vries for recommending ImageData. However, considering the case of the bounding box, is it possible to return the pixels using:

           {Subscript[x, min];;Subscript[x, max],Subscript[y, min];;Subscript[y, max]}] 

In the form of a matrix rather than a 1-dimensional array?

share|improve this question
Take a look at PixelValue. – Kuba Jun 25 '13 at 21:27
Or ImageData. – Sjoerd C. de Vries Jun 25 '13 at 21:32
@Kuba Terrific, thanks! – Bob Jun 25 '13 at 21:33
About returning a matrix: how would a circular matrix look like? – Sjoerd C. de Vries Jun 25 '13 at 22:01
@SjoerdC.deVries Apologies, I meant for the bounding box with some $x_{min}$ to $x_{max}$ and $y_{min}$ to $y_{max}$ specified with the indicated command. – Bob Jun 25 '13 at 22:02

In order to return a square two-dimensional matrix of pixels with edge dimension $2*k+1$ centered at some point $(c_x,c_y)$ - i.e. we grab $k$ pixels to the right, to the left, above, and below $(c_x,c_y)$ - we can write:

data = PixelValue[image, {cx - k ;; cx + k, cy - k ;; cy + k}];
TempArray = Partition[data, 2*k + 1];

This can easily be generalized to a rectangular matrix with edge dimensions $k_1 \times k_2$ by writing:

data = PixelValue[image, {cx - k1 ;; cx + k1, cy - k2 ;; cy + k2}];
TempArray = Partition[data, 2*k1 + 1];

One can also trivially change the offset by slightly modifying the above approach.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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