imgDial = cv2.dilate(imgThreshold, kernel, iterations=2) # APPLY DILATION imgThreshold = cv2.erode(imgDial, kernel, iterations=1) # APPLY EROSION ## FIND ALL COUNTOURS imgContours = img.copy() # COPY IMAGE FOR DISPLAY PURPOSES imgBigContour = img.copy() # COPY IMAGE FOR DISPLAY PURPOSES contours, hierarchy = cv2.findContours(imgThreshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # FIND ALL CONTOURS cv2.drawContours(imgContours, contours, -1, (0, 255, 0), 10) # DRAW ALL DETECTED CONTOURS # FIND THE BIGGEST COUNTOUR biggest, maxArea = utlis.biggestContour(contours) # FIND THE BIGGEST CONTOUR if biggest.size != 0: biggest=utlis.reorder(biggest) cv2.drawContours(imgBigContour, biggest, -1, (0, 255, 0), 20) # DRAW THE BIGGEST CONTOUR imgBigContour = utlis.drawRectangle(imgBigContour,biggest,2) pts1 = np.float32(biggest) # PREPARE POINTS FOR WARP pts2 = np.float32([[0, 0],[widthImg, 0], [0, heightImg],[widthImg, heightImg]]) # PREPARE POINTS FOR WARP matrix = cv2.getPerspectiveTransform(pts1, pts2) imgWarpColored = cv2.warpPerspective(img, matrix, (widthImg, heightImg)) #REMOVE 20 PIXELS FORM EACH SIDE imgWarpColored=imgWarpColored[20:imgWarpColored.shape[0] - 20, 20:imgWarpColored.shape[1] - 20] imgWarpColored = cv2.resize(imgWarpColored,(widthImg,heightImg)) # APPLY ADAPTIVE THRESHOLD imgWarpGray = cv2.cvtColor(imgWarpColored,cv2.COLOR_BGR2GRAY) imgAdaptiveThre= cv2.adaptiveThreshold(imgWarpGray, 255, 1, 1, 7, 2) imgAdaptiveThre = cv2.bitwise_not(imgAdaptiveThre) imgAdaptiveThre=cv2.medianBlur(imgAdaptiveThre,3)
## FIND ALL COUNTOURS imgConts = img.copy() imgBigConts = img.copy() contours, hierarchy = cv2.findContours( imgThreshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # FIND ALL CONTOURS cv2.drawContours(imgConts, contours, -1, (0, 255, 0), 10) # DRAW ALL DETECTED CONTOURS # FIND THE LARGEST COUNTOUR IN THE FRAME big, maxArea = utlis.biggestContour(contours) # FIND THE BIGGEST CONTOUR if big.size != 0: big = utlis.reorder(big) cv2.drawContours(imgBigConts, big, -1, (0, 255, 0), 20) # DRAW THE BIGGEST CONTOUR imgBigConts = utlis.drawRectangle(imgBigConts, big, 2) pts1 = np.float32(big) # PREPARE POINTS FOR WARP pts2 = np.float32([[0, 0], [widthImg, 0], [0, heightImg], [widthImg, heightImg]]) # PREPARE POINTS FOR WARP matrix = cv2.getPerspectiveTransform(pts1, pts2) imgWarpColored = cv2.warpPerspective(img, matrix, (widthImg, heightImg)) #REMOVE EXTRA UNWANTED PIXELS FROM THE SIDES imgWarpColored = imgWarpColored[20:imgWarpColored.shape[0] - 20, 20:imgWarpColored.shape[1] - 20] imgWarpColored = cv2.resize(imgWarpColored, (widthImg, heightImg)) # APPLY ADAPTIVE THRESHOLD imgWarpGray = cv2.cvtColor(imgWarpColored, cv2.COLOR_BGR2GRAY) imgAdaptiveThre = cv2.adaptiveThreshold(imgWarpGray, 255, 1, 1, 7, 2)