在matlab中比较多个图像

Comparing multiple images in matlab

我正在尝试对存储在元胞数组中的 20 张 png 图像进行逐像素比较。对于每个像素位置 (i,j),我想从 20 张图像中找到具有最大和最小值的像素。

我当前的实现似乎可行,但由于它只是一堆嵌套的 for 循环,因此需要几分钟才能完成执行。我正在寻找一种更有效的方法,有人有建议吗?我当前的代码如下。

min = 256;
max = -1;

for j = 1: xMax
    for k = 1: yMax
        for p = 1: 20
            if imageArray{p}(j,k) > max
                max = imageArray{p}(j,k);
            end
            if imageArray{p}(j,k) < min
                min = imageArray{p}(j,k);
            end
        end
        minImg(j,k) = min;
        maxImg(j,k) = max;
        min = 256;
        max = -1;
    end
end

假设所有这些图像的大小都相同,这是一种有效的方法 -

%// Get dimensions of each image
[nrows,ncols] = size(imageArray{1}) 

%// Convert the cell array to a 3D numeric array for vectorized operations
im = reshape(cell2mat(imageArray),nrows,ncols,[])

%// Use MATLAB builtins min and max along the third dimension for final output
minImg = min(im,[],3)
maxImg = max(im,[],3)