Error .. Please Help

Ulrich Eckhardt ulrich.eckhardt at dominolaser.com
Wed Dec 12 10:46:19 EST 2012


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





More information about the Python-list mailing list