|
Gamma图像增强处理:
将设定的亮度区间拉伸或缩小处理;
I = imread('pout.tif');
J = imadjust_ysw(I);
- function out = imadjust_ysw(img)
- %IMADJUST Adjust image intensity values or colormap.
- % J = IMADJUST(I,[LOW_IN; HIGH_IN],[LOW_OUT; HIGH_OUT],GAMMA) maps the
- % values of I to new values in J as described in the previous syntax.
- % GAMMA specifies the shape of the curve describing the relationship
- % between the values in I and J. If GAMMA is less than 1, the mapping is
- % weighted toward higher (brighter) output values. If GAMMA is greater
- % than 1, the mapping is weighted toward lower (darker) output values. If
- % you omit the argument, GAMMA defaults to 1 (linear mapping).
- % LOW_IN, HIGH_IN, LOW_OUT, HIGH_OUT all must be in the range [0,1];
- % GAMMA real, double, nonnegative
- if nargin == 1
- imageType = 'intensity';
- lowIn = 0.3059;
- highIn = 0.6314;
- lowOut = 0;
- highOut = 1;
- gamma = 1;
- end
- if ~isfloat(img) && numel(img) > 65536
- % integer data type image with more than 65536 elements
- out = adjustWithLUT(img,lowIn,highIn,lowOut,highOut,gamma);
- else
- classin = class(img);
- classChanged = false;
- if ~isa(img,'double')
- classChanged = true;
- img = im2double(img);
- end
- if strcmp(imageType, 'intensity')
- out = adjustGrayscaleImage(img,lowIn,highIn,lowOut,highOut,gamma);
- elseif strcmp(imageType, 'indexed')
- out = adjustColormap(img,lowIn,highIn,lowOut,highOut,gamma);
- else
- out = adjustTruecolorImage(img,lowIn,highIn,lowOut,highOut,gamma);
- end
-
- if classChanged
- out = images.internal.changeClass(classin,out);
- end
- end
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- function out = adjustWithLUT(img,lowIn,highIn,lowOut,highOut,gamma)
- imgClass = class(img);
- out = zeros(size(img),imgClass);
- %initialize for lut
- switch imgClass
- case 'uint8'
- lutLength = 256;
- conversionFcn = @im2uint8;
- case 'uint16'
- lutLength = 65536;
- conversionFcn = @im2uint16;
- case 'int16'
- lutLength = 65536;
- conversionFcn = @im2int16;
- otherwise
- error(message('images:imadjust:internalError'))
- end
- for p = 1:size(img,3)
- lut = linspace(0,1,lutLength);
- scalingFactor = 1;
- lut = adjustArray(lut,lowIn(p),highIn(p),lowOut(p),highOut(p), ...
- gamma(p),scalingFactor);
- lut = conversionFcn(lut);
- out(:,:,p) = intlut(img(:,:,p),lut);
- end
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- function out = adjustColormap(cmap,lIn,hIn,lOut,hOut,g)
- % expansion factor that can expand a 1-by-3 range to the size of cmap.
- expansionFactor = ones(size(cmap,1), 1);
- out = adjustArray(cmap, lIn, hIn, lOut, hOut, g, expansionFactor);
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- function out = adjustGrayscaleImage(img,lIn,hIn,lOut,hOut,g)
- expansionFactor = 1;
- out = adjustArray(img, lIn, hIn, lOut, hOut, g, expansionFactor);
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- function out = adjustTruecolorImage(rgb,lIn,hIn,lOut,hOut,g)
- out = zeros(size(rgb), 'like', rgb);
- expansionFactor = 1;
- for p = 1 : 3
- out(:,:,p) = adjustArray(rgb(:,:,p), lIn(p),hIn(p), lOut(p), ...
- hOut(p), g(p), expansionFactor);
- end
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- function out = adjustArray(img,lIn,hIn,lOut,hOut,g,d)
- %make sure img is in the range [lIn;hIn]
- img(:) = max(lIn(d,:), min(hIn(d,:),img));
- out = ( (img - lIn(d,:)) ./ (hIn(d,:) - lIn(d,:)) ) .^ (g(d,:));
- out(:) = out .* (hOut(d,:) - lOut(d,:)) + lOut(d,:);
复制代码
参考:【1】http://pan.baidu.com/s/1dF0B4Tr
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|