Error in working with Dict
Vincent Vande Vyvre
vincent.vandevyvre at swing.be
Mon Apr 1 10:10:06 EDT 2013
Le 01/04/13 15:50, inshu chauhan a écrit :
> I have this program which is working with 2 dictionaries segments,
> class_counts.. but I am getting an error mentioned below the programme.
>
> import cv
> from itertools import *
> from math import floor, sqrt, ceil
> from numpy import array, dot, subtract, add, outer, argmax, linalg as lin
>
>
> def Computesegclass(segimage, refimage):
> f = open("Pixel_count_with_region_num_trial1.txt", "w")
> segments = {}
> class_count = {}
> for y in xrange(0, segimage.height):
> for x in xrange(0, segimage.width):
>
> if segimage[y,x] == (0.0, 0.0, 0.0):
> continue
> else:
> seg_color = segimage[y,x]
> blue = int(seg_color[0])
> green = int(seg_color[1])
> red = int(seg_color[2])
> region_num = blue + 256 * green + 65536 * red
> #print region_num
> segments[region_num] = segments.setdefault(region_num,
> 0) + 1
> #print segments
>
> class_color = refimage[y,x]
>
> if class_color == (0.0,0.0,0.0):
> class_number = 0 # No class
> elif class_color == (0.0,255.0,0.0):
> class_number = 1 # Trees
> elif class_color == (255.0, 0.0, 128.0):
> class_number = 2 # Buildings
> elif class_color == (0.0,0.0,255.0):
> class_number = 3 # Automobiles
> elif class_color == (255.0, 255.0, 0.0):
> class_number = 4 # Road Points
> elif class_color == (0.0, 64.0, 128.0):
> class_number = 5 # Tree trunks nad branches
> elif class_color == (255.0, 0.0 ,0.0):
> class_number = 6 # Poles
> elif class_color == (255.0, 0.0, 255.0):
> class_number = 7 # Traffic Lights
> else:
> class_number = 0 # Gray Pixel
>
> class_count.setdefault(region_num, [0, 0, 0, 0, 0, 0,
> 0, 0])[class_number] += 1
> # print class_count
> for k in sorted(class_count.iterkeys()):
> i = argmax(class_count[k])
> print >> f, i
>
> if __name__== "__main__":
> segimage = cv.LoadImageM(r"C:\Users\inshu\Desktop\Masters
> Thesis\Segmentation\segmentation_numbers_00000.tif",
> cv.CV_LOAD_IMAGE_UNCHANGED)
> refimage = cv.LoadImageM(r"C:\Users\inshu\Desktop\Masters
> Thesis\Segmentation\Hand_Classified1.tif", cv.CV_LOAD_IMAGE_UNCHANGED)
> print segimage
> Computesegclass(segimage, refimage)
>
>
> ERROR :
>
> Traceback (most recent call last):
> File "C:\Users\inshu\Desktop\seg.py", line 49, in <module>
> for k in sorted(class_count.iterkeys()):
> NameError: name 'class_count' is not defined
>
> I know this error is because I have initialized both dicts inside the
> function, But why dicts are not saved out, if I intialize the dicts
> outside the function, the processing is still done inside and end
> result I am getting is an empty dict.
>
>
> Thanks in Advance for suggestions!!!!
>
>
>
Get the class_count in the return of Computesegclass
cc = Computesegclass(segimage, refimage)
for k in sorted(cc.iterkeys()):
...
--
Vincent V.V.
Oqapy <https://launchpad.net/oqapy> . Qarte
<https://launchpad.net/qarte> . PaQager <https://launchpad.net/paqager>
More information about the Python-list
mailing list