Matrix/Image Merging

Hi,
I have 2 matrix A and B which are different size, I would like to combine them together with A as the master to make a new matrix with consist of A and B. The way I want both matrix is merge through a location on both matrix. For example place Matrix A on Matrix B on certain location, Eg ‘8’ on Matrix A to Matrix B’s ‘22’ to result Matrix Merge.
A=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15]
A =
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
B=[13 14 15 20 27 21; 16 17 18 19 25 26; 21 22 23 24 25 23]
B =
13 14 15 20 27 21
16 17 18 19 25 26
21 22 23 24 25 23
Merge=[0 13 14 15 20 27 21 ; 1 2 3 4 5 25 26 ; 6 7 8 9 10 25 23 ; 11 12 13 14 15 0 0]
Merge =
0 13 14 15 20 27 21
1 2 3 4 5 25 26
6 7 8 9 10 25 23
11 12 13 14 15 0 0
Besides that, additional space will have value of 0. I'll use these method to merge 2 images where i have a location at both images which going to be used to control the location of merging.
Thanks;

1 件のコメント

Kyle
Kyle 2011 年 6 月 25 日
Problem solved by Sean de and Matt Fig.
Guys is it possible to modify the code to support this kind of matrix?
A=reshape(1:45,3,5,3);
B=reshape(1:105,5,7,3)+45;
The merging still same as before. Superimpose base location on Matrix A and Matrix B. However now there is 3 level of array. (i thinks its call 2 dimensional array, not very sure though)
i could store each level of array into separate 1 dimensional array and use the code u guys written to combine the matrix then put the matrix back into a new 2 dimensional array. But that means i need to run through the code 3 times.

サインインしてコメントする。

 採用された回答

Sean de Wolski
Sean de Wolski 2011 年 6 月 23 日

1 投票

Repaste New trix every time!
clear Merge
A=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15];
B=[16 17 18 19 20 21; 22 23 24 25 26 27; 28 29 30 31 32 33; 34 35 36 37 38 39;40 41 42 43 44 45];
Awant = 8;
Bwant = 42;
[ra ca] = find(A==Awant,1,'first');
[rb cb] = find(B==Bwant,1,'first');
D = abs(diff([ra ca;rb cb],1,1));
sd = sign(diff([ra ca;rb cb],1,1));
corners = abs(sum(sd));
if ~corners
if sd(1) == 1
Merge(1:(size(B,1)),(D(2)+1:(size(B,2)+D(2)))) = B;
Merge((D(1)+1):(size(A,1)+D(1)),1:(size(A,2))) = A;
else
Merge((D(1)+1):(size(B,1)+D(1)),1:(size(B,2))) = B;
Merge(1:(size(A,1)),(D(2)+1:(size(A,2)+D(2)))) = A;
end
elseif corners == 1;
if ~sd(1)
Merge(1:(size(B,1)),1:(size(B,2))) = B;
Merge((D(1)+1):(size(A,1)+D(1)),(D(2)+1:(size(A,2)+D(2)))) = A;
else
Merge(1:(size(B,1)),1:(size(B,2))) = B;
Merge((D(1)+1):(size(A,1)+D(1)),(D(2)+1:(size(A,2)+D(2)))) = A;
end
else
if sd(1) == 1
Merge(1:(size(B,1)),1:(size(B,2))) = B;
Merge((D(1)+1):(size(A,1)+D(1)),(D(2)+1:(size(A,2)+D(2)))) = A;
else
Merge((D(1)+1):(size(B,1)+D(1)),(D(2)+1:(size(B,2)+D(2)))) = B;
Merge(1:(size(A,1)),1:(size(A,2))) = A;
end
end
Should work for any case.

8 件のコメント

Kyle
Kyle 2011 年 6 月 23 日
When i try
Awant = 7;
Bwant = 22;
The result:
Merge =
13 14 15 20 27 21 0
16 1 2 3 4 5 0
21 6 7 8 9 10 0
0 11 12 13 14 15 0
But should be
C =
13 14 15 20 27 21
1 2 3 4 5 26
6 7 8 9 10 23
11 12 13 14 15 0
Kyle
Kyle 2011 年 6 月 23 日
C=[13 14 15 20 27 21; 1 2 3 4 5 26; 6 7 8 9 10 23; 11 12 13 14 15 0]
Kyle
Kyle 2011 年 6 月 23 日
Yep i tried clear it n it still give me
Merge =
13 14 15 20 27 21 0
16 1 2 3 4 5 0
21 6 7 8 9 10 0
0 11 12 13 14 15 0
Kyle
Kyle 2011 年 6 月 23 日
Weird then only thing i change from ur code is
Awant = 7;
Bwant = 22;
but still cant get the correct result like yours.
If u don't mind. Please re-paste ur code again
Thanks
Kyle
Kyle 2011 年 6 月 24 日
Thanks yep it work after u re paste.
I tested all possible configuration tat i could think of with ur code. The result is correct for all configuration except for one type.
When:
A=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15]
B=[16 17 18 19 20 21; 22 23 24 25 26 27; 28 29 30 31 32 33; 34 35 36 37 38 39;40 41 42 43 44 45]
Awant = 8;
Bwant =25; % try also 26,27
Kyle
Kyle 2011 年 6 月 24 日
Thanks a lot for ur help.
Hopefully u dont get offended. Found some error
Awant = 8;
Bwant =42; % 42,36,30, weird 23 also wrong
Sean de Wolski
Sean de Wolski 2011 年 6 月 24 日
Again!
You're doing your job of testing quite well. Better than me.
Kyle
Kyle 2011 年 6 月 25 日
Thanks.
No error found :D

