help in creating a loop

Hi all,
I trying to correlate PIV images ( about 100 image). I used the below code that can correlate only the first 2 pair of images and I want to modify the loop so it can correlate the rest images. can somebody help me in modifying the loop?
for i= 1: 100;
for j= 1: 100;
max_correlation=0;
test_xmin=xgrid(i);
test_xmax=xgrid(j)+ 20 %w_width/2;
test_ymin=ygrid(i);
test_ymax=ygrid(j)+ 20 %w_width/2;
x_disp=0;
y_disp=0;
test_ima= Imagea(test_xmin:test_xmax, test_ymin:test_ymax);
test_imb= Imageb(test_xmin-x_disp_max:test_xmax+x_disp_max, test_ymin-y_disp_max:test_ymax+ y_disp_max);
correlation= normxcorr2(test_ima, test_imb);
[xpeak, ypeak]= find (correlation==max(correlation(:) ));
end
end
thanks in advance

1 个评论

for i= 1: 100;
for j= 1: 100;
max_correlation=0;
test_xmin=xgrid(i);
test_xmax=xgrid(j)+ 20 %w_width/2;
test_ymin=ygrid(i);
test_ymax=ygrid(j)+ 20 %w_width/2;
x_disp=0;
y_disp=0;
test_ima= Imagea(test_xmin:test_xmax, test_ymin:test_ymax);
test_imb= Imageb(test_xmin-x_disp_max:test_xmax+x_disp_max, test_ymin-y_disp_max:test_ymax+ y_disp_max);
correlation= normxcorr2(test_ima, test_imb);
[xpeak{i,j}, ypeak{i,j}]= find (correlation==max(correlation(:) ));
end
end

请先登录,再进行评论。

 采纳的回答

Umar
Umar 2024-7-8

0 个投票

Hi Mario,

Based on the provided code snippet, it seems like you are attempting to correlate PIV (Particle Image Velocimetry) images using a nested loop structure in MATLAB. To modify the loop to correlate all 100 PIV image pairs, you need to adjust the loop structure and indexing. The current code snippet provided runs nested loops for all image pairs but does not store or utilize the correlation results effectively. You can modify the loop as follows:

for i = 1:99

    for j = i+1:100
        max_correlation = 0;
        test_xmin = xgrid(i);
        test_xmax = xgrid(j) + 20;
        test_ymin = ygrid(i);
        test_ymax = ygrid(j) + 20;
        x_disp = 0;
        y_disp = 0;
        test_ima = Imagea(test_xmin:test_xmax, test_ymin:test_ymax);
        test_imb = Imageb(test_xmin - x_disp_max:test_xmax + x_disp_max, test_ymin - y_disp_max:test_ymax + y_disp_max);
        correlation = normxcorr2(test_ima, test_imb);
        [xpeak, ypeak] = find(correlation == max(correlation(:)));
        % Store or process correlation results here
    end
end

This modified loop structure ensures that each image pair is correlated exactly once, avoiding redundant calculations. Remember to include the necessary logic to store or process the correlation results as needed for your application.

If you encounter any issues or need further assistance, feel free to ask for more help!

10 个评论

Thanks Umar for your assistance. It works!!
My current code record the correlation between the last two pair of images. Could you please advice me how to record the correlation of the whole 100 pair of images in a matrix?
Thanks in advance
Hi Mario,
I am confused. It sounds like the question has already been answered. Please let me know if I am missing something.
Hi Umar,
Sorry for my delay reply. I was trying with the code. The problem that the correlation command in my code is giving me double matrix with size 500x500 and I need it to be 500x500x100.
So I try to modify the below line
correlation = normxcorr2(test_ima, test_imb)
But unfortunately it always give me error. Can you please advice me how to do it?
Thanks in advance
Mario
Hi Mario,
To obtain a 500x500x100 matrix, you can approach it by iterating over your images or by using a sliding window technique along the third dimension. Here's a possible solution using a loop:
% Initialize an empty matrix to store the correlation results
correlation = zeros(500, 500, 100);
for i = 1:100
% Extract the ith slice of test_imb
test_imb_slice = test_imb(:, :, i);
% Perform correlation between test_ima and test_imb_slice
correlation(:, :, i) = normxcorr2(test_ima, test_imb_slice);
end
This code will help you to iterate over each slice of the third dimension (100 slices) of test_imb and calculate the correlation with test_ima using normxcorr2. The results will be stored in the 'correlation' matrix with dimensions 500x500x100. So, by implementing this approach, you should be able to obtain the desired 500x500x100 matrix for your correlation calculations. If you encounter any errors or need further assistance, feel free to provide additional details for more specific guidance.
Hi Umar,
I tried the below code as follow;
for i= 1:2-1 %:w_xcount-1;
for j= i+1:w_ycount;
max_correlation=0;
test_xmin=xgrid(i);
test_xmax=xgrid(j)+w_width;
test_ymin=ygrid(i);
test_ymax=ygrid(j)+ w_width;
x_disp=0;
y_disp=0;
test_ima= Imagea(test_xmin:test_xmax, test_ymin:test_ymax,1:25);
test_imb= Imageb(test_xmin-x_disp_max:test_xmax+x_disp_max, test_ymin-y_disp_max:test_ymax+ y_disp_max,1:25);
test_imb_slice= test_imb(:,:,1:25)
correlation(:,:,i)= normxcorr2(test_ima, test_imb_slice);
end
end
but I got the below error
Error using normxcorr2
Expected input number 1, T, to be two-dimensional.
Error in normxcorr2>ParseInputs (line 261)
validateattributes(T,{'logical','numeric'},...
Error in normxcorr2 (line 61)
[T, A] = ParseInputs(varargin{:});
do you any suggestions?
Thanks in advance
Mario
test_ima= Imagea(test_xmin:test_xmax, test_ymin:test_ymax,1:25);
test_ima includes 25 slice plains, so it is not 2D.
Mario
Mario 2024-7-13
编辑:Mario 2024-7-13
Hi,
I am still trying with the below code
for i= 1:w_xcount-1;
for j= i+1:w_ycount;
max_correlation=0;
test_xmin=xgrid(i);
test_xmax=xgrid(j)+w_width;
test_ymin=ygrid(i);
test_ymax=ygrid(j)+ w_width;
x_disp=0;
y_disp=0;
test_ima= Imagea(test_xmin:test_xmax, test_ymin:test_ymax);
test_imb= Imageb(test_xmin-x_disp_max:test_xmax+x_disp_max, test_ymin-y_disp_max:test_ymax+ y_disp_max);
correlation(:,:,:)= normxcorr2(test_ima, test_imb);
end
end
but I got error related to the size of correlation differ and I do'nt know how to bypass this.
the error appears as:
Unable to perform assignment because the size of the left side is 27-by-27 and the size of the right side is 29-by-29.
thanks in advance
Hi Mario,
Could you please provide details about your code assignment to resolve your problem.
Now, to resolve problem in the code mentioned above, you need to dynamically adjust the size of the `correlation` matrix based on the output from `normxcorr2`, which should be able to resolve the size mismatch error, so you can continue with your correlation calculations successfully.
Please let us know if you have any further questions.
Hi Umar,
I am trying to implement two point correlation between PIV images using the pervious mentioned code. Can you advise me how can I dynamically adjust the size of the `correlation` matrix?
thanks in advance
Mario
Hi Mario,
It is adjusted based on the dimensions of the images being compared (`test_ima` and `test_imb`). By calculating the appropriate size of the correlation matrix before computing the normalized cross-correlation.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Mathematics and Optimization 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by