MI is a good approach to align two images from different sensor. Here is a function with the simplest form to calculate the mutual information between two images.
the function f=cal_mi(I1,I2) is in the test_mi.m file.
your comment or suggestion will be much appreciated.
@HUxb, great work.
However, I think that the function can be optimized to speed up calculations. I have identified two such code portions.
1. Line 12
f = [ f cal_mi(p2,p3)]; can be easily replaced with f(t+51) = cal_mi(p2,p3);
This way array element is simply updated and matlab doesn't form new array every time.
2. Line 39 to 42
ht = zeros(N1,N2); % Joint histogram
for n = 1:length(I1);
ht(I1(n)-min1+1,I2(n)-min2+1) = ht(I1(n)-min1+1,I2(n)-min2+1) + 1;
Above piece of code can be replaced by just one line of code
ht = accumarray([I1 I2]+1,1);
Please note that loop slows down the function.
By making these two changes I was able to reduce the computation time on my laptop from 1.23s to 0.56s which corresponds to a gain of 2.2
Hi HU xb,
thank you for the code. Is all I needed for understand the Mutual Information.
I think you are using the im2 twice in the input of the cal_mi function. In the line 11 the code is:
p3 = im2(h/2-50:h/2+50,t+w/2-50:t+w/2+50); % a patch around the middle of im3
p3 = im3(h/2-50:h/2+50,t+w/2-50:t+w/2+50); % a patch around the middle of im3
I think that there is a little mistake at the end of that function.
f = -(Hx+Hy)/h_xy; % Mutual information
I think it should be
f = -(Hx+Hy) + h_xy; % Mutual information
Thank you HU xb very much for your code ,I have a problem my images are grayscale 32 bits '.tif' I got this error when I used your code:
Undefined function 'cal_mi' for input arguments of type 'single'.
Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.