Hello Mat

 找回密码
 立即注册
查看: 5999|回复: 0

Gamma图像增强处理

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2017-8-29 22:14:45 | 显示全部楼层 |阅读模式
Gamma图像增强处理:
将设定的亮度区间拉伸或缩小处理;
I = imread('pout.tif');
J = imadjust_ysw(I);
  1. function out = imadjust_ysw(img)
  2. %IMADJUST Adjust image intensity values or colormap.
  3. %   J = IMADJUST(I,[LOW_IN; HIGH_IN],[LOW_OUT; HIGH_OUT],GAMMA) maps the
  4. %   values of I to new values in J as described in the previous syntax.
  5. %   GAMMA specifies the shape of the curve describing the relationship
  6. %   between the values in I and J. If GAMMA is less than 1, the mapping is
  7. %   weighted toward higher (brighter) output values. If GAMMA is greater
  8. %   than 1, the mapping is weighted toward lower (darker) output values. If
  9. %   you omit the argument, GAMMA defaults to 1 (linear mapping).

  10. %   LOW_IN, HIGH_IN, LOW_OUT, HIGH_OUT all must be in the range [0,1];
  11. %   GAMMA         real, double, nonnegative

  12. if nargin == 1
  13.     imageType = 'intensity';
  14.     lowIn = 0.3059;
  15.     highIn = 0.6314;
  16.     lowOut = 0;
  17.     highOut = 1;
  18.     gamma = 1;
  19. end

  20. if ~isfloat(img) && numel(img) > 65536
  21.     % integer data type image with more than 65536 elements
  22.     out = adjustWithLUT(img,lowIn,highIn,lowOut,highOut,gamma);
  23. else
  24.     classin = class(img);
  25.     classChanged = false;
  26.     if ~isa(img,'double')
  27.         classChanged = true;
  28.         img = im2double(img);
  29.     end

  30.     if strcmp(imageType, 'intensity')
  31.         out = adjustGrayscaleImage(img,lowIn,highIn,lowOut,highOut,gamma);
  32.     elseif strcmp(imageType, 'indexed')
  33.         out = adjustColormap(img,lowIn,highIn,lowOut,highOut,gamma);
  34.     else
  35.         out = adjustTruecolorImage(img,lowIn,highIn,lowOut,highOut,gamma);
  36.     end
  37.    
  38.     if classChanged
  39.         out = images.internal.changeClass(classin,out);
  40.     end

  41. end

  42. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  43. function out = adjustWithLUT(img,lowIn,highIn,lowOut,highOut,gamma)

  44. imgClass = class(img);
  45. out = zeros(size(img),imgClass);

  46. %initialize for lut

  47. switch imgClass
  48.     case 'uint8'
  49.         lutLength = 256;
  50.         conversionFcn = @im2uint8;
  51.     case 'uint16'
  52.         lutLength = 65536;
  53.         conversionFcn = @im2uint16;
  54.     case 'int16'
  55.         lutLength = 65536;
  56.         conversionFcn = @im2int16;
  57.     otherwise
  58.         error(message('images:imadjust:internalError'))
  59. end

  60. for p = 1:size(img,3)
  61.     lut = linspace(0,1,lutLength);
  62.     scalingFactor = 1;
  63.     lut = adjustArray(lut,lowIn(p),highIn(p),lowOut(p),highOut(p), ...
  64.         gamma(p),scalingFactor);
  65.     lut = conversionFcn(lut);
  66.     out(:,:,p) = intlut(img(:,:,p),lut);
  67. end

  68. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  69. function out = adjustColormap(cmap,lIn,hIn,lOut,hOut,g)

  70. % expansion factor that can expand a 1-by-3 range to the size of cmap.
  71. expansionFactor = ones(size(cmap,1), 1);
  72. out = adjustArray(cmap, lIn, hIn, lOut, hOut, g, expansionFactor);

  73. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  74. function out = adjustGrayscaleImage(img,lIn,hIn,lOut,hOut,g)

  75. expansionFactor = 1;
  76. out = adjustArray(img, lIn, hIn, lOut, hOut, g, expansionFactor);

  77. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  78. function out = adjustTruecolorImage(rgb,lIn,hIn,lOut,hOut,g)

  79. out = zeros(size(rgb), 'like', rgb);
  80. expansionFactor = 1;
  81. for p = 1 : 3
  82.     out(:,:,p) = adjustArray(rgb(:,:,p), lIn(p),hIn(p), lOut(p), ...
  83.         hOut(p), g(p), expansionFactor);
  84. end

  85. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  86. function out = adjustArray(img,lIn,hIn,lOut,hOut,g,d)

  87. %make sure img is in the range [lIn;hIn]
  88. img(:) =  max(lIn(d,:), min(hIn(d,:),img));

  89. out = ( (img - lIn(d,:)) ./ (hIn(d,:) - lIn(d,:)) ) .^ (g(d,:));
  90. out(:) = out .* (hOut(d,:) - lOut(d,:)) + lOut(d,:);
复制代码


参考:【1】http://pan.baidu.com/s/1dF0B4Tr

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-24 21:06 , Processed in 0.230743 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表