Re: [Q] Extending Python for computer vision [LONG]

James C. Ahlstrom (jim@interet.com)
1 Mar 1995 14:48:14 -0500

In article <GRENDEL.95Feb16145831@fen.arc.nasa.gov> grendel@fen.arc.nasa.gov
(Raymond E. Suorsa) writes:
>
> Would a dictionary be better?
> B.filter( {'K1': 1.0, 'K2': 2.0, 'C1': 0.4, 'C2': 0.3 } )
> ...
> But would I have to build up this dictionary myself? How does one get
> the default argument dictionary from the function?

I personally don't like explicit dictionaries, and prefer to use Python
variables (which are implemented as dictionaries anyway). Defaults can
be inherited from classes.

# Example 1:
class FilterParams:
K1 = 1.0
K2 = 2.0
C1 = 0.4
C2 = 0.3
pass

filter_params = FilterParams()
filter_params.K1 = 1.5
B.filter(filter_params)

The above implies that the parameters are an independent object. If they
are really part of the image, then it is better to write:

# Example 2:
B.K1 = 1.5
B.filter()

with the remaining filter parameters either inherited from the class of B, or
else assigned by the __init__ of the class of B.

> The other way (maybe not implementable in python)
>
> B.filter.C2 = 0.3
> B.filter.K1 = 1.0
> B.filter()
>
This implies that the parameters are a part of the filter, but is illegal
Python. Instead try:

# Example 3:
class Filter:
K1 = 1.0 # Or, set these variables in __init__
K2 = 2.0
C1 = 0.4
C2 = 0.3
def filter(self, target):
# lots of Python, or a call to a C function goes here, and
# there are references to self.K1 etc.

f = Filter()
f.K1 = 1.5
f(B)

Any of the above three solutions will "work", but you should choose
the one which is most philosophically correct. That is, decide exactly
what the parameters really are, (an object, a part of the image, or a
part of the filter) and then write that in Python.

IMHO, Python is at is best when used in a strongly object-oriented
style, a style which makes many design problems disappear.

Jim Ahlstrom jim@interet.com $clever