Luca -

I guess there are two issues here. The first, which you've solved, is
how to load data in one format and save it as another. The second
involves modifying the data. This is the real purpose of PDL. :-)

Getting your head around how to store RGB image data in a piddle can
be quite confusing until you get the hang of it. To simplify things, I
suggest we start by looking at a 1-dimensional time series instead of
a two- or three-dimensional color image.

Here are some examples (in script form) that might help you get better
at thinking about your data. I think that what I am about to describe
answers your question, but if not, please ask more! (I say scripts
because I use lexical variables. To run this in the original pdl
shell, remove the use statements and the 'my' portions of the variable
declarations. Or run it in the pdl2 shell if you have that installed.)

Start with a simple time series, say, a sine wave:

----------%<----------
use strict;
use warnings;
use PDL;

# Make x run from 0 to just more than two * pi
my $x = sequence(64)/10;

# Create a sine wave:
my $y = sin($x);
---------->%----------

So $y holds the heights for the sine wave, $x holds the x-coordinates
with which those heights are associated. If PDL had a simple, reliable
plotting interface I would show you how to plot this data, but we
don't, so I won't. Sorry. Hopefully you're familiar with the sine wave
and know what it looks like.

Now, suppose you want to clip your sine wave. There are many ways to
do that, depending on what you're trying to do. You can use the hclip,
lclip, or clip functions (as they are designed explicitly for
clipping) or you can explicitly slice out the data using where, or
which, or (as Craig demonstrated above) whichND.

First, here's how to clip the data using clip. You can clip it using
specified minima and maxima, which is how $y_const_clip is formed. You
can also clip by an arbitrary envelope, which is only slightly more
complicated, and involves creating a piddle that describes that
envelope. $y_linear_clip is the original sine wave clipped by an
envelope that increases linearly:

----------%<----------
# Clip y-data by some min/max value, in this case -0.8 and 0.5
my $y_const_clip = $y->clip(-0.8, 0.5)

# Clip y-data by an envelope, in this case a linear one with slopes
-0.2 and 0.3, respectively:
my $y_linear_clip = $y->clip(-0.2*$x, 0.3*$x)
---------->%----------

Second, you can slice data from the original piddle and modify it as
you wish. This uses the which and where functions. This, for example,
takes all data larger than 0.6 and sets it to 2:

----------%<----------
# This modifies the original y-data in-place:
$y->where($y > 0.6) .= 2;

# or (starting from original $y data)
# get the indices of the points you want to modify
my $indices = $y->which($y > 0.6);
# and modify $y using this 'index' command
$y->index($indices) .= 2;
---------->%----------

Once you understand those, try playing with whichND. The documentation
for all of these functions starts here:
http://pdl.perl.org/?docs=Primitive&title=where#which

David

_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to