# Force the line straight

**URL:** <https://forum.opencv.org/t/force-the-line-straight/10091>\
**Category:** Python\
**Created:** [September 6, 2022, 7:56pm UTC](https://forum.opencv.org/t/force-the-line-straight/10091 "2022-09-06T19:56:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor\_Sued](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/victor_sued/32/5973_2.png) [@Victor\_Sued](https://forum.opencv.org/u/Victor_Sued)\
**Post date:** [September 6, 2022, 7:56pm UTC](https://forum.opencv.org/t/force-the-line-straight/10091/1 "2022-09-06T19:56:14Z")

</div>

I’m trying to get the black rectangle to read the digits that are inside it.  
The code I have so far manages to select the area where this rectangle is, however, the line is entering the rectangle around the numbers, how to force this blue line to continue in a straight line?  
the green line is as I would like it to go…

My code:

```python
img = "gas\\0445550820210000018383.jpg"

img_bgr = cv.imread(img, cv.IMREAD_COLOR)
img_gray = bgr_2_gray(img_bgr)
_, img_th = cv.threshold(img_gray, 0, 255, cv.THRESH_OTSU)
contours, _ = cv.findContours(img_th, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
img_draw = cv.cvtColor(img_bgr, cv.COLOR_BGR2RGB) # opencv reads colors in BGR
cv.drawContours(img_draw, contours, -1, (0, 255, 0), 2)
areas = [cv.contourArea(contour) for contour in contours]
big_contours = [c for c in contours if cv.contourArea(c) > 10e3]
img_draw = cv.cvtColor(img_bgr, cv.COLOR_BGR2RGB)
cv.drawContours(img_draw, big_contours, -1, (0, 255, 0), 5)

for thresh in [30, 40, 50, 60, 70, 80, 90, 100]:
    _, img_th = cv.threshold(img_gray, thresh=thresh, maxval=255, type=cv.THRESH_BINARY)
    contours, _ = cv.findContours(img_th, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    big_contours = [c for c in contours if cv.contourArea(c) > 10e3]
    convex_contours = [cv.convexHull(c) for c in big_contours]
    img_draw = cv.cvtColor(img_th, cv.COLOR_GRAY2RGB)

count_contours = len(convex_contours)

if count_contours > 0:
    for i in range(len(big_contours)):
        contour = big_contours[i]
        minRect = cv.minAreaRect(big_contours[i])
        (x, y), (h, w), angle = minRect
        aspect_ratio = max(h / w, w / h)
        minRextBox = np.int0(cv.boxPoints(minRect))
        img_draw = cv.cvtColor(img_bgr, cv.COLOR_BGR2RGB)
        cv.drawContours(img_draw, [minRextBox], -1, (0, 0, 255), 2)
        apx = cv.approxPolyDP(contour, 0.003 * cv.arcLength(contour, True), True)
        cv.drawContours(img_draw, [apx], -1, (255, 0, 0), 2)

        # print(len(big_contours))
        cv.imshow("xxx", img_draw)
        cv.waitKey(0)
        cv.destroyAllWindows()

```

 ![2022-09-06_16-54](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/f/f1fc0408e900da18e7d0dfcc10d299251ebad1b4.png)
