# Crop defined area in countours

**URL:** <https://forum.opencv.org/t/crop-defined-area-in-countours/5274>\
**Category:** Python\
**Tags:** imgproc, alpr\
**Created:** [September 18, 2021, 3:20am UTC](https://forum.opencv.org/t/crop-defined-area-in-countours/5274 "2021-09-18T03:20:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diego\_Foca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/diego_foca/32/1034_2.png) [@Diego\_Foca](https://forum.opencv.org/u/Diego_Foca)\
**Post date:** [September 18, 2021, 3:20am UTC](https://forum.opencv.org/t/crop-defined-area-in-countours/5274/1 "2021-09-18T03:20:21Z")

</div>

Hello. There is anyway that I can crop a defined area in contours ? I’m trying to crop a license car plate, but all algorithms that I tried work sometimes cropping exactly area of license car, sometimes contours find the right rectangle but it crops the wrong one, if I have x and y approximately could I do anything better ? Thanks all

---

<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 18, 2021, 12:04pm UTC](https://forum.opencv.org/t/crop-defined-area-in-countours/5274/2 "2021-09-18T12:04:52Z")

</div>

please present your data and code. then we have something to work with.

and there’s no need to tag this discussion “opencv”. the whole forum is opencv.

---

<div class="post-metadata">

**Author:** ![Diego\_Foca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/diego_foca/32/1034_2.png) [@Diego\_Foca](https://forum.opencv.org/u/Diego_Foca)\
**Post date:** [September 18, 2021, 1:43pm UTC](https://forum.opencv.org/t/crop-defined-area-in-countours/5274/3 "2021-09-18T13:43:01Z")

</div>

Right. The code works, but when a start getting lots of images, the percents of cropped plates is too low. So I thought setting the size of plate image could be a good idea.

```python
img = cv2.imread('3.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2)
contours,h = cv2.findContours(thresh,1,2)

largest_rectangle = [0,0]
for cnt in contours:
  approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
  if len(approx) == 4:
    area = cv2.contourArea(cnt)
    if area > largest_rectangle[0]:
      largest_rectangle = [cv2.contourArea(cnt), cnt, approx]

x, y, w, h = cv2.boundingRect(largest_rectangle[1])
roi = img[y: y + h, x : x + w]
plt.imshow(roi, cmap = 'gray')
plt.show()

```

---

<div class="post-metadata">

**Author:** ![Diego\_Foca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/diego_foca/32/1034_2.png) [@Diego\_Foca](https://forum.opencv.org/u/Diego_Foca)\
**Post date:** [September 18, 2021, 1:55pm UTC](https://forum.opencv.org/t/crop-defined-area-in-countours/5274/4 "2021-09-18T13:55:05Z")

</div>

The plate shape is between 35, 80 and 145, 285

---

<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 18, 2021, 2:45pm UTC](https://forum.opencv.org/t/crop-defined-area-in-countours/5274/5 "2021-09-18T14:45:09Z")

</div>

and the data please.

---

<div class="post-metadata">

**Author:** ![Diego\_Foca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/diego_foca/32/1034_2.png) [@Diego\_Foca](https://forum.opencv.org/u/Diego_Foca)\
**Post date:** [September 18, 2021, 6:21pm UTC](https://forum.opencv.org/t/crop-defined-area-in-countours/5274/6 "2021-09-18T18:21:36Z")

</div>

![Inked1631797903.3958_LI](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/67fb1344f992b6f7717794f9cfff65981a477885.jpeg)  
 ![Inked1631797909.847415_LI](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/e/e65b16d4e06c18935160cac4e494fb358d66913e.jpeg)  
 ![Inked1631797999.5189052_LI](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/6797e289bd07c48e475ab5640c64650ca1c318ba.jpeg)

---

<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 18, 2021, 6:59pm UTC](https://forum.opencv.org/t/crop-defined-area-in-countours/5274/7 "2021-09-18T18:59:22Z")

</div>

okay, so your code gives you upright crops of the license plate… and what don’t you like about that? I hate having to guess. please clarify clearly what you need. show an example (picture) if you need to. that’s clearer than a verbal attempt.

---

<div class="post-metadata">

**Author:** ![Diego\_Foca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/diego_foca/32/1034_2.png) [@Diego\_Foca](https://forum.opencv.org/u/Diego_Foca)\
**Post date:** [September 18, 2021, 7:51pm UTC](https://forum.opencv.org/t/crop-defined-area-in-countours/5274/8 "2021-09-18T19:51:44Z")

</div>

The most of times it gets those pictures below, but sometime it works as well, and a don’t know why is happening that. Maybe setting the size of plate shape could be the solution, but I don’t know how to do it.

![index3](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/f/f0561faa93ce1beec5bb840af2cc532c41e9e341.png)  
 ![index2](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/d/d6b52fa6273536e6cb33deeb1fc237c984fcef54.png)  
 ![index](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/b/bb10ad3c2bfaed7407198d7942c93c1aef5fc250.png)

---

<div class="post-metadata">

**Author:** ![Diego\_Foca](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/diego_foca/32/1034_2.png) [@Diego\_Foca](https://forum.opencv.org/u/Diego_Foca)\
**Post date:** [September 18, 2021, 7:52pm UTC](https://forum.opencv.org/t/crop-defined-area-in-countours/5274/9 "2021-09-18T19:52:53Z")

</div>

![index4](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/3/34066f26b1e7d83d3d3698943720b4de3cc86e64.png)

---

<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 18, 2021, 8:54pm UTC](https://forum.opencv.org/t/crop-defined-area-in-countours/5274/10 "2021-09-18T20:54:03Z")

</div>

looks like a bad threshold. look at the binarized image (output of threshold).
