Re: Best way to deal with checking array indices are within bounds [message #75638] |
Tue, 15 March 2011 08:38 |
ben.bighair
Messages: 221 Registered: April 2007
|
Senior Member |
|
|
On 3/15/11 11:23 AM, Robin Wilson wrote:
> Hi all,
>
> I was wondering what people generally do about checking that array
> indices are within bounds when working with images - as I assume it is a
> problem many people have. For example, in the following code:
>
> x_index = 49
> y_index = 42
>
> x_index += 3
> x_index += 3
>
> Trying to index a 50x50 array with x_index and y_index will give an
> error because the x_index is outside the bounds. I frequently run into
> this issue as I do a lot of image processing.
>
> How do people generally deal with it? I often end up using a lot of IF
> statements (like below), which gets very messy and is error-prone - does
> anyone have a better way to do this?
>
> IF x_index GT 50 THEN x_index = 50
> IF x_index LT 0 THEN x_index = 0
> ...
>
Hi,
It might be simplest to use IDL's coercion operators like this...
dim = SIZE(image, /DIM)
dx = 3
dy = 3
x_index = 0 > (x_index + dx) < (dim[0]-1)
y_index = 0 > (y_index + dy) < (dim[1]-1)
Cheers,
Ben
|
|
|