Search code examples
pythonopencvartificial-intelligence

How to merge contours in opencv?


Ok guys I have been working on this project for quite some time now.

I am building this bot that plays the chrome dinosaur game. So I tried other methods to detect the characters like matchTemplate and even made my own algorithm to locate the objects, but I like this one (findcontours) the most.

Here's what I have:

What my program sees

Can anyone help me find out how I should merge the two rectangles of the cacti?

img = screen_cap()
roi = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(roi,127, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
first = True
for cnt in contours:
    area = cv2.contourArea(cnt)
    if area > 200: #filtering contours
        x,y,w,h = cv2.boundingRect(cnt)
        if w/h < 4: # filtering even more
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

Solution

  • This is an old question and seems like it has not been answered properly yet (apologies to fellow SOers who did it partially in comments though). To me it seems like the questioner has a two-part question:

    1. Is there a opencv function that merges two rectangles?

    Answer to this question is yes and no. Let me be clear; yes if you are using opencv C++ bindings. Simple & can be used to take a union and | for intersection of two rects. But Python bindings lack those functions.

    1. How to then do it in Python?

      def union(a,b):
          x = min(a[0], b[0])
          y = min(a[1], b[1])
          w = max(a[0]+a[2], b[0]+b[2]) - x
          h = max(a[1]+a[3], b[1]+b[3]) - y
          return (x, y, w, h)
      
      def intersection(a,b):
          x = max(a[0], b[0])
          y = max(a[1], b[1])
          w = min(a[0]+a[2], b[0]+b[2]) - x
          h = min(a[1]+a[3], b[1]+b[3]) - y
          if w<0 or h<0: return () # or (0,0,0,0) ?
          return (x, y, w, h)
          # Please remember a and b are rects.
      

    Source Code Credit: OpenCV union and intersaction on rects