Divide a matrix into submatrices in MATLAB

后端 未结 3 845
时光说笑
时光说笑 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)
    

提交回复
热议问题