how to calculate image pixels shift

12 ビュー (過去 30 日間)
Nir Shwartz
Nir Shwartz 2021 年 12 月 1 日
編集済み: Manish 2024 年 10 月 25 日
hello,
i have two similar images, but one has a pixels shift compare to the other. how can i calculate the the offset (amount of pixels) for axis x and y?

回答 (1 件)

Manish
Manish 2024 年 10 月 25 日
編集済み: Manish 2024 年 10 月 25 日
Hi,
I see that you want to calculate pixel offset between two similar images.
You can achieve this by following the below steps:
  • Process the Images: Start by reading your images that you want to process.
  • Align Usingnormxcorr2: Use thenormxcorr2function to perform normalized cross-correlation on the images.
  • Locate the peak value and calculate the offset based on the position of the peak.
Here is the sample code of above steps:
image1 = imread('img1.png');
image2 = imread('img2.png');
crossCorrMatrix = normxcorr2(image2, image1);
[maxCorr, maxIndex] = max(abs(crossCorrMatrix(:)));
[yPeak, xPeak] = ind2sub(size(crossCorrMatrix), maxIndex);
yOffset = yPeak - size(image2, 1);
xOffset = xPeak - size(image2, 2);
fprintf('Offset in X: %d pixels\n', xOffset);
fprintf('Offset in Y: %d pixels\n', yOffset);
Refer to the documentation link below for more information on thenormxcorr2function:
Hope it helps!

カテゴリ

Help Center および File ExchangeImages についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by