# OpenCv cannot detect contour

**URL:** <https://forum.opencv.org/t/opencv-cannot-detect-contour/3184>\
**Category:** Python\
**Tags:** imgproc\
**Created:** [May 6, 2021, 9:12am UTC](https://forum.opencv.org/t/opencv-cannot-detect-contour/3184 "2021-05-06T09:12:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![abed\_dost4](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/abed_dost4/32/2034_2.png) [@abed\_dost4](https://forum.opencv.org/u/abed_dost4)\
**Post date:** [May 6, 2021, 9:12am UTC](https://forum.opencv.org/t/opencv-cannot-detect-contour/3184/1 "2021-05-06T09:12:18Z")

</div>

import cv2  
import imutils  
import numpy as np  
import pytesseract

pytesseract.pytesseract.tesseract\_cmd = r’C:\Program Files\Tesseract-OCR\tesseract.exe’

img = cv2.imread(‘skoda1.jpg’, cv2.IMREAD\_COLOR)  
img = cv2.resize(img, (600, 400))

gray = cv2.cvtColor(img, cv2.COLOR\_BGR2GRAY)  
gray = cv2.bilateralFilter(gray, 13, 15, 15)

edged = cv2.Canny(gray, 30, 200)  
contours = cv2.findContours(edged.copy(), cv2.RETR\_TREE, cv2.CHAIN\_APPROX\_SIMPLE)  
contours = imutils.grab\_contours(contours)  
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]  
screenCnt = None

for c in contours:

```
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.018 * peri, True)

if len(approx) == 4:
    screenCnt = approx
    break

```

if screenCnt is None:  
detected = 0  
print(“No contour detected”)  
else:  
detected = 1

if detected == 1:  
cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3)

mask = np.zeros(gray.shape, np.uint8)  
new\_image = cv2.drawContours(mask, [screenCnt], 0, 255, -1, )  
new\_image = cv2.bitwise\_and(img, img, mask=mask)

(x, y) = np.where(mask == 255)  
(topx, topy) = (np.min(x), np.min(y))  
(bottomx, bottomy) = (np.max(x), np.max(y))  
Cropped = gray[topx:bottomx + 1, topy:bottomy + 1]

text = pytesseract.image\_to\_string(Cropped, config=’–psm 11’)  
print(“programming\_fever’s License Plate Recognition\n”)  
print(“Detected license plate Number is:”, text)  
img = cv2.resize(img, (500, 300))  
Cropped = cv2.resize(Cropped, (400, 200))  
cv2.imshow(‘car’, img)  
cv2.imshow(‘Cropped’, Cropped)

cv2.waitKey(0)  
cv2.destroyAllWindows()

i get this error:

File “C:\Users\abdul\PycharmProjects\ALPR\main.py”, line 39, in

```
new_image = cv2.drawContours(mask, [screenCnt], 0, 255, -1, )

```

cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wvn\_it83\opencv\modules\imgproc\src\drawing.cpp:2598: error: (-215:Assertion failed) reader.ptr != NULL in function ‘cvDrawContours’

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [May 6, 2021, 10:30am UTC](https://forum.opencv.org/t/opencv-cannot-detect-contour/3184/2 "2021-05-06T10:30:22Z")

</div>

this one is correct:

> [@abed\_dost4](#):
>
> cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3)

this one isn’t:

> [@abed\_dost4](#):
>
> cv2.drawContours(mask, [screenCnt], 0, 255, -1, )

please [look at docs](https://docs.opencv.org/master/d6/d6e/group __imgproc__ draw.html#ga746c0625f1781f1ffc9056259103edbc) , too !
