Joining matrices while combining columns

hello...
i have about 1,500 640x480 matrices that need to be joined both horizontally and vertically. while joining them, i need to overlap the border rows and columns creating an average and replacing the original columns with the average.
simple example while looking at a two column and row overlap;
if i have four 6x6 matrices;
A B C D
i need to combine to create an average of each individual number on column 5 of matrix A with column 1 of matrix B. then do the same with column 6 of A and column 2 of B. do the same for C and D.
then you would end up with two new 6x10 matrices;
E F
i also need to join these two with the same operation but now using the rows. in the end, i end up with a single 10x10 matrix.
these are 'photos' that need to be used to created a 'collage' that has an overlap (average) between each single image.
looking for something that lets me pick the number of pixels i overlap to create an average and eventually a single (huge) picture. i have about 12 rows of 125 pictures to be joined.
any ideas?
i can get it to kinda work but don't know matlab enough to know any short cuts and lean programming. i have used loops and concatenating but i am having no luck.
thanks, kiko

2 Kommentare

Jan
Jan am 7 Sep. 2011
@Henrique: I do not like to be contacted by email. The underlying sense of this forum is, that it is public. Pushing mails do not motivate me to give more or better answers.
Btw., I neither did answer to http://www.mathworks.nl/matlabcentral/newsreader/view_thread/311394, nor has this thread been created in 2009.
Please post the code you have created and explain in detail, what "no luck" means.
Henrique
Henrique am 8 Sep. 2011
first of all, sorry for contacting you directly if that was a no-no.
secondly, somehow the thread i sent you was incorrect; don't know how that happened. here is the right one;
http://www.mathworks.nl/matlabcentral/newsreader/view_thread/262124
and the code i am trying to use is something like;
for pass=1:npass; % vertical matrices
for pic=1:npic-1; % horizontal matrices
A = NMP(:,1:col2-shiftP,pass,pic);
B = (NMP(:,col2-shiftP+1:col2,pass,pic) +
NMP(:,1:shiftP,pass,pic+1))/2;
C = NMP(:,shiftP+1:col1,pass,pic+1);
NMP(:,:,pass,pic) = [A B C]
% A and C being what i am keeping and B being what i am
% averaging between A and C. the error i am getting is at
% NMP(:,:,pass,pic) - the way it's set up, does not work
% in a loop.
[row2,col2] = size(NMP);
end;
end

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 8 Sep. 2011

0 Stimmen

I don't know any special function for this. I think there should be some from maybe the Image Processing Toolbox.
For the task itself, it should be straightforward, as long as you know the basics of referencing matrix.
N=6;
Overlay=2;
A=magic(N);
B=magic(N);
% horizontal overlay
E=[A B];
E(:,N-Overlay+1:N)=(A(:,N-Overlay+1:N)+B(:,1:Overlay))/2;
E(:,N+1:N+Overlay)=[];
% vertical overlay
F=[A;B];
F(N-Overlay+1:N,:)=(A(N-Overlay+1:N,:)+B(1:Overlay,:))/2;
F(N+1:N+Overlay,:)=[];
Go through a loop for more matrices. It is all about figuring out the pattern for matrix subscripting. I'll provide the horizontal overlay.
N=6; % the size of the square matrix
Overlay=2;
M=10; % the number of the matrix
A=zeros(N,N,M);
for k=1:M
A(:,:,k)=magic(N);
end
% horizontal overlay
R=zeros(N,M*(N-Overlay)+Overlay);
R(:,1:N-Overlay)=A(:,1:N-Overlay,1); %left end
R(:,end-N+Overlay+1:end)=A(:,end-N+Overlay+1:end,M); %right end
for k=1:M-1 %do the overlay
R(:,k*(N-Overlay)+1:k*(N-Overlay)+Overlay)=(A(:,end-Overlay+1:end,k)+ ...
A(:,1:Overlay,k+1))/2;
end
for k=2:M-1 %do the middle
R(:,(k-1)*(N-Overlay)+Overlay+1:k*(N-Overlay))=A(:,Overlay+1:N-Overlay,k);
end
figure(1);image(A(:,:,1));
figure(2);image(R);

