Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34693 > unrolled thread

Error .. Please Help

Started byinshu chauhan <insideshoes@gmail.com>
First post2012-12-12 16:00 +0100
Last post2012-12-12 16:46 +0100
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Error .. Please Help inshu chauhan <insideshoes@gmail.com> - 2012-12-12 16:00 +0100
    Re: Error .. Please Help Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-12-12 16:46 +0100

#34693 — Error .. Please Help

Frominshu chauhan <insideshoes@gmail.com>
Date2012-12-12 16:00 +0100
SubjectError .. Please Help
Message-ID<mailman.776.1355324421.29569.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

In this code :

import cv
g = open("PointCloudmitClass.txt", "w")
image = cv.LoadImageM("Z:/modules/intensity_000001.tif",
cv.CV_LOAD_IMAGE_UNCHANGED)
print image
for y in xrange(0,image.height):
    for x in xrange(0,image.width):
        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

            print >> g, x , y , color, classification


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 ??

Thanks in Advance !!!


IC

[toc] | [next] | [standalone]


#34696

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2012-12-12 16:46 +0100
Message-ID<cflmp9-l6s.ln1@satorlaser.homedns.org>
In reply to#34693
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web