【发布时间】:2017-02-01 08:52:38
【问题描述】:
我需要将一个图像划分为 9 个相等或几乎相等的分区,并将每个分区存储到一个数组中。因此,最终结果将类似于数组数组,其中数组的每个元素都是一个 2x2 数组,表示图像的一个分区。到目前为止,我已经想出了以下代码
function [ outputImageRectangles ] = getImagePartitions( inputImage )
%Write the code to partition image into 9 equal or nearly equal size
%rectangles
[height, width] = size(inputImage);
partitions = zeros(3,3);
for i=0:2
for j=0:2
loweri = floor(i*height/3)+1;
higheri = floor((i+1)*height/3);
lowerj = floor(j*width/3)+1;
higherj = floor((j+1)*width/3);
x = i+1;
y = j+1;
loweri
higheri
lowerj
higherj
partitions(x,y,:,:) = inputImage(loweri:higheri, lowerj:higherj);
end
end
outputImageRectangles = partitions;
end
我假设分区过程工作正常,但我在将每个分区存储到数组元素中时遇到问题。我对matlab很陌生,只是想掌握它。我还阅读了一些关于可以包含另一个数组作为数组元素的单元数组。到目前为止,这段代码在
上给了我错误 partitions(x,y,:,:) = inputImage(loweri:higheri, lowerj:higherj);
显然是因为数组维度不匹配。我的问题是如何在不过度降低性能的情况下执行此任务?性能至关重要,因为将针对超过 12000 张图像调用此函数。
【问题讨论】:
-
我不明白
partitions的4D索引的目的,你能解释一下吗?最终结果不会是 9 张图像,尺寸可能不同吗?存储它的最佳方法是单元格数组,如partitions{x,y}=...,除非 4D 意味着我缺少的东西 -
它是 4D,因为我不知道如何正确操作。我尝试了
partitions(x,y,:),但仍然出现错误。 -
你试过我的建议了吗?
-
@AnderBiguri 是的,这对我有用。谢谢
-
黑白元胞数组和普通数组的性能有区别吗?