コード例 #1
0
    # imgThreshold = cv2.dilate(imgThreshold, kernel, iterations=3)
    ## FIND ALL COUNTOURS

    imgContours = img.copy()  # COPY IMAGE FOR DISPLAY PURPOSES
    imgBigContour = img.copy()  # COPY IMAGE FOR DISPLAY PURPOSES
    _, contours, _ = cv2.findContours(
        imgThreshold, cv2.RETR_TREE,
        cv2.CHAIN_APPROX_SIMPLE)  # FIND ALL CONTOURS
    cv2.drawContours(imgContours, contours, -1, (0, 255, 0),
                     10)  # DRAW ALL DETECTED CONTOURS
    resized = cv2.resize(imgContours, (heightImg // 2, widthImg // 2))
    cv2.imshow("contoured image", resized)
    cv2.waitKey(0)
    # FIND THE BIGGEST COUNTOUR
    biggest, maxArea = utlis.biggestContour(
        contours)  # FIND THE BIGGEST CONTOUR
    if biggest.size != 0:
        # print("biggest",biggest)

        biggest, flag = utlis.reorder(biggest)
        if flag == 1:
            for contour in contours:
                (x, y, w, h) = cv2.boundingRect(contour)
                cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 10)

            print("error!!")
            img = cv2.resize(img, (heightImg // 2, widthImg // 2))
            cv2.imshow("image for try", img)
            cv2.waitKey(0)
            break
        # out = img[ topx:bottomx+1,topy:bottomy+1]
コード例 #2
0
    imgBlur = cv2.GaussianBlur(imgGray, (5, 5), 1)
    thres = utlis.valTrackbars()

    imgThreshold = cv2.Canny(imgBlur, thres[0], thres[1])
    kernel = np.ones((5, 5))
    imgDial = cv2.dilate(imgThreshold, kernel, iterations=2)  #elimina blancos
    imgThreshold = cv2.erode(imgDial, kernel,
                             iterations=1)  # engruesa la imagen

    imgContours = img.copy()
    imgBigContour = img.copy()
    contours, hierarchy = cv2.findContours(imgThreshold, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(imgContours, contours, -1, (0, 255, 0), 10)

    biggest, maxArea = utlis.biggestContour(contours)

    if biggest.size != 0:
        biggest = utlis.reorder(biggest)
        cv2.drawContours(imgBigContour, biggest, -1, (0, 255, 0), 20)
        imgBigContour = utlis.drawRectangle(imgBigContour, biggest, 2)

        pts1 = np.float32(biggest)
        pts2 = np.float32([[0, 0], [widthImg, 0], [0, heightImg],
                           [widthImg, heightImg]])

        matrix = cv2.getPerspectiveTransform(pts1, pts2)

        imgWarpColored = cv2.warpPerspective(img, matrix,
                                             (widthImg, heightImg))