Re: Error .. Please Help

Discussion in 'Python' started by Ulrich Eckhardt, Dec 12, 2012.

  1. Am 12.12.2012 16:00, schrieb inshu chauhan:
    > color = image[y,x]
    > if color == (0.0,0.0,0.0):
    > continue
    > else :
    > if color == (0.0,255.0,0.0):
    > classification = 1
    > elif color == (128.0, 0.0, 255.0):
    > classification = 2
    > elif color == (255.0,0.0,0.0):
    > classification = 3
    > elif color == (128.0, 128.0, 128.0):
    > classification = 4
    > elif color == (128.0, 64.0, 0.0):
    > classification = 5
    > elif color == (0.0, 0.0, 255.0):
    > classification = 6
    > elif color == (255.0, 0.0, 255.0):
    > classification = 7



    Use a dict for this, it probably makes things clearer. Something like:

    cclasses = {( 0.0, 0.0, 0.0): None,
    ( 0.0, 255.0, 0.0): 1,
    (128.0, 0.0, 255.0): 2, }

    if cclasses[color] is not None:
    print >> g, x , y , color, cclasses[color]


    Some notes:
    * Some people (and I think PEP8) frown upon the table formatting of
    the dict.
    * d[color] will raise an exception if there is no mapping, but it's
    not clear what you want to do with those inputs anyway.
    * Comparing floating-point values for equality is always a bit tricky,
    as most operations have some rounding. This could mean that you need to
    use ranges instead of fixed values. A good approach is to round the
    input to integral values first.
    * Are you sure that the input uses floating point values but uses
    typical 8-bit ranges 0-255? I would expect floating point values between
    0 and 1 or integral values between 0 and 255, but not such a mixture.
    * Try g.write('{} {} {} {}\n'.format(x, y, color, classification)) to
    get code that you will be able to reuse in Python 3. Also, consider
    upgrading anyway.


    > I am getting the following error..
    >
    > Traceback (most recent call last):
    > File "Z:\modules\Get_Classification.py", line 27, in <module>
    > print >> g, x , y , color, classification
    > NameError: name 'classification' is not defined
    >
    > Its simple error of name but m not getting why it should come as I have
    > already defined Classification in between if-else loop ??


    One comment here: If you don't define "classification" in this loop
    iteration, the one from the previous iteration will be used.
    Effectively, this tells me that the first pixel unequal to (0,0,0)
    already doesn't fit your expectations. Use "import pdb" and
    "pdb.set_trace()" to get into debugging mode, which is a useful skill
    anyway.

    Good luck!

    Uli
    Ulrich Eckhardt, Dec 12, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    491
    Chris Uppal
    May 5, 2005
  2. KK
    Replies:
    2
    Views:
    516
    Big Brian
    Oct 14, 2003
  3. MuZZy
    Replies:
    7
    Views:
    1,717
    Mike Hewson
    Jan 7, 2005
  4. Replies:
    4
    Views:
    302
    Martin Ambuhl
    Nov 14, 2007
  5. mera
    Replies:
    7
    Views:
    1,078
Loading...

Share This Page