フィルターのクリア

Matrix/Image Merging

5 ビュー (過去 30 日間)
Kyle
Kyle 2011 年 6 月 23 日
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 日
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 件のコメント
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 日
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 件のコメント
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 日
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 件のコメント
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');

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

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by