图像分段线性变换

function [A,map,x1,x2,y1,y2] = parse_inputs(varargin)

map=[];

%iptchecknargin(3,4,nargin,mfilename);

%narginchk(3,4,nargin,mfilename);

%iptcheckinput(varargin{1},...
 %   {'uint8','uint16','int16','double'},...
  %  {'real','nonsparse'},mfilename,'I ,X or RGB',1);

  %celldisp(varargin);
  
switch nargin
    case 3
        A = varargin{1};
        x1 = varargin{2}(1);
        x2=varargin{2}(2);
        y1=varargin{3}(1);
        y2=varargin{3}(2);
    case 4
        A = varargin{1};
        map=varargin{2};
        x1 = varargin{2}(1);
        x2=varargin{2}(2);
        y1=varargin{3}(1);
        y2=varargin{3}(2);
end

if (ndims(A) == 3) && (size(A,3) ~= 3)
    msg = sprintf('%s:M-N-3',...
        upper(mfilename));
    eid = sprintf('Images:%s:trueColorRgbImageMustBeMbyNby3',mfilename);
    error(eid,'%s',msg);
        

end

if ~isempty(map)
    if (size(map,2)~=3) || ndims(map)>2
        msg1 = sprintf('%s:matix',...
            upper(mfilename));
        msg2 = 'three 3 parallel';
        eid = sprintf('Images:%s:inColormapMustBe2Dwith3Cols',mfilename);
        error(eid,'%s %s',msg1,msg2);
    elseif (min(map(:))<0) || (max(map(:))>1)
        msg1 = sprintf('%s',upper(mfilename));
        msg2 = 'between 0 and 1';
        eid = sprintf('Images:%s:colormapValsMustBe0tol',mfilename);
        error(eid,'%s %s',msg1,msg2);
    end
end
if isa(A,'int16')
    A = int16tounit16(A);
end

       



function out = imgrayscaling(varargin)

[A,map,x1,x2,y1,y2] = parse_inputs(varargin{:});

range = getrangefromclass(A);
range = range(2);

if ndims(A) == 3,%
    A = rgb2gray(A);
elseif ~isempty(map),%
    A = ind2gray(A,map);
end

[M,N] = size(A);
I = im2double(A);
out = zeros(M,N);

for i=1:M
    for j=1:N
        if I(i,j) < x1
            out(i,j) = y1*I(i,j)/x1;
        elseif I(i,j) > x2
            out(i,j) = (I(i,j)-x1)*(range-y1)/(range-x1)+y1;
        else
            out(i,j) = (I(i,j)-x1)*(y2-y1)/(x2-x1)+y1;
            
        end
    end
end
if isa(A,'uint8')
    out=im2uint8(out);
elseif isa(A,'uint16')
    out =im2uint16(out)
end

if nargout == 0
    imshow(out);
    return;
end


    
I = imread('p1.jpg');
J1 = imgrayscaling(I,[0.3,0.7],[0.15,0.85]);
figure,imshow(J1,[]);


J2 = imgrayscaling(I,[0.15,0.85],[0.3,0.7]);
figure,imshow(J2,[]);

图像分段线性变换_第1张图片

图像分段线性变换_第2张图片

图像分段线性变换_第3张图片

 

你可能感兴趣的:(笔记,Algorithm,Matlab)