Пример #1
0
def test_gray2rgba_dtype():
    img_f64 = np.random.random((5, 5))
    img_f32 = img_f64.astype('float32')
    img_u8 = img_as_ubyte(img_f64)
    img_int = img_u8.astype(int)

    for img in [img_f64, img_f32, img_u8, img_int]:
        assert gray2rgba(img).dtype == img.dtype
def test_gray2rgba_alpha():
    img = np.random.random((5, 5))
    img_u8 = img_as_ubyte(img)

    # Default
    alpha = None
    rgba = gray2rgba(img, alpha)

    assert_equal(rgba[..., :3], gray2rgb(img))
    assert_equal(rgba[..., 3], 1.0)

    # Scalar
    alpha = 0.5
    rgba = gray2rgba(img, alpha)

    assert_equal(rgba[..., :3], gray2rgb(img))
    assert_equal(rgba[..., 3], alpha)

    # Array
    alpha = np.random.random((5, 5))
    rgba = gray2rgba(img, alpha)

    assert_equal(rgba[..., :3], gray2rgb(img))
    assert_equal(rgba[..., 3], alpha)

    # Warning about alpha cast
    alpha = 0.5
    with expected_warnings(["alpha can't be safely cast to image dtype"]):
        rgba = gray2rgba(img_u8, alpha)
        assert_equal(rgba[..., :3], gray2rgb(img_u8))

    # Invalid shape
    alpha = np.random.random((5, 5, 1))
    expected_err_msg = ("could not broadcast input array from shape (5,5,1) "
                        "into shape (5,5)")

    with pytest.raises(ValueError) as err:
        rgba = gray2rgba(img, alpha)
    assert expected_err_msg == str(err.value)
Пример #3
0
def colorize(image, T):
    """Colorize image
    Parameters
    ----------
    image : (M, N) array
    Returns
    -------
    rescaled : rgba float array
        Color transformed image
    """
    # Convert to rgba
    rgba = color.gray2rgba(image, alpha=True)
    # Apply transform
    transformed = np.dot(rgba, T)
    rescaled = exposure.rescale_intensity(transformed)
    return rescaled
def test_gray2rgba(shape):
    # nD case
    img = np.random.random(shape)
    rgba = gray2rgba(img)

    # Shape check
    assert_equal(rgba.shape, shape + (4, ))

    # dtype check
    assert rgba.dtype == img.dtype

    # RGB channels check
    for channel in range(3):
        assert_equal(rgba[..., channel], img)

    # Alpha channel check
    assert_equal(rgba[..., 3], 1.0)
Пример #5
0
    def __init__(self, parent, imgPaths, fov_id_list,
                 image_dir):  #,frame_index,peak_id,fov_id):
        super(PhaseWidget, self).__init__(parent)

        self.image_dir = image_dir

        self.imgPaths = imgPaths
        self.fov_id_list = fov_id_list
        self.fovIndex = 0
        self.fov_id = self.fov_id_list[self.fovIndex]

        self.imgIndex = 0
        self.phaseImgPath = self.imgPaths[self.fov_id][self.imgIndex][0]
        self.phaseStack = io.imread(self.phaseImgPath)

        self.frameIndex = 0
        self.img = self.phaseStack[self.frameIndex, :, :]
        self.originalImgMax = np.max(self.img)
        # self.originalImgMax = np.max(self.phaseStack)
        originalRGBImg = color.gray2rgb(self.img)
        self.originalPhaseQImage = QImage(originalRGBImg,
                                          originalRGBImg.shape[1],
                                          originalRGBImg.shape[0],
                                          originalRGBImg.strides[0],
                                          QImage.Format_RGBA64)

        rescaledImg = self.img / self.originalImgMax * (2**16 - 1)
        RGBImg = color.gray2rgba(rescaledImg)
        self.originalHeight, self.originalWidth, self.originalChannelNumber = RGBImg.shape
        self.phaseQimage = QImage(
            RGBImg, RGBImg.shape[1], RGBImg.shape[0], RGBImg.strides[0],
            QImage.Format_RGBA64).scaled(1024,
                                         1024,
                                         aspectRatioMode=Qt.KeepAspectRatio)
        self.phaseQpixmap = QPixmap(self.phaseQimage)

        # rescaledImg = self.img/self.originalImgMax*255
        # RGBImg = color.gray2rgb(rescaledImg).astype('uint8')
        # self.originalHeight, self.originalWidth, self.originalChannelNumber = RGBImg.shape
        # self.phaseQimage = QImage(RGBImg, RGBImg.shape[1], RGBImg.shape[0], RGBImg.strides[0], QImage.Format_RGB888).scaled(1024, 1024, aspectRatioMode=Qt.KeepAspectRatio)
        # self.phaseQpixmap = QPixmap(self.phaseQimage)

        self.label = QLabel(self)
        self.label.setPixmap(self.phaseQpixmap)
Пример #6
0
def test_gray2rgba(shape, channel_axis):
    # nD case
    img = np.random.random(shape)
    rgba = gray2rgba(img, channel_axis=channel_axis)
    assert rgba.ndim == img.ndim + 1

    # Shape check
    new_axis_loc = channel_axis % rgba.ndim
    assert_equal(rgba.shape,
                 shape[:new_axis_loc] + (4, ) + shape[new_axis_loc:])

    # dtype check
    assert rgba.dtype == img.dtype

    # RGB channels check
    for channel in range(3):
        assert_equal(rgba[slice_at_axis(channel, axis=new_axis_loc)], img)

    # Alpha channel check
    assert_equal(rgba[slice_at_axis(3, axis=new_axis_loc)], 1.0)