how to crop image into overlapping patches

10 views (last 30 days)
i have an image of 720x680 and i want to extract overlapping patches. each patch has size of 16x16 and the overlapped pixels is 10. does anyone know how to do this in Matlab.

Accepted Answer

Image Analyst
Image Analyst on 6 May 2020
Edited: Image Analyst on 6 May 2020
Try this:
% Read in image.
rgbImage = imread('peppers.png');
h1 = subplot(2, 1, 1);
imshow(rgbImage);
hLine = yline(h1, 1, 'Color', 'y', 'LineWidth', 1);
h2 = subplot(2, 1, 2);
[rows, columns, numColorChannels] = size(rgbImage);
stepSize = 10;
subImageWidth = 16;
for row = 1 : stepSize : rows
row2 = min(row + subImageWidth - 1, rows);
% Put a yellow line over the full size image to let us know where it's at.
delete(hLine); % Delete old yellow line.
hLine = yline(h1, row2, 'Color', 'y', 'LineWidth', 1);
for col = 1 : stepSize : columns
col2 = min(col + subImageWidth - 1, columns);
subImage = rgbImage(row:row2, col:col2, :);
imshow(subImage);
drawnow;
end
end
  8 Comments
sotiraw sotiroglou
sotiraw sotiroglou on 28 Jun 2020
I am trying to do the same with the sliding patches, but i need it to be done only for a part of an image. For example a region of image defined by a polygon inside image.
Lets say that somehow or someway we have this region by some coordinates of its nodes. For example if the region is a triangle , we have the cooedinates of the 3 nodes. If its a polygon , more.
How could i aply this only inside this polygon? I would appreciate your help
Image Analyst
Image Analyst on 28 Jun 2020
Use poly2mask() to create a mask from the coordinates,
mask = poly2mask(x, y, rows, columns);
where rows and columns are for the small sliding patch, not the full sized image. Then multiply it by the image patch,
grayImage = grayImage .* uint8(mask);
or use indexing,
grayImage(~mask) = 0; % Erase outside of mask
or use bsxfun()
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!