histoMatch.m 858 B

1234567891011121314151617181920212223242526272829303132333435
  1. % RES = histoMatch(MTX, N, X)
  2. %
  3. % Modify elements of MTX so that normalized histogram matches that
  4. % specified by vectors X and N, where N contains the histogram counts
  5. % and X the histogram bin positions (see histo).
  6. % Eero Simoncelli, 7/96.
  7. function res = histoMatch(mtx, N, X)
  8. if ( exist('histo') == 3 )
  9. [oN, oX] = histo(mtx(:), size(X(:),1));
  10. else
  11. [oN, oX] = hist(mtx(:), size(X(:),1));
  12. end
  13. oStep = oX(2) - oX(1);
  14. oC = [0, cumsum(oN)]/sum(oN);
  15. oX = [oX(1)-oStep/2, oX+oStep/2];
  16. N = N(:)';
  17. X = X(:)';
  18. N = N + mean(N)/(1e8); %% HACK: no empty bins ensures nC strictly monotonic
  19. nStep = X(2) - X(1);
  20. nC = [0, cumsum(N)]/sum(N);
  21. nX = [X(1)-nStep/2, X+nStep/2];
  22. nnX = interp1(nC, nX, oC, 'linear');
  23. if ( exist('pointOp') == 3 )
  24. res = pointOp(mtx, nnX, oX(1), oStep);
  25. else
  26. res = reshape(interp1(oX, nnX, mtx(:)),size(mtx,1),size(mtx,2));
  27. end