Data structure in python containing shapes corners and centroids

asked 2014-01-27 09:20:59 -0500

Francesco Sgaramella gravatar image

I am having a lot of troubles in python programming, mostly because I am a beginner.

I first applied findContours to a picture and I have to save the retrieved shape corners and the shapes centroids in data structure for latter modifications.

I created a data structure for the shapes that looks like this (the number of shapes is not known):

Shapes = [[shape1-Corners],

and another one containing the centers (again the number of shapes is not known):

Centers = [x0, y0, x1, y1, x2, y2,...,xn, yn]

Now I have to access the data in those two structures and perform the pointPolygonTest to check if a shape is inside another one, but OF COURSE it's really hard (because I understand that this solution with two data structure is not really the best)

That's the code I wrote for accessing the data structure:

for k in range(0, len(centers), 2):
    nowcx = centers[k]
    nowcy = centers[k+1]
    for j in range(len(shapes)):
        if cv2.pointPolygonTest(shapes[j],(nowcx, nowcy),False)==1:
            print "inside"
            print "outside"

For easier understanding, this is the picture with the centroids in red. You can see that thare are 3 shapes: triangle, square and pentagon. So the data structures contain 3 records for the 3 shapes and 6 elements for the 3 centroids.

The output gives me 9 comparisons, when they should be actually 3:

  1. triangle against square -> outside
  2. triangle against pentagon -> inside
  3. square againstr pentagon -> inside

That's the output:


What am I doing wrong? Please help me. Thanks in advance!!

edit retag flag offensive close merge delete