简体   繁体   English

MATLAB中有关尺寸不匹配的错误

[英]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 实际上,我正在尝试在以下网站上实现代码:http://www.mathworks.com/matlabcentral/fileexchange/28300,但这仅在给定的两个图像具有相同尺寸时才有效,我想使此代码在一个图像上起作用具有某个维度,而另一个具有其他维度,如果我使用当前代码执行此操作,则会出现错误

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

Line 27: 第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. 我尚未进行测试,因此您可能需要花一点时间才能获得完美的尺寸,例如也许size(I2,2)-size(I1,2)+ 1而不是size(I2,2)-size(I1 ,2),这类事情。

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. 同样,我的代码垫位于底部和左侧,但您可能希望一直垫一下,以使图像位于新图像的中心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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