サインインしてコメントする。

その他の回答 (2 件)

Matt Fig
Matt Fig 2011 年 6 月 24 日

1 投票

Kyle, what to do when multiple matches are found in B, since your example B has duplicates?
%
%
%
%
%
EDIT In response to clarification about duplicates.
Since you say there will not be duplicates in the actual data, I will use example matrices without duplicates:
A = reshape(1:12,3,4);
B = reshape(1:30,5,6)+12;
NA = 5; % The number to overlap in A.
NB = 35; % The number to overlap in B.
[mA,nA] = size(A);
[mB,nB] = size(B);
[IA,JA] = find(A==NA);
[IB,JB] = find(B==NB);
mC = mA+mB+mod(mA+mB,2)+1;
nC = nA+nB+mod(nA+nB,2)+1;
C = zeros(mC,nC);
cC = round([mC/2,nC/2]);
C(cC(1)-IB+1:cC(1)-IB+mB,cC(2)-JB+1:cC(2)-JB+nB) = B;
C(cC(1)-IA+1:cC(1)-IA+mA,cC(2)-JA+1:cC(2)-JA+nA) = A;
C = C(:,any(logical(C)));
C = C(any(logical(C),2),:)

7 件のコメント

Kyle
Kyle 2011 年 6 月 24 日
For Sean's code, he find the location of the Bwant then start computation to determine how to superimpose A on to B.
Therefore it doesnt matter if it has multiple matches as long as u know the location of the point in A and B
Matt Fig
Matt Fig 2011 年 6 月 24 日
But if there are two matches, then there is two ways to overlap. Which is preferred?
Kyle
Kyle 2011 年 6 月 24 日
Oh, i'm going to use this concept to merge 2 image. So there would only be 1 match on each image.
I tried this concept on gray scale image and its applicable. Now i'm trying to used it on RGB image as RGB has 3 array in 1 single image.
A(:,:,1:3)
Sean de Wolski
Sean de Wolski 2011 年 6 月 24 日
That would crop out black columns or rows of an image.
Matt Fig
Matt Fig 2011 年 6 月 24 日
True, but Kyle said there were no duplicates. In that case there shouldn't be an entire row or column that is the same!
Sean de Wolski
Sean de Wolski 2011 年 6 月 24 日
Good Point.
Kyle
Kyle 2011 年 6 月 25 日
Ur code does work for matrix. Very robust
But when i applied on gray images. The output image doesnt seems to be from my input image

サインインしてコメントする。

Kyle
Kyle 2011 年 6 月 25 日

0 投票

Hi Matt,
i used ur code to test on image like shown below.
clc
% A = reshape(1:15,3,5)
% B = reshape(1:35,5,7)+12
A = imread('cameraman.tif');
B = imread('cameraman.tif');
% NA = 8; % The number to overlap in A.
% NB = 32; % The number to overlap in B.
[mA,nA] = size(A);
[mB,nB] = size(B);
% [IA,JA] = find(A==NA);
% [IB,JB] = find(B==NB);
IA=50;
JA=50;
IB=1;
JB=1;
mC = mA+mB+mod(mA+mB,2)+1;
nC = nA+nB+mod(nA+nB,2)+1;
C = zeros(mC,nC);
cC = round([mC/2,nC/2]);
C(cC(1)-IB+1:cC(1)-IB+mB,cC(2)-JB+1:cC(2)-JB+nB) = B;
C(cC(1)-IA+1:cC(1)-IA+mA,cC(2)-JA+1:cC(2)-JA+nA) = A;
C = C(:,any(logical(C)));
C = C(any(logical(C),2),:);
imshow(C)
The resultant image should be Image A overlapping Image B. however i only see black n white. Did i do anything wrong? Ur code works on matrix, and image is also a form of matrix. I dont know how come it when wrong.

3 件のコメント

Matt Fig
Matt Fig 2011 年 6 月 25 日
I don't have IMSHOW (I think that is an image processing toolbox function), but it works with:
image(C)
colormap(gray)
You will have to figure out what went wrong with IMSHOW, as I have no way to trouble-shoot.
Kyle
Kyle 2011 年 6 月 25 日
This is odd. i even check the pixel value. its the same but shows different color. First time encounter this
Kyle
Kyle 2011 年 6 月 28 日
http://www.mathworks.com/matlabcentral/answers/10268-weird-imshow-image-same-pixel-value-different-color
Problem solved, need to change this
C = zeros(mC,nC,'uint8');

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by