Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'true,': 0.04; 'case.': 0.05; '"__main__":': 0.07; 'mentioned,': 0.07; 'pixel': 0.07; 'blue': 0.09; 'collections': 0.09; 'dict': 0.09; 'cc:addr:python-list': 0.10; 'advance': 0.10; 'def': 0.10; 'suggest': 0.11; '0.0,': 0.16; '65536': 0.16; 'defaultdict': 0.16; 'thanx': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'email addr:gmail.com>': 0.20; 'trying': 0.21; 'import': 0.21; 'skip:c 70': 0.22; 'cc:2**0': 0.23; 'cc:addr:python.org': 0.25; 'header :In-Reply-To:1': 0.25; 'values': 0.26; 'set.': 0.27; 'message- id:@mail.gmail.com': 0.27; 'comparison': 0.29; 'dictionary': 0.29; 'falls': 0.29; 'skip:& 10': 0.29; 'subject: : ': 0.30; 'code': 0.31; 'print': 0.32; 'skip:& 20': 0.33; 'received:google.com': 0.34; 'identified': 0.35; 'continue': 0.35; 'something': 0.35; 'created': 0.36; 'but': 0.36; 'subject:with': 0.36; 'test': 0.36; 'correctly': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'green': 0.38; 'your': 0.60; 'red': 0.60; 'john': 0.60; 'skip:u 10': 0.60; 'first': 0.61; 'here': 0.65; 'region.': 0.65; 'color': 0.69; '8bit%:100': 0.70; '3000': 0.71; 'increase': 0.72; '2013': 0.84; 'dict,': 0.84; '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=8K+t8gIjHNgMATTCoQXVCN22Ya59GdIUc1cwkI3Wbvc=; b=tE2OUKiq/a/3e9P70DHqUBgojDB/m/vwZvcMYb+ej3ysYwazVsDh33MQybc98SdYZI VrO2s5a9esXWaI/2XdhBBUb9d7Ze7DNCab2vKpkh9MvAtU/ECTCNCrvZUA65/EpZ8l2W AOLWjq1w9071XV0owp8+w2tblDWXWgHp1qIoNbNIILAp6BDLiYLAAbjVswCQ+FvS41HL mIQ7UEwpPBYRvY8PPD9PIUVzegLrc1VZeWmfivOoFQ0j+5QLlwc7ITEcI4JkPqXFtP0h ahI2UB/Pc/FgZjr+5TWeGbn5ijGk3LocmVTTML7yrYMt7/byOxPzCKb3wJ+bpMDJW5Cq Whlw== MIME-Version: 1.0 X-Received: by 10.180.87.129 with SMTP id ay1mr13551664wib.1.1363017220097; Mon, 11 Mar 2013 08:53:40 -0700 (PDT) In-Reply-To: References: Date: Mon, 11 Mar 2013 15:53:39 +0000 Subject: Re: working with dict : incrementing dict dynamically From: Sven To: inshu chauhan Content-Type: multipart/alternative; boundary=f46d0444e8893a2d2304d7a82d07 Cc: "python-list@python.org" 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: 126 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363017229 news.xs4all.nl 6980 [2001:888:2000:d::a6]:58982 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41072 --f46d0444e8893a2d2304d7a82d07 Content-Type: text/plain; charset=ISO-8859-1 On 11 March 2013 15:23, inshu chauhan wrote: > I am trying to create a dictionary with a key and its values seraching > from a data set. But something is going wrong. This is the first time I am > working with dicts. > > My code is : > > import cv > def Computesegclass(segimage): > num_pixel = 0 > 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: > color = segimage[y,x] > blue = color[0] > green = color[1] > red = color[2] > region_num = blue + 256 * green + 65536 * red > print region_num > segments = dict({region_num : num_pixel}) > if region_num == region_num: > num_pixel = +1 > print segments > > if __name__== "__main__": > segimage = > cv.LoadImageM("Z:/Segmentation/segmentation_numbers_00000.tif", > cv.CV_LOAD_IMAGE_UNCHANGED) > print segimage > Computesegclass(segimage) > > > here I am traversing through an image which is 3000 x 3000. for each pixel > I calculate a region number. What I am trying to do is increase the > num_pixel by 1 if that pixel falls in the same region which is identified > by region_num. How can I do it in dict created ? > > Thanx in Advance > > If I am understanding you correctly you want to accumulate a pixel count for each region. I would suggest a default dict, and that you do something like from collections import defaultdict def ComputeSegClass(seqimage): segments = defaultdict(int) if region_num == region_num: segments[region_num] += 1 But as John mentioned, your comparison here will always be true, so you will need to fix this to test for your case. -- ./Sven --f46d0444e8893a2d2304d7a82d07 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On 11 March 2013 15:23, inshu chauhan &l= t;insideshoes@gm= ail.com> wrote:
I am trying = to create a dictionary with a key and its values seraching from a data set.= But something is going wrong. This is the first time I am working with dic= ts.

My code is :

import cv
def Computesegclass(segimage):
=A0=A0=A0 num_pixel =3D= 0
=A0=A0=A0=A0=A0=A0=A0 for y in xrange(0, segimage.height):
=A0=A0= =A0=A0=A0=A0=A0 for x in xrange(0, segimage.width):
=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0
=A0=A0=A0=A0=A0=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=A0=A0=A0=A0=A0=A0=A0 continue
=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0 else:
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 c= olor =3D segimage[y,x]
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 blu= e =3D color[0]
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 green =3D c= olor[1]
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 red =3D color[2]=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 region_num =3D blue + 256 *= green + 65536 * red
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 print region_num
=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 segments =3D dict({region_num : num_pi= xel})
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 if region_num =3D=3D= region_num:
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 n= um_pixel =3D +1
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 print seg= ments

if __name__=3D=3D "__main__":
=A0=A0=A0 segimage =3D cv.LoadIm= ageM("Z:/Segmentation/segmentation_numbers_00000.tif", cv.CV_LOAD= _IMAGE_UNCHANGED)
=A0=A0=A0 print segimage
=A0=A0=A0 Computesegclass(= segimage)


here I am traversing through an image which is 3000 x 3000. for each = pixel I calculate a region number. What I am trying to do is increase the n= um_pixel by 1 if that pixel falls in the same region which is identified by= region_num. How can I do it in dict created ?

Thanx in Advance


If I am understanding you correctly you wan= t to accumulate a pixel count for each region. I would suggest a default di= ct, and that you do something like

from = collections import defaultdict
def Co= mputeSegClass(seqimage):
=A0 =A0 segm= ents =3D defaultdict(int)
<snip>
=A0= =A0 if region_num =3D=3D region_num:
=A0 = =A0 =A0 =A0 segments[region_num] +=3D 1

But as John mentioned, your comparison here will always be true, so= you will need to fix this to test for your case.

--
./Sven
--f46d0444e8893a2d2304d7a82d07--