Exemple #1
0
def imgLoad(imgPath, color=True):
    """
    Load an image converting from grayscale or alpha as needed.

    Input
      imgPath  -  image path
      color    -  flag for color format. True (default) loads as RGB while False
                  loads as intensity (if image is already grayscale).

    Output
      image    -  an image with type np.float32 in range [0, 1]
                    of size (H x W x 3) in RGB or
                    of size (H x W x 1) in grayscale.
    """
    # load
    try:
        img0 = skimage.io.imread(imgPath)
        img = skimage.img_as_float(img0).astype(np.float32)

    except:
        pr('unable to open img: {}'.format(imgPath))
        return None

    # color channel
    if img.ndim == 2:
        img = img[:, :, np.newaxis]
        if color:
            img = np.tile(img, (1, 1, 3))

    elif img.shape[2] == 4:
        img = img[:, :, :3]

    return img
Exemple #2
0
def equal(nm, A, B):
    """
    Check whether two matrix are equal or not.

    Input
      nm    -  name
      A     -  matrix 1
      B     -  matrix 2

    Output
      isEq  -  result, True | False
    """
    # check type
    if not A.__class__ == B.__class__:
        pri.pr('{}, different types: {} vs {}'.format(nm, A.__class__, B.__class__))
        return False

    # dictionary, check each key
    if A.__class__ == dict:
        keyAs = A.keys()
        keyBs = A.keys()
        keyAs.sort()
        keyBs.sort()
        if not equal(nm, keyAs, keyBs):
            return False
        for key in keyAs:
            if not equal(nm, A[key], B[key]):
                return False
        return True

    # list
    if A.__class__ == list:
        if not len(A) == len(B):
            pri.pr('{}, different lens: {} vs {}'.format(nm, len(A), len(B)))
            return False

        for i in range(len(A)):
            if not equal(nm, A[i], B[i]):
                return False
        return True

    # string
    if A.__class__ == str:
        if not A == B:
            pri.pr('{}, different strings: {} vs {}'.format(nm, A, B))
            return False
        return True

    # check dimension
    if not A.shape == B.shape:
        pri.pr('{}, different size: {} vs {}'.format(nm, A.shape, B.shape))
        return False

    # check value
    diff = max(abs((A - B).flatten()))
    isEq = diff < 1e-5
    if isEq:
        pri.pr('{}, same'.format(nm))
    else:
        pri.pr('{}, different values: diff = {}'.format(nm, diff))

    return isEq