Whatzat
Whatzat

Reputation: 81

Part of an image removed when dividing an image into sub images in MATLAB

I have an image of 336*104 pixels and I need to divide it into 35*35 sub images. However 336 and 104 are not divisible by 35, therefore a part of the image is removed when I divide it. I get a re-assembled image without the parts which are not of 35*35 pixels. I can't resize it because the image is very different if I do it. Here my code:

% Size of the image
[H W] = size(im);

% Each sub image has 35*35 pixels
size_sub_image = 35;

% Size of each sub image
H_divided = H/size_sub_image;
W_divided = W/size_sub_image;

% Reminders allowing to take the images which are not divided
R_H = rem(H,size_sub_image);
R_W = rem(W,size_sub_image);

%Variable which is incremented and stand for each sub image
nbr_sub_images = 1;  

% Division of the image into sub images    
for i = 1:floor(H_divided)
    for j = 1:floor(W_divided)

        % R_H and R_W allow to create the sub images reminding, which have not
        %35*35 pixels. 
        %My idea was that when each i or j reach the max in the loop, the 
        %algorithm take the remindind images which have the size of R_H or R_W.
        % R_H = 21 (336 = 35*9+21) and R_W = 34 (104 = 35*2+34)

        if (i ~= floor(H_divided) && j ~= floor(W_divided))   

            nbr_sub_images = nbr_sub_images + 1;
            im_divided(:,:,nbr_sub_images) = im(size_sub_image*(i-1)+1:size_sub_image*i ,size_sub_image*(j-1)+1:size_sub_image*j );   

        end 

        if (i == floor(H_divided))

            nbr_sub_images = nbr_sub_images + 1;      
            im_divided(:,:,nbr_sub_images) = im(size_sub_image*(i-1)+1:size_sub_image*i ,size_sub_image*(j-1)+1:size_sub_image*j );

            nbr_sub_images = nbr_sub_images + 1;      
            im_divided(:,:,nbr_sub_images) = im(size_sub_image*i+1:size_sub_image*i + R_H,size_sub_image*(j-1)+1:size_sub_image*j);

        end

        if (j == floor(W_divided))

            nbr_sub_images = nbr_sub_images + 1;   
            im_divided(:,:,nbr_sub_images) = im(size_sub_image*(i-1)+1:size_sub_image*i ,size_sub_image*(j-1)+1:size_sub_image*j );

            nbr_sub_images = nbr_sub_images + 1;   
            im_divided(:,:,nbr_sub_images) = im(size_sub_image*(i-1)+1:size_sub_image*i,size_sub_image*j+1:size_sub_image*j + R_W);                 


        end

    end
end

When I execute it, i get the error: "Subscripted assignment dimension mismatch", but I don't understand why because when I remove the R_H and R_W, the code works perfectly.

Upvotes: 0

Views: 43

Answers (1)

dasdingonesin
dasdingonesin

Reputation: 1358

As I understand it, you are trying to put sub-images of different sizes into a 3-D array, which is not going to work. When you add 2-D images to your array im_divided, the first and second dimension have to be the same (i.e. 35x35) for all sub-images. For more information, see the MATLAB documentation.

There are 2 possible solutions: Either you pad the edges of your image (with zeroes or some other value) so that width and height are divisible by 35, or you use a cell array to store your sub-images:

im_divided{nbr_sub_images} = im(rows, columns)

Cell arrays are capable of storing matrices of different sizes (or in fact all kinds of different objects).

Upvotes: 1

Related Questions