How do I efficiently extract image patches at specified locations?

user570593

I need to extract image patches of size s x s x 3 around specified 2D locations from an image (3 channels).

How can I do this efficiently without a for loop? I know I can extract one patch around (x,y) location as:

apatch = I(y-s/2:y+s/2, x-s/2:x+s/2, :)

How can I do this for many patches? I know I can use MATLAB's function blockproc but I can't specify the locations.

rayryeng

You can use im2col from the image processing toolbox to transform each pixel neighbourhood into a single column. The pixel neighbourhoods are selected such that each block is chose on a column-basis, which means that the blocks are constructed by traversing down the rows first, then proceeding to the next column and getting the neighbourhoods there.

You call im2col this way:

B = im2col(A, [M N]);

I'm assuming you'll want sliding / overlapping neighbourhoods and not distinct neighbourhoods, which are what is normally used when performing any kind of image filtering. A is your image and you want to find M x N pixel neighbourhoods transformed as columns. B would be the output where each neighbourhood is a single column and horizontally-tiled together. However, you'll probably want to handle the case where you want to grab pixel neighbourhoods along the borders of the image. In this case, you'll want to pad the image first. We're going to assume that M and N are odd to allow the padding to be easier. Specifically, you want to be sure that there are floor(M/2) rows padded on top of the image as well as the bottom as well as floor(N/2) columns padded to the left of the image as well as the right. As such, we should pad A first by using padarray. Let's assume that the border pixels will be replicated, which means that the padded rows and columns will simply be those grabbed from the top or bottom row, or the left and right column, depending on where we need to pad. Therefore:

Apad = padarray(A, floor([M N]/2), 'replicate');

For the next part, if you want to choose specify neighbourhoods, you can use sub2ind to convert your 2D co-ordinates into linear indices so you can select the right columns to get the right pixel blocks. However, because you have a colour image, you'll want to perform im2col on each colour channel. Unfortunately, im2col only works on grayscale images, and so you'd have to repeat this for each channel in your image.

As such, to get ready for patch sampling, do something like this:

B = arrayfun(@(x) im2col(Apad(:,:,x), [M N]), 1:size(A,3), 'uni', 0);
B = cat(3, B{:});

The above code will create a 3D version of im2col, where each 3D slice would be what im2col produces for each colour channel. Now, we can use sub2ind to convert your (x,y) co-ordinates into linear indices so that we can choose which pixel neighbourhoods we want. Therefore, assuming your positions are stored in vectors x and y, you would do something like this:

%// Generate linear indices
ind = sub2ind([size(A,1) size(A,2)], y, x);

%// Select neighbourhoods
%// Should be shaped as a MN x len(ind) x 3 matrix
neigh = B(:,ind,:);

%// Create cell arrays for each patch
patches = arrayfun(@(x) reshape(B(:,x,:), [M N 3]), 1:numel(ind), 'uni', 0);

patches will be a cell array where each element contains your desired patch at each location of (x,y) that you specify. Therefore, patches{1} would be the patch located at (x(1), y(1)), patches{2} would be the patch located at (x(2), y(2)), etc. For your copying and pasting pleasure, this is what we have:

%// Define image, M and N here
%//...
%//...

Apad = padarray(A, floor([M N]/2), 'replicate');
B = arrayfun(@(x) im2col(Apad(:,:,x), [M N]), 1:size(A,3), 'uni', 0);
B = cat(3, B{:});

ind = sub2ind([size(A,1) size(A,2)], y, x);
neigh = B(:,ind,:);
patches = arrayfun(@(x) reshape(neigh(:,x,:), [M N 3]), 1:numel(ind), 'uni', 0);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do i know locations of google street view image?

From Dev

How do I efficiently created a BW mask for this microscopic image?

From Dev

How do I efficiently created a BW mask for this microscopic image?

From Dev

How do I create a grid of patches in NetLogo

From Dev

How do I apply software patches?

From Dev

How do I create a grid of patches in NetLogo

From Dev

How to crop same size image patches with different locations from a stack of images?

From Dev

How to efficiently get all elements along 3rd direction at specified locations in a matrix?

From Dev

How to efficiently get all elements along 3rd direction at specified locations in a matrix?

From Dev

How do I extract bytes with offsets from a huge block efficiently in Python?

From Dev

How do I extract every consecutive pair of lines at a time from a file efficiently?

From Dev

How do I bind uniform locations in GLSL?

From Dev

Mercurial Queues: How to reorder patches efficiently

From Java

Extract N number of patches from an image

From Dev

Extract arbitrary rectangular patches from image in python

From Dev

Extract arbitrary rectangular patches from image in python

From Dev

how do I place my turtles within a square of 5 x 5 patches and 10 x 10 patches?

From Dev

How can I efficiently render an image in Django?

From Dev

How do I build a kernel using patches from LKML?

From Dev

How do I efficiently move a large, empty disk image to another system

From Dev

Extracting patches of a certain size from the image in python efficiently

From Dev

Extracting patches of a certain size from the image in python efficiently

From Dev

how do I efficiently test this Django model?

From Java

How do I debug efficiently with spyder in Python?

From Dev

How do I efficiently replace a function with a lookup?

From Dev

How do I efficiently block advertisements on a Mac?

From Dev

How do I extract a portion of an image in canvas and use it as background image for a div?

From Dev

How do I make an Image appear for a specified amount of time when a button is Clicked? In eclipse

From Dev

How do I see the memory locations of static variables within .bss?

Related Related

  1. 1

    How do i know locations of google street view image?

  2. 2

    How do I efficiently created a BW mask for this microscopic image?

  3. 3

    How do I efficiently created a BW mask for this microscopic image?

  4. 4

    How do I create a grid of patches in NetLogo

  5. 5

    How do I apply software patches?

  6. 6

    How do I create a grid of patches in NetLogo

  7. 7

    How to crop same size image patches with different locations from a stack of images?

  8. 8

    How to efficiently get all elements along 3rd direction at specified locations in a matrix?

  9. 9

    How to efficiently get all elements along 3rd direction at specified locations in a matrix?

  10. 10

    How do I extract bytes with offsets from a huge block efficiently in Python?

  11. 11

    How do I extract every consecutive pair of lines at a time from a file efficiently?

  12. 12

    How do I bind uniform locations in GLSL?

  13. 13

    Mercurial Queues: How to reorder patches efficiently

  14. 14

    Extract N number of patches from an image

  15. 15

    Extract arbitrary rectangular patches from image in python

  16. 16

    Extract arbitrary rectangular patches from image in python

  17. 17

    how do I place my turtles within a square of 5 x 5 patches and 10 x 10 patches?

  18. 18

    How can I efficiently render an image in Django?

  19. 19

    How do I build a kernel using patches from LKML?

  20. 20

    How do I efficiently move a large, empty disk image to another system

  21. 21

    Extracting patches of a certain size from the image in python efficiently

  22. 22

    Extracting patches of a certain size from the image in python efficiently

  23. 23

    how do I efficiently test this Django model?

  24. 24

    How do I debug efficiently with spyder in Python?

  25. 25

    How do I efficiently replace a function with a lookup?

  26. 26

    How do I efficiently block advertisements on a Mac?

  27. 27

    How do I extract a portion of an image in canvas and use it as background image for a div?

  28. 28

    How do I make an Image appear for a specified amount of time when a button is Clicked? In eclipse

  29. 29

    How do I see the memory locations of static variables within .bss?

HotTag

Archive