Ask Your Question
0

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

asked 2019-02-14 19:09:43 -0500

Vikum Dheemantha gravatar image

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.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-02-16 07:03:26 -0500

supra56 gravatar image

updated 2019-02-16 07:06:43 -0500

Firstly, I haven't tested it. Because, I'm using OpenCV 4.0.1. I have 2 errors for you. I noticed that you are using dictionary. You will have to remove p1. The correct way to do like this.

imgDictionary['p1'] = (int(t[0]), int(t[1]))

Secondly, put inside for block:

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]))
    if rect_contains(rect, p1) and rect_contains(rect, p2) and rect_contains(rect, p3):
        value = imgDictionary[p1]
edit flag offensive delete link more

Comments

even here key of the imageDictionary dictionary is String 'p1'. But I want to make the tuple 'p1' as a string and other value returning from the first list (mentioned as data in the code) as the value.

Vikum Dheemantha gravatar imageVikum Dheemantha ( 2019-02-19 23:49:09 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-02-14 19:09:43 -0500

Seen: 185 times

Last updated: Feb 16