Search code examples
matlabcrop

Matlab - Save the 32x32 centre of an imread image


So I'm reading in a bunch of images in Matlab.

The section of the image I'm interested in, is the central 32x32 most pixels (a small square in the centre of the actual image).

Once you've read the image in, how can I extract just a centre piece or 32x32 and store it in a new image variable?

Thanks in advance


Solution

  • If your images aren't RGB, this could be slightly simpler, but why not cover the more complex case:

    img = imread('image.png');
    sz = size(img);
    x = floor(sz(2)/2);
    y = floor(sz(1)/2);
    patch = img(y-15:y+16, x-15:x+16, :);