将图像划分为9个相等或几乎相等的分区:Matlab

问题描述 投票:0回答:2

我需要将图像分区为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张图像的此功能。

arrays image matlab
2个回答
3
投票

只需使用一个单元格阵列。由于您的子块大小不同,因此它是存储数据的最佳选择。

partitions{x,y} = inputImage(loweri:higheri, lowerj:higherj);

-1
投票

你可以尝试一下

function [ outputImageRectangles ] = getImagePartitions( inputImage )
[height, width] = size(inputImage);
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;

        partitions{x,y} = inputImage(loweri:higheri, lowerj:higherj);
    end
end
outputImageRectangles = partitions;
© www.soinside.com 2019 - 2024. All rights reserved.