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


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

working with dict : incrementing dict dynamically

Started byinshu chauhan <insideshoes@gmail.com>
First post2013-03-11 16:23 +0100
Last post2013-03-11 15:37 +0000
Articles 2 — 2 participants

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


Contents

  working with dict : incrementing dict dynamically inshu chauhan <insideshoes@gmail.com> - 2013-03-11 16:23 +0100
    Re: working with dict : incrementing dict dynamically John Gordon <gordon@panix.com> - 2013-03-11 15:37 +0000

#41070 — working with dict : incrementing dict dynamically

Frominshu chauhan <insideshoes@gmail.com>
Date2013-03-11 16:23 +0100
Subjectworking with dict : incrementing dict dynamically
Message-ID<mailman.3191.1363015388.2939.python-list@python.org>

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

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

[toc] | [next] | [standalone]


#41071

FromJohn Gordon <gordon@panix.com>
Date2013-03-11 15:37 +0000
Message-ID<khktna$g2r$1@reader2.panix.com>
In reply to#41070
In <mailman.3191.1363015388.2939.python-list@python.org> inshu chauhan <insideshoes@gmail.com> writes:

> --14dae93408ffe4594104d7a7bf0c
> Content-Type: text/plain; charset=ISO-8859-1

> 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})

You're creating a new segments dictionary for each pixel, overwriting
the previous one.  Instead, you probably want to create an empty dictionary
at the top of your function.

>                 if region_num == region_num:

This if statement will always evaluate true, because you're comparing
region_num to itself.

>                     num_pixel = +1

This doesn't store the value of num_pixel in the dictionary.  You probably
want to do this instead:

    segments[region_num] += 1

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [standalone]


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


csiph-web