Reputation: 1453
I have a problem so what to do with it?!
I read two images of different sizes in matlab and then I covert them to double to make operations on them but the problem that they are not in same size so what to do to make them same as bigger one and then fill other empty size by zero?
Upvotes: 0
Views: 574
Reputation: 21563
Your questioin is a bit vague, but suppose you have two matrices A
and B
which have different sizes.
Now if you always want to pad the smallest dimension with zeros, you can do this:
rs = max([size(A);size(B)]); % Find out what size you want
A(rs(1)+1,rs(2)+1) = 0; % Automatically pad the matrix to the desired corner (plus 1)
B(rs(1)+1,rs(2)+1) = 0; % Plus one is required to prevent loss of the value at (end,end)
A = A(1:end,1:end); %Now remove that one again
B = B(1:end,1:end);
Note that this works regardless of which one is bigger, and whether one is higher while the other one is wider. When you like to use if statements, this may be easier to understand:
rs = max([size(A);size(B)]); % Find out what size you want
if any(size(A) < rs)
A(rs(1),rs(2)) = 0;
end
if any(size(B) < rs)
B(rs(1),rs(2)) = 0;
end
Upvotes: 0
Reputation: 95948
Lets say you have two matrices:
a = 1 2 3
4 5 6
7 8 9
b = 1 2
3 4
You can do something like this:
c = zeros(size(a)) %since a is bigger
Which will create:
c = 0 0 0
0 0 0
0 0 0
And then you copy the content of the smaller matrix (b
in this case):
c(1:size(b,1), 1:size(b,2)) = b;
(size(b,1) returns the number of rows and size(b,2) returns the numbers of columns)
And the final result will be a matrix of size a
filled with the values of b
and 0
everywhere else:
c = 1 2 0
3 4 0
0 0 0
image1=imread(image1Path);
image2=imread(image2Path);
image1= double(image1);
image2= double(image2);
%%%ASSUME image1 is bigger%%%
new_image = zeros(size(image1));
new_image(1:size(image2,1), 1:size(image2,2)) = image2;
%NOW new_image will be as you want.
Upvotes: 2