<div dir="ltr">Hello Everyone, <div><br></div><div style>I am trying to work with lists and dictionaries together. </div><div style>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. </div>
<div style><br></div><div style>THE CODE IS :</div><div style><div>import cv</div><div>def access_segments(segimage, data):</div><div> print segimage</div><div> segments = {}</div><div> for y in xrange(0, segimage.height):</div>
<div> for x in xrange(0, segimage.width):</div><div> if segimage[y,x] == (0.0, 0.0, 0.0):</div><div> continue</div><div> else:</div><div> seg_color = segimage[y,x]</div>
<div> blue = int(seg_color[0])</div><div> green = int(seg_color[1])</div><div> red = int(seg_color[2])</div><div> reg_num = blue + 256 * green + 65536 * red</div>
<div> point = data[y,x]</div><div> segments.setdefault(reg_num, [])[point] += point</div><div> </div><div> for p in sorted(segments.iterkeys()):</div><div> points = (segments[p])</div>
<div> print len(points)</div><div> print points</div><div><br></div><div>if __name__== "__main__":</div><div> data = cv.Load(r"C:\Users\inshu\Desktop\Masters Thesis\data\xyz_00000.yml")</div>
<div> segimage = cv.LoadImageM(r"C:\Users\inshu\Desktop\Masters Thesis\Segmentation\segmentation_numbers_00000.tif", cv.CV_LOAD_IMAGE_UNCHANGED)</div><div> access_segments(segimage, data)</div><div> </div>
<div><br></div><div style>THE ERROR IS:</div><div style><br></div><div style><div><cvmat(type=42424010 8UC3 rows=3000 cols=3000 step=9000 )></div><div><br></div><div>Traceback (most recent call last):</div><div> File "C:\Users\inshu\Desktop\test_reading .py", line 27, in <module></div>
<div> access_segments(segimage, data)</div><div> File "C:\Users\inshu\Desktop\test_reading .py", line 16, in access_segments</div><div> segments.setdefault(reg_num, [])[point] += point</div><div>TypeError: list indices must be integers, not tuple</div>
</div><div style><br></div><div style>How can I access the data without getting this error ? the points have x, y, z co-ordinates.</div><div style><br></div><div style>Thanks In Advance for your suggestions. </div></div></div>