Matlab allRGB图像生成

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

我正在研究学校项目的脚本/功能,它将在matlab中生成所有24位RGB颜色图像。

我写了这样的东西,但它很慢(而且matlab不像我一样崩溃了很多)。在崩溃前的最后一次,它正在工作5天。这是代码:

a = 1;
for r = 0:255
    for g = 0:255
        for b = 0:255
            colors(a,:) = [r g b];
            a = a + 1;
        end
    end
end

colors = reshape(colors, [4096, 4096, 3]);

colors = uint8(colors);
imshow(colors);
imwrite(colors, 'generated.png');

有没有更快的方法来做到这一点?

image matlab image-processing rgb
2个回答
2
投票

使用repmat / repelem分别构建三列,然后将它们连接起来。

colors = [repelem((0:255).',256^2),...
          repmat([repelem((0:255).',256) repmat((0:255).',256,1)],256,1)];

2
投票

通常最好预先分配大型矩阵以加速代码。使用当前的实现,colors的大小每次迭代都会增长一行,这需要大量的内存分配资源。尝试使用。定义矩阵

colors = zeros(2^24, 3);

在代码的开头。为了节省内存和时间,您甚至可以从一开始就将矩阵定义为uint8,而不是之后进行转换

colors = zeros(2^24, 3, 'uint8');
© www.soinside.com 2019 - 2024. All rights reserved.