Cross correlation of images, adding a loop to go through multiple images
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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);
0 commentaires
Réponse acceptée
Image Analyst
le 4 Avr 2022
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 commentaires
Image Analyst
le 4 Avr 2022
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Explore and Edit Images with Image Viewer App dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!