function piecewise_transform(f,n,r1,s1,r2,s2)
% ============================================
% SYNTAX: piecewise_transform(f,n,r1,s1,r2,s2)
% DESCRIPTION: Performs a piecewise linear transformation
% INPUT(s): input image (f), number of bits per pixel (n), transformation
% gray level values (r1,s1,r2,s2)
% OUTPUT(s): an image with the piecewise linear transformation applied to
% it.
% ============================================
% NAME: Michael J. Ryan
% LAB: 2 -- Gray Level Transformations
% DATE: October 11, 2011
% QUESTION: #2
% ============================================
a = [s1/r1 (r1-s1)/(r2-r1)];
b = [0 s1];
c = [0 r1];
T = zeros(size(n,1),2);
for i=1:size(n,1)
current_value = n(i);
if current_value <= r1;
p=1;
elseif r2 >= current_value > r1;
p=2;
else
p=3;
end;
transformed_value = a(p) * (current_value - c(p)) + b(p);
T(i,1:2,2:3)=[current_value, transformed_value];
end;
plot(T(:,1),T(:,2),T(:,3));
axis equal, axis tight;
end
"Michael " <mry...@masonlive.gmu.edu> wrote in message
news:j71pfp$e99$1...@newscl01ah.mathworks.com...
> Hello everyone, I need to write a function that performs a piecewise
> linear transformation on input gray levels (r) so that the output gray
> levels (s) are 'r' from 0<=r<=r1, '(r-r1)+s1' from r1<r<=r2, and
> '(r-r2)+s2' from r2<r<= L-1. However, I am completely new to matlab and
> can't even conceptualize how this could be done. I have a start though,
> which I know is close but still need a lot of work. Any help and advice
> would be awesome. Thanks! Here is my code so far below.
I'll let others analyze the full code; I just want to comment on one thing
that jumped out at me.
*snip*
> elseif r2 >= current_value > r1;
This does NOT do what you want; it's equivalent to (r2 >= current_value) >
r1, which is either 1 > r1 or 0 > r1 depending on r2 and current_value. What
you want to use to determine if current_value is in the interval (r1, r2]
is:
elseif (r1 < current_value) & (current_value <= r2)
If r1, r2, and current_value are all scalars (1-by-1 arrays) then use &&
instead of &.
--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com