필터 지우기
필터 지우기

Cross correlation of images, adding a loop to go through multiple images

조회 수: 13 (최근 30일)
Hello,
I have a code that cross correlates an image 1 and matches it to an image 2. It then outputs the cross-correlation matrix (X).
How can I put a loop around this code for a stack of 3 images (like my_mat_file.mat that I have attached), and save all the cross correlation matrices that result from the loop.
This is my code for cross-correlation of images: (I have also attached my .mat data file to be used with this code)
%%
clc;
clear;
close all;
load('my_mat_file.mat');
%%
template=ims(:,:,1);
full=ims(:,:,2);
S_full = size(full);
S_temp = size(template);
%%
X=normxcorr2(template, full);
r=max(X(:));
[i,j]=find(X==r);
%%
figure;
subplot(2,2,1), imagesc(full), title('image2');
subplot(2,2,2), imagesc(template), title('image1');
subplot(2,2,3), imagesc(X), rectangle('Position', [j-20 i-20 40 40]);
R = zeros(S_temp);
shift_a = [0 0];
shift_b = [i j] - S_temp;
R((1:S_full(1))+shift_a(1), (1:S_full(2))+shift_a(2)) = full;
R((1:S_temp(1))+shift_b(1), (1:S_temp(2))+shift_b(2)) = template;
subplot(2,2,4), imagesc(R);
%%
figure;
mesh(X);

채택된 답변

Image Analyst
Image Analyst 2022년 4월 4일
I don't see a stack of 3 images and a template image in there. ims is a uint16 RGB image. It doesn't make sense to have the template be the full size red channel, and then the other gray scale image be the green channel image. Since your images are so similar, it would just give the trivial result that the closest match is where there is no shift at all. Your template should be smaller.
  댓글 수: 10
Gucci
Gucci 2022년 4월 4일
편집: Gucci 2022년 4월 4일
Thanks @Image Analyst, this works,
Is there anything I could do to the code to make it run faster (for a set of thousands of images) and still obtain the same results? Maybe I could preallocate the R and C variables, but i don't know how
Here is the updated code,
%%
clc;
clear;
close all;
load('my_mat_file.mat');
%%
for k = 2 : size(ims, 3)
template = ims(:, :, k-1); % Template is prior image.
full = ims(:, :, k); % This is the "current" image.
S_temp = size(template);
S_full = size(full);
%%
X=normxcorr2(template, full);
r=max(X(:));
[i,j]=find(X==r);
%%
B=im2gray(X);
temp=graythresh(B);
bin_im=im2bw(B,temp);
%% centroid of cross-correlation matrix
tot_mass = sum(bin_im(:));
[ii,jj] = ndgrid(1:size(bin_im,1),1:size(bin_im,2));
R(k) = sum(ii(:).*bin_im(:))/tot_mass;
C(k) = sum(jj(:).*bin_im(:))/tot_mass;
end
Image Analyst
Image Analyst 2022년 4월 4일
You could get rid of this
r=max(X(:));
[i,j]=find(X==r);
since you don't seem to do anything with (the badly-named) i and j.
I still don't know why you're doing this. The cross correlation will just give you how much the weighted centroid is shifted from the prior image. You can get that a lot faster with the regionprops() code I first gave you. Your cross correlation matrix is going to be 4 times as big as your input matrices. Plus it's computationally intensive. It's going to take a long time if you use full size matrices. I've said it before, there's just no need to do it.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Explore and Edit Images with Image Viewer App에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by