Divide a matrix into submatrices in MATLAB

后端 未结 3 837
时光说笑
时光说笑 2020-12-12 06:16

I have an RGB image of size 412x550. I want to divide it into sub matrices of size 2x2. I have tried using mat2cell function but it is

相关标签:
3条回答
  • 2020-12-12 06:25

    In general, you can use the following code to divide the image into blocks (for compression process or whatever)

        A=imread('image.bmp'); % i assume 8-bit gray scale image
        [m,n,k]=size(A); % and m=n with 1 channel k=1
        ImageSize=m*n;
        BlockD=2; % i assume 2x2 block
        BlockSize=BlockD*BlockD;
        NoOfBlock=ImageSize/BlockSize;
        SubB=zeros(BlockD,BlockD,NoOfBlock); %arrays of blocks.
        B=double(A); important to convert uint8 to double when dialing with image.
        % thats what ru asking for.
        k=1;
        for i=1:BlockD:m
        for j=1:BlockD:n
            SubB(:,:,k)=B(i:i+BlockD-1,j:j+BlockD-1); k=k+1;
        end
        end
        %compare between first submatrix A with first block.. its the same elements.
        B(1:2,1:2)
        SubB(:,:,1)
    
    0 讨论(0)
  • 2020-12-12 06:38

    You probably got the syntax a bit off. The correct syntax for your situation would be

    >> A = rand(412,550);
    >> B = mat2cell(A, 2*ones(size(A,1)/2,1), 2*ones(size(A,2)/2,1))
    
    ans = 
        [2x2 double]    [2x2 double]    ... 
        [2x2 double]    [2x2 double]    ...
        ...
    
    0 讨论(0)
  • 2020-12-12 06:46

    You should use the function im2col. It is exactly what you need.

    A = rand(412,550);
    B = im2col(A,[2 2]);
    

    The doc is there:

    http://www.mathworks.fr/help/images/ref/im2col.html;jsessionid=4d24325613716d84d4635b4fc636

    0 讨论(0)
提交回复
热议问题