0

I have two vectors: x and y where corresponding rows contain the coordinates of pixels in an image.

I also have a variable rad that defines a square window with dimensions 2*rad+1 by 2*rad+1

If x and y are scalars,

patch = img(y - rad : y + rad, x - rad : x + rad);

produces the 2*rad+1 by 2*rad+1 square window around the point (y,x).

However, as I said above, my x and y are vectors. Is there a way to get the windows around each coordinate without just looping through the vector.

That is, I want to avoid this:

patch = zeros(2*rad+1, 2*rad+1, length(x));
for i = 1 : length(x)
    patch(:,:,i) = img(y(i) - rad : y(i) + rad, x(i) - rad : x(i) + rad);
end

Is it possible to do this in a vectorized fashion?

marcman
  • 3,233
  • 4
  • 36
  • 71
  • My bad--my search terms didn't match the other question I guess. On a different note: @rayeng--man, you're prolific in image-processing – marcman Dec 18 '15 at 06:20
  • how's it going buddy? Yeah I remember answering this kind of question in the past, which is why I marked it as a duplicate. I probably should have left you a comment with an explanation of the duplicate :)... in any case, if you look at the answers, a `for` loop is actually the fastest approach... you're pretty solid with using a `for` loop, but there are vectorized implementations available as you can see in the post. BTW, thanks for the kudos :) Image Processing is what I love to do and I do it everyday. Good luck! – rayryeng Dec 18 '15 at 08:47

0 Answers0