#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2022/5/26 10:16:16

@author: LiuKuan
@copyright: Apache License, Version 2.0
'''
from laok.third_lib import cv2
#===============================================================================
'''    
cv2.MORPH_RECT = 0
cv2.BORDER_CONSTANT = 0         
'''
#===============================================================================
__all__ = ['morph_close_cv', 'morph_open_cv', 'morph_blackhat_cv',
           'morph_dilate_cv', 'morph_erode_cv', 'morph_gradient_cv',
           'morph_hitmiss_cv', 'morph_tophat_cv']

def _morph_impl(img, op, ksize=(3, 3), shape=0, kernel=None, anchor=(-1, -1), iterations=1, borderType=0, borderValue=(0, 0, 0)):
    if kernel is None:
        kernel = cv2.getStructuringElement(shape=shape, ksize=ksize, anchor=anchor)
    return cv2.morphologyEx(img, op=op, kernel=kernel, anchor=anchor, iterations=iterations, borderType=borderType, borderValue=borderValue)

def morph_close_cv(img, ksize=(3, 3), shape=0, kernel=None, anchor=(-1, -1), iterations=1, borderType=0, borderValue=(0, 0, 0)):
    return _morph_impl(img, op=cv2.MORPH_CLOSE, ksize=ksize, shape=shape, kernel=kernel, anchor=anchor, iterations=iterations, borderType=borderType, borderValue=borderValue)

def morph_open_cv(img, ksize=(3, 3), shape=0, kernel=None,anchor=(-1, -1), iterations=1, borderType=0, borderValue=(0, 0, 0)):
    return _morph_impl(img, op=cv2.MORPH_OPEN, ksize=ksize, shape=shape, kernel=kernel, anchor=anchor, iterations=iterations, borderType=borderType, borderValue=borderValue)

def morph_dilate_cv(img, ksize=(3, 3), shape=0, kernel=None, anchor=(-1, -1), iterations=1, borderType=0, borderValue=(0, 0, 0)):
    return _morph_impl(img, op=cv2.MORPH_DILATE, ksize=ksize, shape=shape, kernel=kernel, anchor=anchor, iterations=iterations, borderType=borderType, borderValue=borderValue)

def morph_erode_cv(img, ksize=(3, 3), shape=0, kernel=None, anchor=(-1, -1), iterations=1, borderType=0, borderValue=(0, 0, 0)):
    return _morph_impl(img, op=cv2.MORPH_ERODE, ksize=ksize, shape=shape, kernel=kernel, anchor=anchor, iterations=iterations, borderType=borderType, borderValue=borderValue)

def morph_gradient_cv(img, ksize=(3, 3), shape=0, kernel=None, anchor=(-1, -1), iterations=1, borderType=0, borderValue=(0, 0, 0)):
    return _morph_impl(img, op=cv2.MORPH_GRADIENT, ksize=ksize, shape=shape, kernel=kernel, anchor=anchor, iterations=iterations, borderType=borderType, borderValue=borderValue)

def morph_tophat_cv(img, ksize=(3, 3), shape=0, kernel=None, anchor=(-1, -1), iterations=1, borderType=0, borderValue=(0, 0, 0)):
    return _morph_impl(img, op=cv2.MORPH_TOPHAT, ksize=ksize, shape=shape, kernel=kernel, anchor=anchor, iterations=iterations, borderType=borderType, borderValue=borderValue)

def morph_blackhat_cv(img, ksize=(3, 3), shape=0, kernel=None, anchor=(-1, -1), iterations=1, borderType=0, borderValue=(0, 0, 0)):
    return _morph_impl(img, op=cv2.MORPH_BLACKHAT, ksize=ksize, shape=shape, kernel=kernel, anchor=anchor, iterations=iterations, borderType=borderType, borderValue=borderValue)

def morph_hitmiss_cv(img, ksize=(3, 3), shape=0, kernel=None, anchor=(-1, -1), iterations=1, borderType=0, borderValue=(0, 0, 0)):
    return _morph_impl(img, op=cv2.MORPH_HITMISS, ksize=ksize, shape=shape, kernel=kernel, anchor=anchor, iterations=iterations, borderType=borderType, borderValue=borderValue)

