Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fast 2dimensional histograming in matlab

I have written a 2D histogram algorithm for 2 matlab vectors. Unfortunately, I cannot figure out how to vectorize it, and it is about an order of magnitude too slow for my needs. Here is what I have:

    function [ result ] = Hist2D( vec0, vec1 )
%Hist2D takes two vectors, and computes the two dimensional histogram
% of those images.  It assumes vectors are non-negative, and bins
% are the integers.
%
%  OUTPUTS
%      result - 
%         size(result) = 1 + [max(vec0) max(vec1)]
%         result(i,j)  = number of pixels that have value 
%                             i-1 in vec0 and value j-1 in vec1.

    result = zeros(max(vec0)+1, max(vec1)+1);

    fvec0 = floor(vec1)+1;
    fvec1 = floor(vec0)+1;

    % UGH, This is gross, there has to be a better way...
    for i = 1 : size(fvec0);
        result(fvec0(i), fvec1(i)) = 1 + result(fvec0(i), fvec1(i));
    end
end

Thoughts?

Thanks!! John

like image 927
John Avatar asked Jul 21 '11 14:07

John


People also ask

How do you plot a 2D histogram in Matlab?

Description. histogram2( X,Y ) creates a bivariate histogram plot of X and Y . The histogram2 function uses an automatic binning algorithm that returns bins with a uniform area, chosen to cover the range of elements in X and Y and reveal the underlying shape of the distribution.

What is a 2D histogram?

In image processing a 2D histogram shows the relationship of intensities at the exact position between two images. The 2D histogram is mostly used to compare 2 channels in a multi-channel images, where the x-axis represent the intensities of the first channel and the y-axis the intensities of the second channel.

What are bins in histogram Matlab?

histogram displays the bins as rectangles such that the height of each rectangle indicates the number of elements in the bin. histogram( X , nbins ) uses a number of bins specified by the scalar, nbins . histogram( X , edges ) sorts X into bins with the bin edges specified by the vector, edges .


1 Answers

Here is my version for a 2D histogram:

%# some random data
X = randn(2500,1);
Y = randn(2500,1)*2;

%# bin centers (integers)
xbins = floor(min(X)):1:ceil(max(X));
ybins = floor(min(Y)):1:ceil(max(Y));
xNumBins = numel(xbins); yNumBins = numel(ybins);

%# map X/Y values to bin indices
Xi = round( interp1(xbins, 1:xNumBins, X, 'linear', 'extrap') );
Yi = round( interp1(ybins, 1:yNumBins, Y, 'linear', 'extrap') );

%# limit indices to the range [1,numBins]
Xi = max( min(Xi,xNumBins), 1);
Yi = max( min(Yi,yNumBins), 1);

%# count number of elements in each bin
H = accumarray([Yi(:) Xi(:)], 1, [yNumBins xNumBins]);

%# plot 2D histogram
imagesc(xbins, ybins, H), axis on %# axis image
colormap hot; colorbar
hold on, plot(X, Y, 'b.', 'MarkerSize',1), hold off

hist2d

Note that I removed the "non-negative" restriction, but kept integer bin centers (this could be easily changed into dividing range into equally-sized specified number of bins instead "fractions").

This was mainly inspired by @SteveEddins blog post.

like image 144
Amro Avatar answered Oct 13 '22 20:10

Amro