def startFrame():
    # Read a frame from feed
    global frame
    ret, frame = cap.read()
    # Convert to greyscale frame
    global frame_gray
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Get histogram of frame
    hist_img = cv2.calcHist([frame], [0], None, [256], [0, 256])
    #plt.plot(hist_img)
    #plt.show()
    
    # Pass frame to histogram adjustment to remove ouliers
    hist_no_outliers, lower_index = outliers.removeOutliersThresh(hist_img)
    #plt.plot(hist_no_outliers)
    #plt.show()
    
    # Pass histogram to adaptive thresholding to determine level
    global threshLevel
    threshLevel = thresh.bi_level_img_threshold(hist_no_outliers)
    
    # Adjust start index of hist and add manual level adjustment
    global threshLevelAdjust
    threshLevelAdjust = threshLevel + lower_index + 15
    #print('Bi level thresh', threshLevelAdjust)
    
    struct_el = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(25,25))
    global frame_open
    frame_open = cv2.morphologyEx(frame_gray, cv2.MORPH_OPEN, struct_el)
    #cv2.imshow('open',frame_open)

    return frame_open
def imgThreshold(frame):

    # Convert to greyscale frame
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    #cv2.imshow('original',frame_gray)

    # Create structuring element - disk to remove glint
    struct_el = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(35,35))
    frame_open = cv2.morphologyEx(frame_gray, cv2.MORPH_OPEN, struct_el)
    #cv2.imshow('open',frame_open)

    # Get histogram of frame
    # more efficient than calcHist and eliminates memory error
    hist_img = np.bincount(frame_gray.flatten())
    #print('len_hist',len(hist_img))
    # truncate histogram to remove bin 0 and bin 255 instead of removing outliers which
    # removes all the bins
    hist_img = hist_img[1:len(hist_img)-2]

    # Pass histogram to adaptive thresholding to determine level

    threshLevel = thresh.bi_level_img_threshold(hist_img)
        #if(threshLevel > 100):
    threshLevel = 30

    # Adjust start index of hist and add manual level adjustment
    #threshLevelAdjust = threshLevel + lower_index
    #print('Bi level thresh', threshLevelAdjust)


    # Threshold frame using level obtained from adaptive threshold
    #print ("thresh level %d " % threshLevel)
    ret,threshPupil = cv2.threshold(frame_open,threshLevel,255,cv2.THRESH_BINARY)
    #cv2.imshow('thresh pupil',threshPupil)
   

    # Invert and threshold frame to isolate only glint
    #frameInv = np.invert(frame_gray)
    #cv2.imshow('frameInv',frameInv)

    ret,threshGlint = cv2.threshold(frame_gray,200,255,cv2.THRESH_BINARY_INV)
    #cv2.imshow('thresh glint',threshGlint)

    return threshPupil, threshGlint
    # Convert to greyscale frame
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
i = i+1
print('i ', i)
    # Get histogram of frame
hist_img = cv2.calcHist([frame_gray], [0], None, [256], [0, 256])
    #plt.plot(hist_img)
    #plt.show()

    # Pass frame to histogram adjustment to remove ouliers
hist_no_outliers, lower_index = outliers.removeOutliersThresh(hist_img)
    #plt.plot(hist_no_outliers)
    #plt.show()

    # Pass histogram to adaptive thresholding to determine level
threshLevel = thresh.bi_level_img_threshold(hist_no_outliers)

    # Adjust start index of hist and add manual level adjustment
threshLevelAdjust = 40 #threshLevel + lower_index 
print('Bi level thresh', threshLevelAdjust)

plt.plot(hist_no_outliers)
plt.show()

struct_el = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(25,25))
frame_open = cv2.morphologyEx(frame_gray, cv2.MORPH_OPEN, struct_el)
cv2.imshow('open',frame_open)
    # Threshold frame using level obtained from adaptive threshold
ret,frameBinary = cv2.threshold(frame_open,threshLevelAdjust,255,cv2.THRESH_BINARY)
cv2.imshow('binaryOrig',frameBinary)