def otsu_one(img, img_file, man_img, mask=None):
    # cv2.GaussianBlur(img, (5,5),0)
    # Bilateral Filter is used separately so it can be adjusted for each type of segmentation
    blur = cv2.bilateralFilter(img, 5, 5, 5)
    th = single_threshold_otsu(blur, mask)
    if mask is None:
        ret, thresh = cv2.threshold(blur, th, 255, cv2.THRESH_BINARY)
    else:
        combined_img = cv2.bitwise_and(blur, blur, mask=mask)
        ret, thresh = cv2.threshold(combined_img, th, 255, cv2.THRESH_BINARY)
    # Otsu result image
    out_img_o = thresh
    # morphological ops
    kernel = morphologicalOps.open_center_kernel()
    out_img_o = morphologicalOps.opening(out_img_o, kernel)

    out_info_o = "_otsu_%d" % (th)
    out_str_o = out_info_o + '.png'
    out_file_o = re.sub(r'\.jpg', out_str_o, img_file)
    cv2.imwrite(out_file_o, out_img_o)
    t = evaluation.findTotals(out_img_o, man_img)
    f = open('o1_all.txt', 'a')
    f.write(img_file + " " + str(t[0]) + " " + str(t[1]) + " " + str(t[2]) +
            " " + str(t[3]) + "\n")
    f.close()
def bilateral_filter(img, seed, th, img_file, out_info, man_img):
    img_b = cv2.bilateralFilter(img, 5, 5, 5)
    #  SRG result image
    out_img_b = seeded_region_growing(img_b, seed, th)
    # morphological ops
    kernel = morphologicalOps.open_elliptical_kernel()
    out_img_b = morphologicalOps.opening(out_img_b, kernel)

    out_str_b = out_info + '_b.png'
    out_file_b = re.sub(r'\.jpg', out_str_b, img_file)
    cv2.imwrite(out_file_b, out_img_b)
    t = evaluation.findTotals(out_img_b, man_img)
    f = open('b_all.txt', 'a')
    f.write(img_file + " " + str(t[0]) + " " + str(t[1]) + " " + str(t[2]) + " " + str(t[3]) + "\n")
    f.close()
def unsmoothed(img, seed, th, img_file, out_info, man_img):
    img_n = img
    # SRG result image
    out_img_n = seeded_region_growing(img_n, seed, th)
    # Morphological op
    kernel = morphologicalOps.open_elliptical_kernel()
    out_img_n = morphologicalOps.opening(out_img_n, kernel)

    out_str_n = out_info + '_n.png'
    out_file_n = re.sub(r'\.jpg', out_str_n, img_file)
    cv2.imwrite(out_file_n, out_img_n)
    t = evaluation.findTotals(out_img_n, man_img)
    f = open('n_all.txt', 'a')
    f.write(img_file + " " + str(t[0]) + " " + str(t[1]) + " " + str(t[2]) + " " + str(t[3]) + "\n")
    f.close()
def bilateral_filter(img, seed, th, img_file, out_info, man_img):
    img_b = cv2.bilateralFilter(img, 5, 5, 5)
    #  SRG result image
    out_img_b = seeded_region_growing(img_b, seed, th)
    # morphological ops
    kernel = morphologicalOps.open_cross_kernel()
    out_img_b = morphologicalOps.opening(out_img_b, kernel)

    out_str_b = out_info + '_b.png'
    out_file_b = re.sub(r'\.jpg', out_str_b, img_file)
    cv2.imwrite(out_file_b, out_img_b)
    t = evaluation.findTotals(out_img_b, man_img)
    f = open('b_all.txt', 'a')
    f.write(img_file + " " + str(t[0]) + " " + str(t[1]) + " " + str(t[2]) + " " + str(t[3]) + "\n")
    f.close()
def unsmoothed(img, seed, th, img_file, out_info, man_img):
    img_n = img
    # SRG result image
    out_img_n = seeded_region_growing(img_n, seed, th)
    # Morphological op
    kernel = morphologicalOps.open_cross_kernel()
    out_img_n = morphologicalOps.opening(out_img_n, kernel)

    out_str_n = out_info + '_n.png'
    out_file_n = re.sub(r'\.jpg', out_str_n, img_file)
    cv2.imwrite(out_file_n, out_img_n)
    t = evaluation.findTotals(out_img_n, man_img)
    f = open('n_all.txt', 'a')
    f.write(img_file + " " + str(t[0]) + " " + str(t[1]) + " " + str(t[2]) + " " + str(t[3]) + "\n")
    f.close()
def otsu_one(img, img_file, man_img, mask=None):
    # cv2.GaussianBlur(img, (5,5),0)
    # Bilateral Filter is used separately so it can be adjusted for each type of segmentation
    blur = cv2.bilateralFilter(img, 5, 5, 5)
    th = single_threshold_otsu(blur, mask)
    if mask is None:
        ret, thresh = cv2.threshold(blur,th,255,cv2.THRESH_BINARY)
    else:
        combined_img = cv2.bitwise_and(blur, blur, mask=mask)
        ret, thresh = cv2.threshold(combined_img,th,255,cv2.THRESH_BINARY)
    # Otsu result image
    out_img_o = thresh
    # morphological ops
    kernel = morphologicalOps.open_elliptical_kernel()
    out_img_o = morphologicalOps.opening(out_img_o, kernel)

    out_info_o = "_otsu_%d" % (th)
    out_str_o = out_info_o + '.png'
    out_file_o = re.sub(r'\.jpg', out_str_o, img_file)
    cv2.imwrite(out_file_o, out_img_o)
    t = evaluation.findTotals(out_img_o, man_img)
    f = open('o1_all.txt', 'a')
    f.write(img_file + " " + str(t[0]) + " " + str(t[1]) + " " + str(t[2]) + " " + str(t[3]) + "\n")
    f.close()