Search code examples
arraysimagematlabcell

How to create single image from cell array of matrices in Matlab?


I needed to split a grayscale image in equal parts so I used the function mat2cell. Then I had to equalize each of the parts separatelly, for this purpose I used the function histeq. I reused the same cell array variable for this. Here is the code:

height=round(size(img,1)/number_of_divisions);
length=round(size(img,2)/number_of_divisions);
M=zeros(number_of_divisions,1);
N=zeros(1,number_of_divisions);
M(1:number_of_divisions)=height;
N(1:number_of_divisions)=length;
aux=mat2cell(img,M,N);

for i=1:size(aux,1)
    for j=1:size(aux,2)
        aux{i,j}=histeq(aux{i,j},256);
    end
end

So now how do I merge each cell into one single image?


Solution

  • Use cell2mat

    img2=cell2mat(aux);
    

    For a better performance, replace your code with blockproc

    blocksize=ceil(size(img)./number_of_divisions);
    img2=blockproc(img,blocksize,@(block_struct)histeq(block_struct.data));