# Merge Nearby Bounding Boxes

**URL:** <https://forum.opencv.org/t/merge-nearby-bounding-boxes/3906>\
**Category:** Python\
**Created:** [June 18, 2021, 7:31am UTC](https://forum.opencv.org/t/merge-nearby-bounding-boxes/3906 "2021-06-18T07:31:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aimi\_Azizan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/aimi_azizan/32/673_2.png) [@Aimi\_Azizan](https://forum.opencv.org/u/Aimi_Azizan)\
**Post date:** [June 18, 2021, 7:31am UTC](https://forum.opencv.org/t/merge-nearby-bounding-boxes/3906/1 "2021-06-18T07:31:06Z")

</div>

I have a code that can detect sunspots but the output that it gave detected the spots individually. However, I want the spots to be grouped like this image. How do I merge the boxes to get an output similar to this picture?

 ![jBMKl](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/8/8ab43e0da21fbe81296ad193dc23d97cba2af8aa.jpeg)

Here is my code. Any help would be appreciated, thanks.

```auto
import os
import cv2 # opencv library
import numpy as np
import matplotlib.pyplot as plt

"""Make the pwd implementation"""
cwd = os.getcwd()
file = "/sunspot1.jpg"
path = cwd + file
image = cv2.imread(path,0)

image_1 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
#plt.show()

#plot the image in graycolor
#gray = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)
#plt.imshow(gray)
#plt.show()

# perform image thresholding
ret, thresh = cv2.threshold(image, 90, 255, cv2.THRESH_BINARY)
#plt.imshow(thresh, cmap = 'gray')
#plt.show()

#circle = cv2.circle(thresh, (249,249),(238),(0, 255, 0),1)
# plt.imshow(circle)
# plt.show()

# find taches contours
contours, hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
valid_cntrs = []

for i,cntr in enumerate(contours):
    x,y,w,h = cv2.boundingRect(cntr)
    #print("x = ",x,"y = ",y,"w = ",w,"h = ",h)
    if ((x-249)**2 + (y-249)**2)<= 238**2:
        valid_cntrs.append(cntr)
"""implement image size detection for the contour LINE 36"""
        
#count the taches number
taches= len(valid_cntrs);
#sunspot= 1*(10*groups+taches);

# count the number of dicovered sunspots
print("The number of taches is: ",taches)

if taches == 0:
    plt.imshow(image_1)
    plt.show()

else:

    contour_sizes = [(cv2.contourArea(contour), contour) for contour in valid_cntrs]

    for i in range(len(valid_cntrs)):
        x,y,w,h = cv2.boundingRect(contour_sizes[i][1])
        final = cv2.rectangle(image_1,(x,y),(x+w,y+h),(0,255,0),1)

plt.imshow(final)
plt.show()

```

---

<div class="post-metadata">

**Author:** ![astrid\_coder](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/astrid_coder/32/2432_2.png) [@astrid\_coder](https://forum.opencv.org/u/astrid_coder)\
**Post date:** [June 18, 2021, 7:35am UTC](https://forum.opencv.org/t/merge-nearby-bounding-boxes/3906/2 "2021-06-18T07:35:45Z")

</div>

Hi, Aimi\_Azizan.  
In my opinion, you can use morphology operations & fill connection.  
If you want, I can help u.  
Regards.

---

<div class="post-metadata">

**Author:** ![Aimi\_Azizan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/aimi_azizan/32/673_2.png) [@Aimi\_Azizan](https://forum.opencv.org/u/Aimi_Azizan)\
**Post date:** [June 18, 2021, 10:10am UTC](https://forum.opencv.org/t/merge-nearby-bounding-boxes/3906/4 "2021-06-18T10:10:49Z")

</div>

Hi, Astrid.  
I would really appreciate the help. Is it the same as merging them after thresholding by using morphology close with a kernel size that would connect them?

---

<div class="post-metadata">

**Author:** ![astrid\_coder](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/astrid_coder/32/2432_2.png) [@astrid\_coder](https://forum.opencv.org/u/astrid_coder)\
**Post date:** [June 18, 2021, 1:27pm UTC](https://forum.opencv.org/t/merge-nearby-bounding-boxes/3906/5 "2021-06-18T13:27:26Z")

</div>

References:

- [image processing - Filling holes inside a binary object - Stack Overflow](https://stackoverflow.com/questions/10316057/filling-holes-inside-a-binary-object)
- [Filling holes in an image using OpenCV ( Python / C++ ) | Learn OpenCV](https://learnopencv.com/filling-holes-in-an-image-using-opencv-python-c/)
