使用opencv打印预期的输出

时间:2019-06-20 06:00:11

标签: python-3.x opencv

我正在使用opencv开发omr气泡扫描仪。我遇到了我很长一段时间无法克服的错误。任何人都可以通过它,让我知道。下面我提到了预期的输出。请仔细检查代码,并尽可能修正错误。谢谢

from imutils.perspective import four_point_transform
import matplotlib.pyplot as plt
from imutils import contours
import random
import numpy as np
import argparse
import imutils
import cv2


ANSWER_KEY={}
for i in range(45):
    ANSWER_KEY[i]=random.choice([0,1,2,3])

image = cv2.imread('H:/download.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 75, 120)

kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(edged,kernel,iterations = 8)


cnts, hiers = cv2.findContours(dilated.copy(), 
cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[1] if imutils.is_cv2() else cnts[0]
docCnt = None

if len(cnts) > 0:
    image, contours = cv2.findContours(dilated.copy(),cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for c in cnts:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        if len(approx) == 4:
            docCnt = approx     
            break
print(docCnt)

输出:

None

预期输出:

 array([[[ 142,  183]],

   [[ 137, 3767]],

   [[2896, 3827]],

   [[2957,  220]]], dtype=int32)

0 个答案:

没有答案