0

In the context of image registration, I have to compute the joint histogram of two images i.e. each histogram(i,j) contains the number of pixels whose intensity is i in the first image and j in the second one.

To do this I loop on the intensities:

clear all; close all; clc

M = 1024;
N = 1024;

world_map=NaN(M,N,2);

rail_1 = rgb2gray(imread('img2.bmp'));

% Initial misregistration
dx = 12;
dy = 15;

[M1,N1] = size(rail_1);

% Center the 1st image on the world_map first layer
world_map(floor(M/2-M1/2):floor(M/2+(M1/2-1)),floor(N/2-N1/2):floor(N/2+(N1/2-1)),1) = rail_1;

% Translate the same image on the world_map second layer
world_map(floor(M/2-M1/2-dx):floor(M/2+(M1/2-1-dx)),floor(N/2-N1/2-dy):floor(N/2+(N1/2-1)-dy),2) = rail_1;


figure
h1=imagesc(world_map(:,:,1));
hold on
h2=imagesc(world_map(:,:,2));
set(h2,'AlphaData',0.5)
colormap gray

% find the overlapping area of the two images
overlap = find(~isnan(world_map(:,:,1)) & ~isnan(world_map(:,:,2)));
rail_1 = world_map(:,:,1);
rail_2 = world_map(:,:,2);

% compute the joint histogram (we here suppose that we have 256 gray level)
histo = zeros(256,256);

for i=1:256
    for j=1:256

        histo(i,j) = numel(find(rail_1(overlap)==i-1 & rail_2(overlap)==j-1));

    end
end

However such an algorithm is too much slow for my project and I have tried to do this without for loops but,so far, I have not find any solution to solve my problem.

I hope you guys could help me to solve it ;)

neon29
  • 179
  • 1
  • 3
  • 13
  • Have you tried to [profile](http://www.mathworks.com/help/matlab/ref/profile.html) your code to determine what is causing it to be slow? – ioums Jun 11 '15 at 20:14
  • Don't let the title mislead you. In order to compute the joint entropy in the duplicate post, you need to know how to calculate the joint histogram, which my answer describes. It can be done in three lines of code. – rayryeng Jun 12 '15 at 04:43
  • Thanks for your answers. I have used accumarray as you suggest in the other post. However, I was wondering wether it could be possible to force accumarray to provide the joint histogram between 0 and 255 even if some of these intensity values does not exist in both images ? – neon29 Jun 17 '15 at 11:57

0 Answers0