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;
채택된 답변
추가 답변 (2개)
Matt Fig
2011년 6월 24일
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
2011년 6월 24일
Matt Fig
2011년 6월 24일
But if there are two matches, then there is two ways to overlap. Which is preferred?
Kyle
2011년 6월 24일
Sean de Wolski
2011년 6월 24일
That would crop out black columns or rows of an image.
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
2011년 6월 24일
Good Point.
Kyle
2011년 6월 25일
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!