Advertisement
LightningIsMyName

[Matlab] Histogram Equalization

Mar 27th, 2011
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.26 KB | None | 0 0
  1. % BI_histeq.m
  2. % Histogram Equalization operation
  3. % Written by Barak Itkin, http://barak-itkin.blogspot.com
  4.  
  5. %%%%%%%%%%%%%%%%%%%% Image commands "cheat sheet" %%%%%%%%%%%%%%%%%%%%%%%%%
  6.  
  7. % im = imread (path);           % read an image file (try 'cameraman.tif'
  8. %                               % and 'peppers.png')
  9. % info = imfinfo (path);        % Get information about the image (use the
  10. %                               % same path!). Try printing this ;)
  11. %
  12. % if info.ColorType == 'truecolor' % Check if the image is RGB type
  13. %    img = rgb2gray(im);        % If so, convert to gray 0-255
  14. % else                          % Otherwise (if gray), do nothing
  15. %    img = im;                  % Nothing :)
  16. % end                           % End of if statement
  17. %
  18. % imgd = double(img) / 255;     % convert from range 0-255 to 0-1
  19. % imshow (imgd);                % Display some image in rgb/gray/0-1/0-255
  20.  
  21. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  22.  
  23. %%% BI_histeq (img, outputLevels) %%%
  24. % img           = Input image, one gray channel with values in range 0-1
  25. % outputLevels  = Maximal amount of output levels in the equalized
  26. %                 histogram, 256 if no value is explicitly specified
  27. % imeq (return) = an image with equalized histogram
  28.  
  29. % Example useage - imshow (BI_histeq (imgd)
  30. function imeq = BI_histeq (img, outputLevels)
  31.  
  32. % Check if number of arguments was less than 2. If so, then no value was
  33. % passed for the outputLevels parameter - so use 256
  34. if nargin < 2
  35.    outputLevels = 256;
  36. end
  37.  
  38. % Create a result image, initialize it to zeros
  39. imeq = zeros (size(img));
  40.  
  41. % Total number of pixels (in source image)
  42. total = numel (img);
  43.  
  44. % Create a sorted list of the output levels in ascending order
  45. outstep = 1/(outputLevels-1);
  46. b = 0:outstep:1;
  47.  
  48. % Create a sorted list of the intput levels in ascending order
  49. a = unique(img);
  50. % count the amount of input pixels
  51. jlim = numel(a);
  52.  
  53. % Initialize loop parameters
  54. % An index over the input levels
  55. j = 1;
  56. % A counter of how many pixels we already converted (range [0-total])
  57. sigma = 0;
  58.  
  59.  
  60. % Pass over all the output levels
  61. for bi=b
  62.  
  63.    % Pass over input levels we haven't covered yet
  64.    % Note that we are adding one step, to make sure that for output level
  65.    % 0, we accumulate pixels until we reached the brightness of the next
  66.    % step. This way, we won't have 0 pixels in output level 0
  67.    while sigma < (bi + outstep) * total && j <= jlim
  68.  
  69.        % Create a matrix with 0's and 1's, indicating where in the source
  70.        % image we have the current input level
  71.        temp = (img == a(j));
  72.  
  73.        % Add to the result image, areas with the current input level
  74.        % colored with the current output level
  75.        imeq = imeq + temp * bi;
  76.  
  77.        % Add the amount of pixels we converted in this step, to the
  78.        % total amount of pixels we converted
  79.        % MATLAB: M(:) makes a vector containing all the elements in the
  80.        %         matrix M. Remember also that temp had 0's and 1's
  81.        %         indicating where we matched the input level (match=1)
  82.        sigma = sigma + sum (temp(:));
  83.  
  84.        % Progress to the next input level
  85.        j = j + 1;
  86.  
  87.    % End the iteration over input levels
  88.    end
  89.  
  90. % End the iteration over output levels
  91.  
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement