0

I have an array containing the coordinates of many rectangles as a numpy array from opencv.

rec_arr = [[1404,  677,   73,   73],
           [2670, 1062,  115,  115],
           [2981, 1077,  114,  114],
           [ 691, 1077,  107,  107],
           [2251, 1272,  131,  131],
           [3035, 1239,  139,  139],
           [1050, 1359,  135,  135],
           [ 887, 1663,  149,  149]]

this is a representation of 1 rectangle given from the array x, y, w, h

+--------------------> X axis
|
|    (X,Y)      (X+W, Y)
|    +--------------+
|    |              |
|    |              |
|    |              |
|    +--------------+
v    (X, Y+H)     (X+W,Y+H)

Y axis

I want to loop through the array to compare all the rectangles to each other then highlight the rectangles that intersect. this is my attempt:

for (x, y, w, h) in rec_arr:
                X1 = x
                Y1 = y
                W1 = w
                H1 = h


                for (x, y, w, h) in rec_arr:
                    X2 = x
                    Y2 = y
                    W2 = w
                    H2 = h

                    if (X1+W1<X2 or X2+W2<X1 or Y1+H1<Y2 or Y2+H2<Y1):
                        print 'no intersect'
                    else:
                        print 'intersect'
                        cv2.rectangle(image, (x, y), (x+w, y+h), (0, 0, 255), -1)

I've also attempted using itertools.combinations() to compare each combination of 2 rectangles in the array with no success. any help would be great!

  • 1
    openCV rectangles have operator & for intersection. If result has area > 0 they interintersect. – Micka Feb 16 '15 at 20:00
  • There seems to be something wrong with your indentation - the "if" is not part of the inner for loop. – Sebastian Feb 16 '15 at 21:35
  • point taken - this still does not work and ends up marking every rectangle – androidwiltron Feb 16 '15 at 23:15
  • Ran you script. It prints intersect for some pairs and no intersect for others. So problem is not that everything is intersecting with everything. Script correct, but your expectation is wrong? – Sebastian Feb 18 '15 at 20:10

0 Answers0