Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. % EE368/CS232 Digital Image Processing
  2. % Bernd Girod
  3. % Department of Electrical Engineering, Stanford University
  4.  
  5. % Script by Qiyuan Tian and David Chen
  6. % Contrast-limited histogram equalization
  7.  
  8. clear, clc, close all
  9.  
  10. % Load test image
  11. %img = imread('moon.jpg');
  12. img = imread('brain.jpg');
  13. % img2 = imread('bay.jpg');
  14.  
  15. [counts, index] = imhist(img);
  16. maxCount = max(counts(:));
  17.  
  18. % Loop over clip ratios
  19. clipRatio = [1; 0.7; 0.4; 0.1]; % Ratio 1 means no clipping
  20. limitEqImg = cell(1, numel(clipRatio));
  21. LUT = uint8(zeros(numel(clipRatio), 256));
  22. for i = 1 : numel(clipRatio)
  23. % Clip histogram
  24. clip = clipRatio(i) * maxCount;
  25. clippedCounts = (counts < clip) .* counts + (counts >=clip) * clip;
  26.  
  27. % Construct a 1 dimensional virtual image for histeq to get mapping
  28. % function
  29. clippedImg = [];
  30. for level = 0 : 255
  31. clippedImg = cat(2, clippedImg, level * ones(1, fix(clippedCounts(level + 1))));
  32. end
  33. [temp, T] = histeq(uint8(clippedImg));
  34.  
  35. % Apply mapping function
  36. LUT(i, :)= uint8(T * 255);
  37. limitEqImg{i} = intlut(img, LUT(i, :));
  38. end
  39.  
  40. % Show images
  41. figure('Name','Clip Ratio for Conrast Histogram Equalization','NumberTitle','off');, clf;
  42. for i = 1 : numel(clipRatio)
  43. subplot(1, numel(clipRatio), i), imshow(limitEqImg{i});
  44. title(['Clip at ' num2str(clipRatio(i)) '*max']);
  45. imwrite(limitEqImg{i}, ['CLHE_ratio' num2str(clipRatio(i)) '.png'])
  46. end
  47.  
  48. %%
  49. % Show histogram and mapping function
  50. figure(2), clf;
  51. set(gcf, 'Position', [100 100 600 290]);
  52. subplot(1, 2, 1),
  53. bar(index, counts), axis([0 255 0 120000]);
  54. title('Histogram');
  55. xlabel('Gray level'); ylabel('Count');
  56. set(gca, 'FontSize', 10);
  57.  
  58. subplot(1, 2, 2), plot(LUT(1:4,:)', 'LineWidth', 2);
  59. legend(['Clip limit = ' num2str(clipRatio(1))], ['Clip limit = ' num2str(clipRatio(2))], ...
  60. ['Clip limit = ' num2str(clipRatio(3))], 'Original histogram', ...
  61. 'Location', 'SouthEast');
  62. title('Mapping functions');
  63. xlabel('Input gray level'); ylabel('Output gray level');
  64. axis([0 255 0 255]);
  65. set(gca, 'FontSize', 10);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement