MATLAB: Error !! horzcat CAT arguments dimensions are not consistent.

hi sir i am very much new to matlab ..i have two images ..i want to use "imoverlay" for the images but whenever i run it ,i get error saying horzcat arguments dimension are consistent ..i am working on retinal image analysis..belo i have uploaded my code and error.sorry for not providing sufficient info error is" horzcat CAT arguments dimensions are not consistent.
Error in ==> file1>filter_Callback at 692 C = horzcat(I2,1);"

Best Answer

Vidya, you can solve your padding problem by
  1. switching vertcat and horzcat (reasoning in the comment below)
  2. image is 3 dimensional matrix (the 3rd dimension for the rgb color)
Here is the fixed code:
%Pad the columns
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),size(I2,3));
%I2 = [I2 ; pad]; %Append the rows of zeros to the bottom of I2
I2 = vertcat(I2,pad); % vertical concatenation as you pad rows

else %I2 has more rows so pad I1

pad = zeros (size(I2,1) - size(I1,1), size(I1,2),size(I1,3));
%I1 = [I1 ; pad]; %Append the rows of zeros to the bottom of I1
I1 = vertcat(I1,pad); % vertical concatenation as you pad rows
end
%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),size(I2,3));
%I2 = [I2 , pad]; %Append the columns of zeros to the left of I2
I2 = horzcat(I2,pad); % horizontal concatenation as you pad columns

else %I2 has more rows so pad I1
pad = zeros (size(I1,1), size(I2,2) - size(I1,2),size(I1,3));
% I1 = [I1 , pad]; %Append the columns of zeros to the left of I1
I1 = horzcat(I1,pad); % horizontal concatenation as you pad columns
end
ovr1=imoverlay(I1,I2,[0,0,0]); % overlaying updated images