Working with lists within Dictionaries

inshu chauhan insideshoes at gmail.com
Thu Apr 18 09:58:16 EDT 2013


Hello Everyone,

I am trying to work with lists and dictionaries together.
In the following code I have to access pixels in a segmented image through
a dictionary. But the problem is when I am trying to update the list
through dict it is giving me this error of tuple, ofcourse because list
indices should be integer.

THE CODE IS :
import cv
def access_segments(segimage, data):
    print segimage
    segments = {}
    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])
                reg_num = blue + 256 * green + 65536 * red
                point = data[y,x]
                segments.setdefault(reg_num, [])[point] += point

    for p in sorted(segments.iterkeys()):
        points = (segments[p])
        print len(points)
        print points

if __name__== "__main__":
    data = cv.Load(r"C:\Users\inshu\Desktop\Masters
Thesis\data\xyz_00000.yml")
    segimage = cv.LoadImageM(r"C:\Users\inshu\Desktop\Masters
Thesis\Segmentation\segmentation_numbers_00000.tif",
cv.CV_LOAD_IMAGE_UNCHANGED)
    access_segments(segimage, data)


THE ERROR IS:

<cvmat(type=42424010 8UC3 rows=3000 cols=3000 step=9000 )>

Traceback (most recent call last):
  File "C:\Users\inshu\Desktop\test_reading .py", line 27, in <module>
    access_segments(segimage, data)
  File "C:\Users\inshu\Desktop\test_reading .py", line 16, in
access_segments
    segments.setdefault(reg_num, [])[point] += point
TypeError: list indices must be integers, not tuple

How can I access the data without getting this error ? the points have x,
y, z co-ordinates.

Thanks In Advance for your suggestions.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130418/f6fe5f8f/attachment.html>


More information about the Python-list mailing list