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
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
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
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 Center 및 File Exchange에서 Graphics Performance에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!