-1

This my code in python 3.5 but a have problem when running this code:

import matplotlib.pyplot as plt 
import imutils 
import numpy as np  
import cv2

image=cv2.imread('/home/pi/Downloads/marquage-laser-de-codes-matriciels-de-donnees-913.jpg')
ratio = image.shape[0] / 300.0
orig = image.copy()
image = imutils.resize(image, height = 300)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
edged = cv2.Canny(gray, 30, 200)
plt.subplot(151); plt.title('A')
plt.imshow(edged)
dst, cnts, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:10]
screenCnt = None
for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    if len(approx) == 4:
        screenCnt = approx
        break
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 3)
cv2.imshow("Game Boy Screen", image)
cv2.waitKey(0)
plt.show() 

This is the message of error:

Traceback (most recent call last):
  File "/home/pi/Desktop/programme 2.py", line 23, in <module>
    cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 3)
cv2.error: OpenCV(3.4.3) /home/pi/packaging/opencv-python/opencv/modules/imgproc/src/drawing.cpp:2613: error: (-215:Assertion failed) reader.ptr != NULL in function 'cvDrawContours'  

Please can one help me.

  • Can you check if screenCnt is empty? – yapws87 Nov 21 '18 at 13:43
  • Yes screenCnt is empty.It gives "None" – moez messi Nov 21 '18 at 14:52
  • can you try drawing contour only when screenCnt is not empty? – yapws87 Nov 21 '18 at 23:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.