필터 지우기
필터 지우기

How to center data on the max value

조회 수: 2 (최근 30일)
Ole
Ole 2021년 8월 28일
댓글: Ole 2021년 8월 28일
How to center 2D matrix (NxM) on the pixel with max value and fill rest with zeros?
The end result should be (NxM) with centered max.
x = -2:0.5:2;
y = 1:0.2:2;
[X,Y] = meshgrid(x,y);
Z = peaks(X,Y)
[row, col] = find(ismember(Z, max(Z(:))));
N = length(x);
M= length(y);
row = row(1);
col = col(1);
if row > N/2 && col > M/2
Z(1:(N/2-row),:) = [];
Z = [Z; zeros((N/2-row),M)];
Z(1:(M/2-row),:) = [];
Z = [Z; zeros((M/2-row),N)];
elseif row < N/2 && col < M/2
Z(:,end-(N/2-col):end) = [];
Z = [zeros(M,(N/2-col)) Z];
Z(:,end-(M/2-col):end) = [];
Z = [zeros(N,(M/2-col+1)) Z];
elseif row > N/2 && col < M/2
Z(1:(N/2-row),:) = [];
Z = [Z; zeros((N/2-row),M)];
Z(:,end-(M/2-col):end) = [];
Z = [zeros(N,(M/2-col+1)) Z];
elseif row < N/2 && col > M/2
Z(:,end-(N/2-col):end) = [];
Z = [zeros(M,(N/2-col)) Z];
Z(1:(M/2-row),:) = [];
Z = [Z; zeros((M/2-row),N)];
else
Z = Z;
end

채택된 답변

Matt J
Matt J 2021년 8월 28일
Without the Image Processing toolbox? With the toolbox, you can simply use imtranslate().
  댓글 수: 3
Matt J
Matt J 2021년 8월 28일
imtranslate does that by default. Example:
A=rand(100);
B=imtranslate(A,[30,40]);
montage({A,B},'Back','w','Bord',[2,10])
Ole
Ole 2021년 8월 28일
Thank you!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by