How to make summation of moved and fixed matrix ?

1 view (last 30 days)
I would like to calculate the summation of moved and fixed matrix.
The purpose is to join two image without the spaces(zeros) between.
TEMP is the matrix and DATA is the moved matrix by LengthofZero.
To calculate the summation of different sizes matrix, I made the zeros to make the same size as below.
The below is the code I made, but I think there is more efficient ways available in Matlab.
Please help me please.
TEMP = A1; DATA = A2;
LengthofZero = 251;
% Move the data in the column direction by LengthofZero and summation of DATA & TEMP
[m1,n1] = size(TEMP);
[m2,n2] = size(DATA);
TEMP = [TEMP,zeros(m2,n2)];
DATA = [zeros(m1,n1),DATA];
DATA = circshift(DATA,-LengthofZero,2);

Accepted Answer

Image Analyst
Image Analyst on 4 Feb 2022
Is this what you mean:
subplot(2, 2, 1);
imshow(A1, []);
axis('image', 'on')
subplot(2, 2, 2);
imshow(A2, []);
axis('image', 'on')
[rows1, columns1] = size(A1);
[rows2, columns2] = size(A2);
% Make a canvass to hold the two images.
TEMP = [A1, zeros(size(A2))];
for row = 1 : rows1
% Find the last non-zero column in A1
col1 = find(A1(row, :), 1, 'last');
% Find the first non-zero column in A2
col2 = find(A2(row, :), 1, 'first');
% Extract the pixels from A2 that we need to paste onto TEMP
rowFromA2 = A2(row, col2:end);
% Get the length of the part we're going to paste
numColumns = length(rowFromA2);
% Paste that row from A2 from that column to the right.
TEMP(row, col1:col1 + numColumns - 1) = rowFromA2;
% Get rid of any columns in TEMP that are all zeros
allZeroColumns = all(TEMP == 0, 1);
TEMP(:, allZeroColumns) = []
% Display final image.
subplot(2, 2, 3:4);
imshow(TEMP, []);
axis('image', 'on')
Image Analyst
Image Analyst on 4 Feb 2022
circshift() will not slide the second piece up to the right side of the left piece like a jigsaw puzzle. There is no built-in function to do this automatically - you have to do it by using lower level functions like I did.
How big is your array? This should be pretty fast unless your matrices are like gigabytes in size, but for a few thousand by a few thousand matrix it should be pretty quick. If you have millions of rows it will of course take longer.

Sign in to comment.

More Answers (1)

Simon Chan
Simon Chan on 4 Feb 2022
Not sure it is efficient or not, just an optional way of doing it:
s1 = regionprops(bwareafilt(~A1,2),'BoundingBox'); % Use complement image and remove the smallest object
bbox1 = cat(1,s1.BoundingBox); % Find the bounding boxes coordinates
B1 = A1(:,1:floor(max(bbox1(:,1)))); % First image get the largest starting point of the Bounding Box
s2 = regionprops(bwareafilt(~A2,2),'BoundingBox');
bbox2 = cat(1,s2.BoundingBox);
B2 = A2(:,ceil(min(bbox2(:,1)+bbox2(:,3))):end); % Second image get the smallest ending point of the Bounding Box
B = [B1,B2]; % Combine them
  1 Comment
Smithy on 4 Feb 2022
Thank you very much for your answers. I really reeally appreciate with it. It works really well.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!