⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imadjust.m

📁 有关matlab的电子书籍有一定的帮助希望有用
💻 M
字号:
function out = imadjust(img,lowhigh,bottop,gamma)
%IMADJUST Adjust image intensity values or colormap.
%   J = IMADJUST(I,[LOW HIGH],[BOTTOM TOP],GAMMA) transforms the
%   values in the intensity image I to values in J by mapping
%   values between LOW and HIGH to values between BOTTOM and
%   TOP. Values below LOW and above HIGH are clipped; that is,
%   values below LOW map to BOTTOM, and those above HIGH map to
%   TOP. You can use an empty matrix ([]) for [LOW HIGH] or for
%   [BOTTOM TOP] to specify the default of [0 1]. 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).
%
%   NEWMAP = IMADJUST(MAP,[LOW HIGH],[BOTTOM TOM],GAMMA)
%   transforms the colormap associated with an indexed image. If
%   [LOW HIGH] and [BOTTOM TOP] are both 2-by-3, and GAMMA is a
%   1-by-3 vector, IMADJUST rescales the red, green, and blue
%   components separately. The rescaled colormap, NEWMAP, is the
%   same size as MAP.
%
%   RGB2 = IMADJUST(RGB1,...) performs the adjustment on each
%   image plane (red, green, and blue) of the RGB image RGB1. As
%   with the colormap adjustment, you can use different parameter
%   values for each plane by specifying [LOW HIGH] and [BOTTOM
%   TOP] as 2-by-3 matrices, and GAMMA as a 1-by-3 vector.
%
%   Note that if TOP < BOTTOM, the output image is reversed, as
%   in a photographic negative.
%
%   Class Support
%   -------------
%   For syntaxes that include an input image (rather than a
%   colormap), the input image can be of class uint8 or
%   double. The output image has the same class as the input
%   image.
%
%   Example
%   -------
%       I = imread('pout.tif');
%       J = imadjust(I,[0.3 0.7],[]);
%       imshow(I), figure, imshow(J)
%
%   See also BRIGHTEN, HISTEQ.

%   Clay M. Thompson 10-26-92
%   modified by Chris Griffin 7-9-96
%   Copyright 1993-1998 The MathWorks, Inc.  All Rights Reserved.
%   $Revision: 5.10 $  $Date: 1997/11/24 15:35:16 $

error(nargchk(2,4,nargin));

if ndims(img)==3 & size(img,3)==3,
    isRgb = 1; isGray = 0;  % RGB Intensity Image
elseif size(img,2)~=3, 
    if (isbw(img))
        error('Binary images not supported.');
    end
    isGray = 1; isRgb = 0;  % Grayscale Intensity Image
else 
    isGray = 0; isRgb = 0;  % Colormap
end


% Default values
if nargin<3, bottop = [0;1]; end
if nargin<4, gamma = 1; end
if isempty(lowhigh), lowhigh = [0;1]; end
if isempty(bottop), bottop = [0;1]; end
if isempty(gamma), gamma = 1; end

% Create triples for RGB image or Colormap
if min(size(lowhigh))==1,
    low = lowhigh(1)*ones(1,3);
    high = lowhigh(2)*ones(1,3);
else
    low = lowhigh(1,:);
    high = lowhigh(2,:);
end
if min(size(bottop))==1,
    bot = bottop(1)*ones(1,3);
    top = bottop(2)*ones(1,3);
else
    bot = bottop(1,:);
    top = bottop(2,:);
end
if prod(size(gamma))==1, gamma = gamma*ones(1,3); end

% All the parameters must be scalars for Grayscale intensity image.
if isGray, 
    low = low(1); 
    high = high(1); 
    bot = bot(1); 
    top = top(1); 
    gamma = gamma(1);
end

if min(low)<0 | max(low)>1 | min(high)<0 | max(high)>1 | ...
        min(bot)<0 | max(bot)>1 | min(top)<0  | max(top)>1,
    error('LOW, HIGH, BOT and TOP must be in the range [0.0, 1.0].');
end

if min(gamma)<0, 
    error('GAMMA must be greater than 0.0 .'); 
end

% Promote image data to double if it is a uint8
if isa(img, 'uint8'),
    img = im2double(img);
    u8input = 1;
else
    u8input = 0;
end


if ~isRgb
    % Transform colormap or Grayscale intensity image (v1 functionality)
    if isGray, n = 1; else n = size(img,1); end

    % Make sure img is in the range [low,high]
    img(:) = max(low(ones(n,1),:),min(high(ones(n,1),:),img));

    % The transformation
    d = ones(n,1);
    out = ( (img-low(d,:))./(high(d,:)-low(d,:)) ).^(gamma(d,:));
    out(:) = out.*(top(d,:)-bot(d,:)) + bot(d,:);
else 
    % Transform RGB image
    out = zeros(size(img));

    % Loop over image planes and perform transformation
    for p=1:3,
        % Make sure img is in the range [low,high]
        img(:,:,p) =  max(low(p),min(high(p),img(:,:,p)));

        % The transformation
        out(:,:,p) = ( (img(:,:,p)-low(p))./(high(p)-low(p)) ).^(gamma(p));
        out(:,:,p) = out(:,:,p).*(top(p)-bot(p)) + bot(p);
    end
end

if u8input,
    out = im2uint8(out);
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -