在图像配准的上下文中,我必须计算两个图像的联合直方图,即每个直方图(i,j)包含在第一个图像中强度为i而在第二个图像中为j的像素数。
要做到这一点,我循环强度:
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
然而,这样的算法对于我的项目来说太慢了,我试图在没有for循环的情况下做到这一点,但到目前为止,我还没有找到任何解决方案来解决我的问题。
我希望你们能帮助我解决它;)