how to crop image into overlapping patches

4 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2020-5-6
编辑:Image Analyst 2020-5-6
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 个评论
sotiraw sotiroglou
sotiraw sotiroglou 2020-6-28
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 2020-6-28
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));

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by