フィルターのクリア

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

13 ビュー (過去 30 日間)
Gucci
Gucci 2022 年 4 月 3 日
コメント済み: Image Analyst 2022 年 4 月 4 日
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 Center および File ExchangeExplore 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