Path: csiph.com!usenet.pasdenom.info!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'tree': 0.05; 'subject:Error': 0.07; 'advance': 0.07; '"__main__":': 0.09; '#print': 0.09; '[0,': 0.09; 'blue': 0.09; 'function,': 0.09; 'pixel': 0.09; 'skip:o 50': 0.09; 'trees': 0.09; 'def': 0.12; '"w")': 0.16; '>>': 0.16; '49,': 0.16; '65536': 0.16; 'add,': 0.16; 'dictionaries': 0.16; 'dot,': 0.16; 'itertools': 0.16; 'nameerror:': 0.16; 'numpy': 0.16; '8bit%:5': 0.22; 'import': 0.22; 'to:name:python-list@python.org': 0.22; 'print': 0.22; 'error': 0.23; 'math': 0.24; 'skip:" 30': 0.26; 'defined': 0.27; 'points': 0.29; 'message-id:@mail.gmail.com': 0.30; 'getting': 0.31; "skip:' 10": 0.31; 'branches': 0.31; 'initialized': 0.31; 'file': 0.32; 'class': 0.32; 'skip:c 30': 0.32; '(most': 0.33; 'skip:t 40': 0.33; 'subject:with': 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'done': 0.36; 'thanks': 0.36; 'skip:& 10': 0.38; '8bit%:4': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'skip:& 20': 0.39; '\xa0\xa0\xa0': 0.39; 'to:addr:python.org': 0.39; '8bit%:6': 0.40; 'skip:c 50': 0.60; 'mentioned': 0.61; 'traffic': 0.61; 'name': 0.63; '8bit%:10': 0.64; 'road': 0.65; 'floor,': 0.68; 'dict.': 0.84; 'gray': 0.84; 'poles': 0.84; 'trunks': 0.84; 'buildings': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=Z0XJA63ggS4qb3sL+q3QJkCSfPifsDmDcHXKrWCYeKw=; b=SrX0M+KAqT6Z1Y0iLN42i0IVYHp4h97wtSst6T8LjTn4Hg+0ZrEuT7MOOBIrOpC0Fb KfH690KuBKVWdr/QA4rpfWFLp6wYSQjR9j2Z7mH1L6kmG0TzM6HVQQ6CH/HgjZNeOFqj OPwD53tv43FOYfTZQKVwxa6m2kVDJHHMK6JQYEiur/Lrzw9XkL+IoX/XnFCH4F7CblDT LPJ7z5NuebMfNEbhLNTwpVYRHWeUaZqnjGAJOSRr/IPHCOG66Buil5nGsd059wS3pvD1 H12ac+79oZNxL8usCKwnM0SKy+B3HwZnvk043fpzjrg3KT4MhWSXUwTQB8xKHm2OCrR7 kjbQ== MIME-Version: 1.0 X-Received: by 10.50.13.39 with SMTP id e7mr3449786igc.97.1364824232570; Mon, 01 Apr 2013 06:50:32 -0700 (PDT) Date: Mon, 1 Apr 2013 19:20:32 +0530 Subject: Error in working with Dict From: inshu chauhan To: "python-list@python.org" Content-Type: multipart/alternative; boundary=089e011605e09039d104d94ce726 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 165 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364824236 news.xs4all.nl 6909 [2001:888:2000:d::a6]:49474 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42463 --089e011605e09039d104d94ce726 Content-Type: text/plain; charset=ISO-8859-1 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 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!!!! --089e011605e09039d104d94ce726 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
I have this program which is working with 2 dictionaries s= egments, class_counts.. but I am getting an error mentioned below the progr= amme.=A0

import cv
from itertools import = *
from math import floor, sqrt, ceil
from numpy import array, = dot, subtract, add, outer, argmax, linalg as lin

