我有一大堆图像,我希望从中提取图像补丁.贴片尺寸均匀,并在常规网格点从每个图像中提取.我可以使用以下代码执行此操作:
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中提取图像补丁而不会产生如此巨大的处理开销?