3 Kommentare

Henrique
Henrique am 8 Sep. 2011
let me study the loop... i'll get back with you.
thank you so much! kiko
Henrique
Henrique am 8 Sep. 2011
.
dear fangjun:
your looped code is great, works wonders.
i am now trying to construct a loop to create the 12 long matrices that i need to later connect them vertically.
however, i am wondering if it would be easier to concatenate the 1,500 hundred matrices first (creating a 7680x60000 matrix) and then average columns and rows every so often reducing the number of columns by half of the overlap.
but a concern would be on how to display the new (very big) matrix as a photo?!?! the original concatenated one is 460,800,000 elements; i don't see the overlapped one being much smaller.
while i don't see the processing being a problem on something that big, does matlab have a limit on size? how would i even display a matrix this big if each element represents a pixel?!?!
thanks again.
kiko
Fangjun Jiang
Fangjun Jiang am 8 Sep. 2011
Usually MATLAB doesn't have limit on the size of the matrix as long as the computer or OS has enough memory for it. Each double takes 8 bytes. So your 460M elements will take 3.7G memory. I think you are about to reach the limit of 32bits OS.
If you have thousands of 640x480 images and you stitch them together, you will have the issue of how effectively display them. Again, this is your application and you need to think about what you want and what is the best way. I don't think it is an issue particularly related to MATLAB.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Henrique
Henrique am 8 Sep. 2011

0 Stimmen

dear fangjun jiang... thanks for the comment.
your code is similar to what i have and it makes total sense but i am having a hard time putting all that into a loop. again, i have 1,500 matrices i need to run through this.
i get things to work once (i am able to create a new 6x10 matrix from two 6x6 matrices) but when i go through the loop again (to create a new 6x14) i have problems...
i a getting a "??? Subscripted assignment dimension mismatch." error. it's having a hard time overwriting the original 6x6 with the new 6x10 to be combined with the next 6x6.
thanks, kiko

3 Kommentare

Henrique
Henrique am 8 Sep. 2011
.
all these matrices (photos) were thermal IR data that was collected on a concrete bridge deck. the FOV of the camera was so small so we had to do many passes (lanes) to collect pictures. it was a ~200ft long bridge about 24ft wide. we had to do 12 passes collecting 125 photos on each pass.
to make sure we were collecting data on the entire deck, we designed for overlaps.
the help i need is in stitching these 1,500 photos while averaging the overlaps. you can get more specifics on the data collection on page 13 of this document;
http://mtri.org/bridgecondition/doc/Tech%20Memo%2020%20-%20Field%20Deployment%20and%20Instrument%20Installation%20and%20Calibration%20Plan%20Final.pdf
general project overview at;
http://mtri.org/bridgecondition/
thanks again.
kiko
Fangjun Jiang
Fangjun Jiang am 8 Sep. 2011
See update.
Fangjun Jiang
Fangjun Jiang am 8 Sep. 2011
Update, use image() instead of pcolor() to show the picture. If you don't have image(), then still use pcolor() but be aware the number of gird is 1 less of the size.

Melden Sie sich an, um zu kommentieren.

Henrique
Henrique am 12 Sep. 2011

0 Stimmen

