For a project I'm asked to use rough interpolation on an image which is originally mxn values and convert it to an MxN matrix where each value from mxn gets duplicated nearby. So if my mxn matrix is:
A = [1 2 3;4 5 6]
A m = 2, n = 3 matrix and I want to interpolate by M = 2, N = 2 then my new matrix would be m*M x n*N (4x6 in this example):
A_int = [1 1 2 2 3 3; 1 1 2 2 3 3; 4 4 5 5 6 6; 4 4 5 5 6 6]
The code I'm using works fine but I'm wondering if there is an easier way to do this. Essentially it'll copy row 1 and repeat it M times downwards, then take row 2 and copy it M times downward, etc. Then take column 1 and copy it N times to the right, then column 2, etc.
function M_save = sample_interpolation(M, m, n)
[rows, cols, chs] = size(M); %determine the size of the incoming matrix
rows_new = rows * m; %calculate the size of the new matrix
cols_new = cols * n;
M_save = uint8(zeros(rows_new, cols_new, chs)); %create a new matrix full of zeros
a = 1:m:rows_new; %incremental value in step size of m
b = 1:n:cols_new; %incremental value in step size of n
M_save(a,b,:) = M(:,:,:); %save each incremented value as the value from the original matrix
for a = 1:m:rows_new %from the first row to the last skipping rows of zeros
for i = 1:m-1 %from the current row to the n-1 row upstream
M_save(i+a,:,:) = M_save(a,:,:); %save the value from a position in i+a
end
end
for a = 1:n:cols_new %from the first col to the last skipping rows of zeros
for i = 1:n-1 %from the current col to the m-1 row upstream
M_save(:,a+i,:) = M_save(:,a,:); %save the value from a position in i+a
end
end
figure('Name','Sample Effect')
imshow(M_save)
title([num2str(m),'x' ,num2str(n),' interpolation'])
end

 채택된 답변

Guillaume
Guillaume 2018년 4월 17일

1 개 추천

Assuming you're on R2015b or later, this is extremely easy:

A = [1 2 3;4 5 6];
M = 2;
N = 2;
A_int = repelem(A, M, N)

댓글 수: 1

Aron
Aron 2018년 4월 17일
Thank you so much. I knew there had to be a simpler way to do this and I appreciate your help. My for loops work, it's just convoluted.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2018년 4월 17일

댓글:

2018년 4월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by