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


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

Re: working with dict : incrementing dict dynamically

Started bySven <svenito@gmail.com>
First post2013-03-11 15:53 +0000
Last post2013-03-11 15:53 +0000
Articles 1 — 1 participant

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: working with dict : incrementing dict dynamically Sven <svenito@gmail.com> - 2013-03-11 15:53 +0000

#41072 — Re: working with dict : incrementing dict dynamically

FromSven <svenito@gmail.com>
Date2013-03-11 15:53 +0000
SubjectRe: working with dict : incrementing dict dynamically
Message-ID<mailman.3192.1363017229.2939.python-list@python.org>

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

On 11 March 2013 15:23, inshu chauhan <insideshoes@gmail.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 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)
<snip>
    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

[toc] | [standalone]


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


csiph-web