How to make summation of moved and fixed matrix ?

3 vues (au cours des 30 derniers jours)
Smithy le 4 Fév 2022
Commenté : Smithy le 4 Fév 2022
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);

Réponse acceptée

Image Analyst
Image Analyst le 4 Fév 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')
  2 commentaires
Smithy le 4 Fév 2022
Thank you very much for your answers. I really reeally appreciate with it.
My first trial was for loop as your answers. But due to the size of my original data. it takses almost 10 min.
My mentioned code takes almost 4 min.
Therfore, I would like to know more suitable way for matlab manipulation.
I assume there might be helpful built-in function.
if not available, I would like to know the more efficient way to translate the matrix.
To use the circshift, I need to make the spaces to zero array for Non-circular shift.
Also I tried with imtranslate function. But the data disappear after big shifting.
I assume it also make the spaces to zero array.
Is there more efficient way?
Image Analyst
Image Analyst le 4 Fév 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.

Connectez-vous pour commenter.

Plus de réponses (1)

Simon Chan
Simon Chan le 4 Fév 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 commentaire
Smithy le 4 Fév 2022
Thank you very much for your answers. I really reeally appreciate with it. It works really well.

Connectez-vous pour commenter.


En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by