【问题标题】:Partition Image into 9 equal or nearly equal partitions: Matlab将图像划分为 9 个相等或几乎相等的分区:Matlab
【发布时间】: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 是的,这对我有用。谢谢
  • 黑白元胞数组和普通数组的性能有区别吗?

标签: arrays image matlab


【解决方案1】:

只需使用元胞数组。由于您的子块大小不同,因此它是存储数据的最佳选择。

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

【讨论】:

    【解决方案2】:

    你可以试试这个

    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;
    

    【讨论】:

    • @downvoter,您愿意发表评论吗?
    • 我没有投反对票,但partitions 现在是一个 4D 元胞数组,其中这个答案像 2D 数组一样索引到元胞数组。此代码将在第一次运行 partiions{x,y} 时崩溃。
    • 我没有投反对票,但这实际上是我的答案和 OPs 代码的复制粘贴,您没有添加任何额外信息
    • 好吧,我确实在你之前回答了......犯了一个小错误,后来我更正了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 2012-11-25
    • 2011-10-15
    • 1970-01-01
    • 2011-02-09
    • 2015-06-25
    • 2018-10-04
    相关资源
    最近更新 更多