<= br>
def Computesegclass(segimage, refimage): =A0
=A0 = =A0 f =3D open("Pixel_count_with_region_num_trial1.txt", "w&= quot;)
=A0 =A0 segments =3D {}
=A0 =A0 class_count =3D {}
=A0 =A0 for y in xrange(0, segimage.height):
=A0 =A0 =A0 =A0 for= x in xrange(0, segimage.width):

=A0 =A0 =A0 =A0 = =A0 =A0 if segimage[y,x] =3D=3D (0.0, 0.0, 0.0):
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 continue
=A0 =A0 =A0 =A0 =A0= =A0 else:
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 seg_color =3D segimage= [y,x]
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 blue =3D int(seg_color[0])<= /div>
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 green =3D int(seg_color[1])
=
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 red =3D int(seg_color[2])
=A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 region_num =3D blue + 256 * green + 65536 * red
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #print region_num
=A0 =A0= =A0 =A0 =A0 =A0 =A0 =A0 segments[region_num] =3D segments.setdefault(regio= n_num, 0) + 1
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 #print segments
=A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0=A0
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 class_colo= r =3D refimage[y,x]
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=A0
= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if class_color =3D=3D (0.0,0.0,0.0):
<= div>=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 class_number =3D 0 =A0 =A0 =A0#= No class
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 elif class_color =3D=3D (0.0,255.0,0.0= ):
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 class_number =3D 1 =A0= # Trees=A0
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 elif class_color =3D= =3D (255.0, 0.0, 128.0):
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = class_number =3D 2 =A0 # Buildings=A0
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 elif class_color =3D=3D (0.0,0.0,255.0= ):
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 class_number =3D 3 =A0= =A0# Automobiles=A0
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 elif class_c= olor =3D=3D (255.0, 255.0, 0.0):
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 class_number =3D 4 =A0 =A0# Road Points=A0
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 elif class_color =3D=3D (0.0, 64.0, 12= 8.0):
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 class_number =3D 5 = =A0 =A0# Tree trunks nad branches=A0
=A0 =A0 =A0 =A0 =A0 =A0 =A0 = =A0 elif class_color =3D=3D (255.0, 0.0 ,0.0):
=A0 =A0 =A0 =A0 = =A0 =A0 =A0 =A0 =A0 =A0 class_number =3D 6 =A0 =A0# Poles =A0
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 elif class_color =3D=3D (255.0, 0.0, 2= 55.0):
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 class_number =3D 7= =A0 =A0# Traffic Lights
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 else:
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 class_number =3D 0 =A0 =A0 = # Gray Pixel
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=A0
=A0 =A0 =A0 =A0 =A0 =A0 = =A0 =A0 class_count.setdefault(region_num, [0, 0, 0, 0, 0, 0, 0, 0])[class_= number] +=3D 1
=A0 =A0 =A0 =A0 =A0 =A0 # print class_count
<= div>for k in sorted(class_count.iterkeys()):
=A0 =A0 i =3D argmax(class_count[k])
=A0 =A0 print >> = f, i=A0

if __name__=3D=3D "__main__":
=A0 =A0 segimage =3D cv.LoadImageM(r"C:\Users\inshu\Desktop\Ma= sters Thesis\Segmentation\segmentation_numbers_00000.tif", cv.CV_LOAD_= IMAGE_UNCHANGED)
=A0 =A0 refimage =3D cv.LoadImageM(r"C:\Users\inshu\Desktop\Maste= rs Thesis\Segmentation\Hand_Classified1.tif", cv.CV_LOAD_IMAGE_UNCHANG= ED)
=A0 =A0 print segimage
=A0 =A0 Computesegclass(segi= mage, refimage)


ERROR :=A0
<= br>
Traceback (most recent call last):
=A0 F= ile "C:\Users\inshu\Desktop\seg.py", line 49, in <module>
=A0 =A0 for k in sorted(class_count.iterkeys()):
NameError: = name 'class_count' is not defined

I = know this error is because I have=A0initialized=A0both dicts inside the fun= ction, But why dicts are not saved out, if I intialize the dicts outside th= e function, the processing is still done inside and end result I am getting= is an empty dict.


Thanks in Advance for = suggestions!!!!=A0

--089e011605e09039d104d94ce726--