import cv2
import numpy as np


# https://zhuanlan.zhihu.com/p/84214563
def mask2polygon(mask):
    contours, hierarchy = cv2.findContours((mask).astype(np.uint8), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    # mask_new, contours, hierarchy = cv2.findContours((mask).astype(np.uint8), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    segmentation = []
    for contour in contours:
        contour_list = contour.flatten().tolist()
        if len(contour_list) > 4: # and cv2.contourArea(contour)>10000
            segmentation.append(contour_list)
    return segmentation


def convert_region_to_masks(region_label):
    min_val = int(np.min(region_label))
    max_val = int(np.max(region_label))
    masks = []
    for i in range(min_val, max_val+1):
        cur_mask = region_label == i
        if np.sum(cur_mask) > 0:
            masks.append(cur_mask.astype(np.float)[:, :, np.newaxis])

    mask_cat = np.concatenate(masks, axis=2)
    return mask_cat


def drop_bg(region_label):
    h, w = region_label.shape
    region_label = region_label.astype(np.int)
    for i in range(0, h//4):
        edge_val = region_label[i, 0]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0
        edge_val = region_label[i, w-1]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0

        edge_val = region_label[h-i-1, 0]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0
        edge_val = region_label[h-i-1, w-1]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0

    for j in range(1, w//4):
        edge_val = region_label[0, j]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0
        edge_val = region_label[h-1, j]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0

        edge_val = region_label[0, w-j-1]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0
        edge_val = region_label[h-1, w-j-1]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0

    region_label_uq = unique(region_label)
    # assert np.sum(region_label_uq) > 0
    return region_label_uq


def drop_small(region_label):
    h, w = region_label.shape
    region_label = region_label.astype(np.int)

    for i in range(1, np.max(region_label)+1):
        mask = region_label == i
        if np.sum(mask) < 0.1 * h * w:
            region_label[mask] = 0

    region_label_uq = unique(region_label)
    # assert np.sum(region_label_uq) > 0
    return region_label_uq

def unique(region_label):
    max_val = np.max(region_label)
    label = 1
    for i in range(1, max_val+1):
        mask = region_label == i
        if np.sum(mask) > 0:
            region_label[mask] = label
            label += 1
    return region_label
import cv2
import numpy as np


# https://zhuanlan.zhihu.com/p/84214563
def mask2polygon(mask):
    contours, hierarchy = cv2.findContours((mask).astype(np.uint8), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    # mask_new, contours, hierarchy = cv2.findContours((mask).astype(np.uint8), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    segmentation = []
    for contour in contours:
        contour_list = contour.flatten().tolist()
        if len(contour_list) > 4: # and cv2.contourArea(contour)>10000
            segmentation.append(contour_list)
    return segmentation


def convert_region_to_masks(region_label):
    min_val = int(np.min(region_label))
    max_val = int(np.max(region_label))
    masks = []
    for i in range(min_val, max_val+1):
        cur_mask = region_label == i
        if np.sum(cur_mask) > 0:
            masks.append(cur_mask.astype(np.float)[:, :, np.newaxis])

    mask_cat = np.concatenate(masks, axis=2)
    return mask_cat


def drop_bg(region_label):
    h, w = region_label.shape
    region_label = region_label.astype(np.int)
    for i in range(0, h//4):
        edge_val = region_label[i, 0]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0
        edge_val = region_label[i, w-1]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0

        edge_val = region_label[h-i-1, 0]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0
        edge_val = region_label[h-i-1, w-1]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0

    for j in range(1, w//4):
        edge_val = region_label[0, j]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0
        edge_val = region_label[h-1, j]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0

        edge_val = region_label[0, w-j-1]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0
        edge_val = region_label[h-1, w-j-1]
        if edge_val != 0:
            region_label[region_label == edge_val] = 0

    region_label_uq = unique(region_label)
    # assert np.sum(region_label_uq) > 0
    return region_label_uq


def drop_small(region_label):
    h, w = region_label.shape
    region_label = region_label.astype(np.int)

    for i in range(1, np.max(region_label)+1):
        mask = region_label == i
        if np.sum(mask) < 0.1 * h * w:
            region_label[mask] = 0

    region_label_uq = unique(region_label)
    # assert np.sum(region_label_uq) > 0
    return region_label_uq

def unique(region_label):
    max_val = np.max(region_label)
    label = 1
    for i in range(1, max_val+1):
        mask = region_label == i
        if np.sum(mask) > 0:
            region_label[mask] = label
            label += 1
    return region_label
# https://zhuanlan.zhihu.com/p/84214563 # mask_new, contours, hierarchy = cv2.findContours((mask).astype(np.uint8), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) # and cv2.contourArea(contour)>10000 # assert np.sum(region_label_uq) > 0 # assert np.sum(region_label_uq) > 0