# Detect different regions from the image

**URL:** <https://forum.opencv.org/t/detect-different-regions-from-the-image/7695>\
**Category:** Python\
**Tags:** findcontours\
**Created:** [February 24, 2022, 12:43am UTC](https://forum.opencv.org/t/detect-different-regions-from-the-image/7695 "2022-02-24T00:43:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krupali](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/krupali/32/4600_2.png) [@Krupali](https://forum.opencv.org/u/Krupali)\
**Post date:** [February 24, 2022, 12:43am UTC](https://forum.opencv.org/t/detect-different-regions-from-the-image/7695/1 "2022-02-24T00:43:26Z")

</div>

I have an image and want to detect different regions using OpenCV contouring. The regions very in size.  
The resultant image I want is

 ![2IP6f](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/2/207fa1b9751f2b77ee984d443db834c4a4447bde.jpeg)  
So, I tried contouring but its difficult to detect all the three mentioned region at a time.  
Can anyone suggest me different methods for it, I am trying Template Matching method where I have created different templates and to match it , which works but can these be detected using contouring.  
I have my below code for contouring:

```auto
image = cv2.imread("dst11.jpg")
new_image = image.copy()
src = cv2.GaussianBlur(image, (3, 3), 0)
imgray = cv2.cvtColor(src,cv2.COLOR_BGR2GRAY)
ret, bw_img = cv2.threshold(imgray,85,255, cv2.THRESH_BINARY)
iin = ~bw_img
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS ,(3,2))
dilation = cv2.dilate(iin,kernel,iterations = 2)
_,contours, hierarchy = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print("Number of Contours found = " + str(len(contours)))
for c in contours:
    perimeter = cv2.arcLength(c,True)
    x, y, w, h = cv2.boundingRect(c)
    if perimeter > 60 and perimeter < 150:
        print(perimeter)
        cv2.rectangle(image,(x,y), (x+w,y+h), (0,0,255), 2)
plt.imshow(image,cmap='gray')
plt.show()

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [February 24, 2022, 6:49am UTC](https://forum.opencv.org/t/detect-different-regions-from-the-image/7695/2 "2022-02-24T06:49:59Z")

</div>

related:

- [python - detect different region from the image using contouring - Stack Overflow](https://stackoverflow.com/questions/71106810/detect-different-region-from-the-image-using-contouring)
- [python - remove the white rectangle part of the image and merge the remaining image - Stack Overflow](https://stackoverflow.com/questions/71013564/remove-the-white-rectangle-part-of-the-image-and-merge-the-remaining-image)

---

<div class="post-metadata">

**Author:** ![Krupali](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/krupali/32/4600_2.png) [@Krupali](https://forum.opencv.org/u/Krupali)\
**Post date:** [February 25, 2022, 2:26am UTC](https://forum.opencv.org/t/detect-different-regions-from-the-image/7695/3 "2022-02-25T02:26:44Z")

</div>

Yes both are the question posted by me.
