Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Comparing the triangle vertex recived from subdiv2d with the points that used to insert to the subdiv2d in python

I am using OpenCV with python for one of my image processing project. There I create a list of points to insert to the subdiv2d object and I use getTriangleList() method to retrieve Delaunay triangles. same time I create a python dictionary and add each point of the point list (that inserted to subdiv2d) as a key and assigned some value. Finally, I tried to use triangular vertex (returned from subdiv2d.getTriangleList()) as key to retrieve a value from the dictionary I created It will end up with KeyError. This is a sample code for more understanding.

points = []

rect = (0, 0, size[0], size[1])
subdiv = cv2.Subdiv2D(rect)

imgDictionary = {}

for t in data: #data that received from another method with [num, num, num] format
    x = t[0]
    y = t[1]
    value = t[2]
    imgDictionary[(x,y)] = value
    points.append((x, y))

subdiv.insert(points)
triangleList = subdiv.getTriangleList()

for t in triangleList:
p1 = (int(t[0]), int(t[1]))
p2 = (int(t[2]), int(t[3]))
p3 = (int(t[4]), int(t[5]))

#to ensure all the points are inside the rectangle
if rect_contains(rect, p1) and rect_contains(rect, p2) and rect_contains(rect, p3):

    value = imgDictionary[p1] #here come the KeyError
...

earlier I thought this will happen because there is some triangle that points are not in the range of `subdiv. But since we are using a rect_containing method to avoid the vertexes that are outside from the subdiv range.

Please help me to find a proper solution to retrieve the value from the dictionary with keys with tuple data objects.