this is the best i could come up with. it shows the routine running with 42 matrices. i was able to run it with about 150 of them. memory -IS- a problem so i need to talk with our IT department on where i can run a 1,500 photo (460,800,000 elements) matrix.
thank you so much fangjun jiang!
here;
% routine for uploading individual photos (matrices) and overlapping them
% by a certain amount. upload is currently not in a loooooop.
% kiko - 09/SEP/11
clear;
tic
M11=rot90(dlmread('Test4-33.txt','\t')); % loads pic 01 of pass 01 and rotates (pass 4, pic 33)
M12=rot90(dlmread('Test6-40.txt','\t')); % loads pic 02 of pass 01 and rotates (pass 6, pic 40)
M13=rot90(dlmread('Test6-41.txt','\t')); % loads pic 03 of pass 01 and rotates (pass 6, pic 41)
M14=rot90(dlmread('Test6-49.txt','\t')); % loads pic 04 of pass 01 and rotates (pass 6, pic 49)
M15=rot90(dlmread('Test6-63.txt','\t')); % loads pic 05 of pass 01 and rotates (pass 6, pic 63)
M16=rot90(dlmread('Test6-67.txt','\t')); % loads pic 06 of pass 01 and rotates (pass 6, pic 67)
M17=rot90(dlmread('Test6-68.txt','\t')); % loads pic 07 of pass 01 and rotates (pass 6, pic 68)
M21=rot90(dlmread('Test7-49.txt','\t')); % loads pic 01 of pass 02 and rotates (pass 7, pic 51)
M22=rot90(dlmread('Test7-50.txt','\t')); % loads pic 02 of pass 02 and rotates (pass 7, pic 51)
M23=rot90(dlmread('Test7-51.txt','\t')); % loads pic 03 of pass 02 and rotates (pass 7, pic 51)
M24=rot90(dlmread('Test7-63.txt','\t')); % loads pic 04 of pass 02 and rotates (pass 7, pic 63)
M25=rot90(dlmread('Test7-64.txt','\t')); % loads pic 05 of pass 02 and rotates (pass 7, pic 64)
M26=rot90(dlmread('Test7-65.txt','\t')); % loads pic 06 of pass 02 and rotates (pass 7, pic 65)
M27=rot90(dlmread('Test7-66.txt','\t')); % loads pic 07 of pass 02 and rotates (pass 7, pic 66)
[ORIrow,ORIcol]=size(M11); % row and columns size of individual matrix
Hpass1=[M11 M12 M13 M14 M15 M16 M17]; % joins each pic for a pass (pass 7 in this case)
Hpass2=[M21 M22 M23 M24 M25 M26 M27]; % joins each pic for a pass (pass 7 in this case)
H=vertcat(Hpass1,Hpass2,Hpass1,Hpass2,Hpass1,Hpass2); % joins all pix and creates main matrix without overlaps (42 matrices)
% the 'H' matrix created here is just an example. actual matrix to be
% processed will have 1,500 individual matrices/images. about 12 of them
% row-wise and about 125 column-wise. 'H' will have about 460,800,000
% elements in total before is scalled down by averaging and deleting
% certain rows and columns.
[OArow,OAcol]=size(H); % calculates number of columns and rows
%%%%%horizontal overlap - along direction of cart travel
OLcol=48; % number of pixels overlapping column wise (per pic)
NHMcol=OAcol/ORIcol; %
SScol=OAcol/NHMcol; % counters and indexes
ffcol=OAcol-SScol; %
for f=1:NHMcol-1;
for j=0:OLcol-1;
H(:,ffcol-j)=(H(:,ffcol-j)+H(:,ffcol-j+OLcol))/2; % creates new columns averaging overlapping columns
end
for k=1:OLcol;
H(:,ffcol+1)=[]; % deletes extra columns
end
ffcol=ffcol-SScol;
end
%%%%%vertical overlap - along direction of cart travel
OLrow=64; % number of pixels overlapping row wise (per pass)
NHMrow=OArow/ORIrow; %
SSrow=OArow/NHMrow; % counters and indexes
ffrow=OArow-SSrow; %
for f=1:NHMrow-1;
for j=0:OLrow-1;
H(ffrow-j,:)=(H(ffrow-j,:)+H(ffrow-j+OLrow,:))/2; % creates new rows avereaging overlapping rows
end
for k=1:OLrow;
H(ffrow+1,:)=[]; % delets extra rows
end
ffrow=ffrow-SSrow;
end
figure(1); pcolor(H); % plotting new matrix
toc

Kategorien

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by