import cv2
import numpy as np



image  = cv2.imread("dig/digging.jpg")

cv2.imshow("Image", image)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("gray", gray)

blur = cv2.GaussianBlur(gray, (5,5), 0)
cv2.imshow("blur", blur)

thresh = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2)

#thresh = cv2.bitwise_not(thresh)

cv2.imshow("thresh", thresh)
#cv2.waitKey()
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

max_area = 0
c = 0
for i in contours:
        area = cv2.contourArea(i)
        if area > 1000:
                if area > max_area:
                    max_area = area
                    best_cnt = i
                    image = cv2.drawContours(image, contours, c, (0, 255, 0), 1)
        c+=1

mask = np.zeros((gray.shape),np.uint8)
cv2.drawContours(mask,[best_cnt],0,255,-1)
cv2.drawContours(mask,[best_cnt],0,0,1)
cv2.imshow("mask", mask)

out = np.zeros_like(gray)
out[mask == 255] = gray[mask == 255]
cv2.imshow("New image", out)

blur = cv2.GaussianBlur(out, (5,5), 0)
cv2.imshow("blur1", blur)

thresh = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2)
thresh = cv2.bitwise_not(thresh)
cv2.imshow("thresh1", thresh)

contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

squares = []
c = 0
for i in contours:
        area = cv2.contourArea(i)
        cnt_len = cv2.arcLength(i, True)
        cnt = cv2.approxPolyDP(i, 0.01 * cnt_len, True)
        if area > 1000/2:
            squares.append(cnt)
            #cv2.drawContours(image, contours, c, (0, 255, 0), 1)
        c+=1
cv2.drawContours(image, squares, -1, (0, 255, 0), 1)

cv2.imshow("Final Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()