Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Piecewise Linear Transformation

734 views
Skip to first unread message

Michael

unread,
Oct 11, 2011, 12:05:13 PM10/11/11
to
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.

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

Steven_Lord

unread,
Oct 11, 2011, 1:18:00 PM10/11/11
to

"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

dpb

unread,
Oct 11, 2011, 1:21:26 PM10/11/11
to
On 10/11/2011 11:05 AM, Michael wrote:
> 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
...
>
> function piecewise_transform(f,n,r1,s1,r2,s2)

You'll need a return value for the result to not lose it when the
function returns (unless the assignment says the function is to be fully
self-contained)...see the "Getting Started" section on functions on how
to add this; the example given does return values.

...
> 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;
...

Instead of the loop, consider how Matlab can operate on arrays and look
at "logical indexing"--the use of the locations that satisfy a condition
as indices into the array/matrix and then operate on the appropriate values.

For example, consider the following code snippet...

x=rand(3); % generate some data
x1=0.8; % sample threshold value
dx = 0.01; % sample modifier value
x(x>x1)=x(x>x1).*(1+dx); % Fiddle w/ values above threshold

NB: You'll have to be certain your order of applying the modifications
doesn't affect the elements to be selected for the modification...

HTH...

--

0 new messages