# Detecting rectangle with border in image

**URL:** <https://forum.opencv.org/t/detecting-rectangle-with-border-in-image/5287>\
**Category:** Python\
**Created:** [September 19, 2021, 2:00pm UTC](https://forum.opencv.org/t/detecting-rectangle-with-border-in-image/5287 "2021-09-19T14:00:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![codingmonk](https://avatars.discourse-cdn.com/v4/letter/c/b4bc9f/32.png) [@codingmonk](https://forum.opencv.org/u/codingmonk)\
**Post date:** [September 19, 2021, 2:00pm UTC](https://forum.opencv.org/t/detecting-rectangle-with-border-in-image/5287/1 "2021-09-19T14:00:34Z")

</div>

I’m trying to find coordinates of rectangle border in the image [https://i.stack.imgur.com/KKjfg.gif](https://i.stack.imgur.com/KKjfg.gif) using opencv but I’m not able to achieve this instead I end up getting some random not present rectangles on image. Below is my code of the execution

```auto
import cv2
import numpy as np
import random as rng
image = cv2.imread('strawberry.png')
threshold = 100
grayscale= cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
canny_output = cv2.Canny(grayscale, threshold, threshold * 2)

ret, thresh= cv2.threshold(grayscale,200,255,cv2.THRESH_BINARY_INV)
contours, _ = cv2.findContours(canny_output,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
contours_poly = [None]*len(contours)
#boundRect = [None]*len(contours)
boundRect = list()
centers = [None]*len(contours)
radius = [None]*len(contours)
for i, c in enumerate(contours):
    contours_poly[i] = cv2.approxPolyDP(c, 3, True)
    if len(contours_poly[i]) == 4:
        boundRect.append(cv2.boundingRect(contours_poly[i]))
    #boundRect[i] = cv.boundingRect(contours_poly[i])
    #centers[i], radius[i] = cv.minEnclosingCircle(contours_poly[i])

drawing = np.zeros((canny_output.shape[0], canny_output.shape[1], 3), dtype=np.uint8)

print(len(boundRect))
#for i in range(len(contours)):
# color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256))
# cv.drawContours(drawing, contours_poly, i, color)
# cv.rectangle(drawing, (int(boundRect[i][0]), int(boundRect[i][1])), \
# (int(boundRect[i][0]+boundRect[i][2]), int(boundRect[i][1]+boundRect[i][3])), color, 2)
#cv.circle(drawing, (int(centers[i][0]), int(centers[i][1])), int(radius[i]), color, 2)
for i in range(len(boundRect)):
    if boundRect[i][2]/boundRect[i][3] > 2:
        color = (rng.randint(0,256), rng.randint(0,256), rng.randint(0,256))
        cv2.rectangle(drawing, (int(boundRect[i][0]), int(boundRect[i][1])), \
            (int(boundRect[i][0]+boundRect[i][2]), int(boundRect[i][1]+boundRect[i][3])), color, 2)
        print(f'Rectangle X:{boundRect[i][0]}, Y:{boundRect[i][1]}, W:{boundRect[i][2]}, H:{boundRect[i][3]}')

```

```auto
I end up identifying random rectangles not the which I'm interested in like below
[detected rectangle output](https://i.stack.imgur.com/32QQx.png)

How can I detect rectangle coordinates present in image with white border? what am I doing wrong in my approach, any suggestions or guide on this will be very helpful thanks
```

---

<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:** [September 19, 2021, 3:26pm UTC](https://forum.opencv.org/t/detecting-rectangle-with-border-in-image/5287/2 "2021-09-19T15:26:58Z")

</div>

related, with answer:

> <https://stackoverflow.com/questions/69193917/how-to-find-white-bordered-rectangle-in-an-image>

---

<div class="post-metadata">

**Author:** ![codingmonk](https://avatars.discourse-cdn.com/v4/letter/c/b4bc9f/32.png) [@codingmonk](https://forum.opencv.org/u/codingmonk)\
**Post date:** [September 19, 2021, 3:49pm UTC](https://forum.opencv.org/t/detecting-rectangle-with-border-in-image/5287/3 "2021-09-19T15:49:36Z")

</div>

Tried it didn’t get expected output using python

---

<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:** [September 19, 2021, 4:42pm UTC](https://forum.opencv.org/t/detecting-rectangle-with-border-in-image/5287/4 "2021-09-19T16:42:16Z")

</div>

can’t work with “it didn’t work”

every time you feel like saying “it didn’t work”, catch yourself, and instead say something that conveys information.

use python’s debugger on your code. report your findings.
