繁体   English   中英

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

[英]error in MATLAB regarding dimensions mismatch

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

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

第27行:

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

你能帮我解决这个问题吗?

您可以尝试用零填充较小的图像,以便与较大的图像具有相同的尺寸。 例如

%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

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

但是,您首先需要弄清楚您要做什么的逻辑。 用零填充在您的应用程序中可能没有意义。 同样,我的代码垫位于底部和左侧,但您可能希望一直垫一下,以使图像位于新图像的中心。

暂无
暂无

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

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