def scalar_2_rgba(img, color='#ff0000'):
    """
    generate display a image in (RGBA).(np.uint8) format which can be displayed by imshow
    alpha is defined by values in the img
    :param img: input image
    :param alphaMatrix: matrix of alpha
    :param foreGroundColor: color for 1 in the array, RGB str, i.e. '#ff0000'
    :return: displayImg, (RGBA).(np.uint8) format, ready for imshow
    """

    R, G, B = get_rgb(color)

    RMatrix = (R * ia.array_nor(img.astype(np.float32))).astype(np.uint8)
    GMatrix = (G * ia.array_nor(img.astype(np.float32))).astype(np.uint8)
    BMatrix = (B * ia.array_nor(img.astype(np.float32))).astype(np.uint8)

    alphaMatrix = (ia.array_nor(img.astype(np.float32)) * 255).astype(np.uint8)

    displayImg = np.zeros((img.shape[0], img.shape[1], 4)).astype(np.uint8)
    displayImg[:, :, 0] = RMatrix
    displayImg[:, :, 1] = GMatrix
    displayImg[:, :, 2] = BMatrix
    displayImg[:, :, 3] = alphaMatrix

    return displayImg
Пример #2
0
def generateAVI(saveFolder,
                fileName,
                matrix,
                frameRate=25.,
                encoder='XVID',
                zoom=1,
                isDisplay=True):
    """
    :param saveFolder:
    :param fileName: can be with '.avi' or without '.avi'
    :param matrix: can be 3 dimensional (gray value) or 4 dimensional
                   if the length of the 4th dimension equals 3, it will be considered as rgb
                   if the length of the 4th dimension equals 4, it will be considered as rgba
    :param frameRate:
    :param encoder:
    :param zoom:
    :return: generate the .avi movie file
    """

    if len(matrix.shape) == 4:
        if matrix.shape[3] == 3:
            r, g, b = np.rollaxis(matrix, axis=-1)
        elif matrix.shape[3] == 4:
            r, g, b, a = np.rollaxis(matrix, axis=-1)
        else:
            raise IndexError(
                'The depth of matrix is not 3 or 4. Can not get RGB color!')
        r = r.reshape(r.shape[0], r.shape[1], r.shape[2], 1)
        g = g.reshape(g.shape[0], g.shape[1], g.shape[2], 1)
        b = b.reshape(b.shape[0], b.shape[1], b.shape[2], 1)
        newMatix = np.concatenate((r, g, b), axis=3)
        newMatrix = (ia.array_nor(newMatix) * 255).astype(np.uint8)
    elif len(matrix.shape) == 3:
        s = (ia.array_nor(matrix) * 255).astype(np.uint8)
        s = s.reshape(s.shape[0], s.shape[1], s.shape[2], 1)
        newMatrix = np.concatenate((s, s, s), axis=3)
    else:
        raise IndexError(
            'The matrix dimension is neither 3 or 4. Can not get RGB color!')

    fourcc = cv2.cv.CV_FOURCC(*encoder)

    if fileName[-4:] != '.avi':
        fileName += '.avi'

    size = (int(newMatrix.shape[1] * zoom), int(newMatrix.shape[2] * zoom))

    filePath = os.path.join(saveFolder, fileName + '.avi')
    out = cv2.VideoWriter(filePath, fourcc, frameRate, size)

    for i in range(newMatrix.shape[0]):
        out.write(newMatrix[i, :, :, :])
        if isDisplay:
            cv2.imshow('movie', newMatrix[i, :, :, :])

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    out.release()
    cv2.destroyAllWindows()
def merge_normalized_images(imgList,
                            isFilter=True,
                            sigma=50,
                            mergeMethod='mean',
                            dtype=np.float32):
    """
    merge images in a list in to one, for each image, local intensity variability will be removed by subtraction of
    gaussian filtered image. Then all images will be collapsed by the mergeMethod in to single image
    """

    imgList2 = []

    for currImg in imgList:
        imgList2.append(ia.array_nor(currImg.astype(dtype)))

    if mergeMethod == 'mean':
        mergedImg = np.mean(np.array(imgList2), axis=0)
    elif mergeMethod == 'min':
        mergedImg = np.min(np.array(imgList2), axis=0)
    elif mergeMethod == 'max':
        mergedImg = np.max(np.array(imgList2), axis=0)
    elif mergeMethod == 'median':
        mergedImg = np.median(np.array(imgList2), axis=0)

    if isFilter:
        mergedImgf = ni.filters.gaussian_filter(mergedImg.astype(np.float),
                                                sigma=sigma)
        return ia.array_nor(mergedImg - mergedImgf).astype(dtype)
    else:
        return ia.array_nor(mergedImg).astype(dtype)