On 04/01/2013 09:50 AM, inshu chauhan wrote:
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
You do realize you ended the function when you put the following line at
the left margin? Perhaps you really intended to put the for loop inside
the function? I can't tell.
You don't call Computesegclass() till later, so how can you use its
dicts, even if it did correctly pass them back?
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!!!!
--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list