简体   繁体   中英

error in MATLAB regarding dimensions mismatch

Actually i am trying to implement the code on the following website http://www.mathworks.com/matlabcentral/fileexchange/28300 but this works only when two images given have the same dimensions , i want to make this code work where one image has some dimension and other has some other dimension, if i do this with current code, it gives the error

??? Sub scripted assignment dimension mismatch. Error in ==> example2 at 27

Line 27:

I(:,1:size(I1,2),:)=I1; I(:,size(I1,2)+1:size(I1,2)+size(I2,2),:)=I2; 

can you resolve this for me?

You can try padding the smaller image with zeros so that is has the same dimensions as the larger image. For example

%Start with rows:    
if size(I1,1) > size(I2,1) %I1 has more rows so pad I2
        pad = zeros (size(I1,1) - size(I2,1), size(I2,2));
        I2 = [I2 ; pad]; %Append the rows of zeros to the bottom of I2
else %I2 has more rows so pad I1
        pad = zeros (size(I2,1) - size(I1,1), size(I1,2));
        I1 = [I1 ; pad]; %Append the rows of zeros to the bottom of I1

%Pad the columns    
if size(I1,2) > size(I2,2) %I1 has more rows so pad I2
        pad = zeros (size(I2,1), size(I1,2) - size(I2,2));
        I2 = [I2 , pad]; %Append the columns of zeros to the left of I2
else %I2 has more rows so pad I1
        pad = zeros (size(I1,1), size(I2,2) - size(I1,2));
        I1 = [I1 , pad]; %Append the columns of zeros to the left of I1

I haven't tested that though so you might need to fiddle a bit to get the dimension perfect, like maybe size(I2,2) - size(I1,2) + 1 instead of size(I2,2) - size(I1,2), that sort of thing.

But you need to first figure out the logic of what you are trying to do. Padding with zeros might not make sense in your application. Also my code pads on the bottom and the left but you might want to pad all the way around so your image is in the centre of the new image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM