我有一大组图像,我希望从中提取图像补丁。补丁的大小是统一的,并在规则网格点从每个图像中提取。我可以使用以下代码执行此操作:
for n = 1:nImages
% Read image
imageFile = imageFiles{n};
I = imread(imageFile);
% Grid point locations
height = size(I, 1);
width = size(I, 2);
border = floor(patchSize/2);
centres = gridPoints(height, width, nPointsX, nPointsY, border);
% Extract and process patches
for p = 1:nPatches
% Patch location
x = centres(p, 1);
y = centres(p, 2);
% Top-left point of patch
x = x - floor(patchSize/2) + 1;
y = y - floor(patchSize/2) + 1;
% Extract patch -- BOTTLENECK!
patch = imcrop(I, [x y patchSize-1 patchSize-1]);
% Process patch
% ...
end
end
这段代码效率很低,尤其是考虑到大量图像和大量网格点(我也在为每个图像以不同的比例执行此操作)。我已经运行了 Matlab 的分析器,发现 imcrop 是导致这种低效率的原因。仅在 50 张图像上运行(但在 3 个尺度上具有 100 x 100 网格点)耗时 756 秒。
是否有另一种方法可以在 Matlab 中提取图像补丁而不会产生如此巨大的处理开销?