Ejemplo n.º 1
0
    def image(self,
              fname,
              group=None,
              scaled=None,
              dtype=None,
              idxexp=None,
              zoom=None,
              gray=None):
        """Get named image.

        Parameters
        ----------
        fname : string
          Filename of image
        group : string or None, optional (default None)
          Name of image group
        scaled : bool or None, optional (default None)
          Flag indicating whether images should be on the range
          [0,...,255] with np.uint8 dtype (False), or on the range
          [0,...,1] with np.float32 dtype (True). If the value is None,
          scaling behaviour is determined by the `scaling` parameter
          passed to the object initializer, otherwise that selection is
          overridden.
        dtype : data-type or None, optional (default None)
          Desired data type of images. If `scaled` is True and `dtype`
          is an integer type, the output data type is np.float32. If the
          value is None, the data type is determined by the `dtype`
          parameter passed to the object initializer, otherwise that
          selection is overridden.
        idxexp :  index expression or None, optional (default None)
          An index expression selecting, for example, a cropped region
          of the requested image. This selection is applied *before* any
          `zoom` rescaling so the expression does not need to be
          modified when the zoom factor is changed.
        zoom : float or None, optional (default None)
          Optional rescaling factor to apply to the images. If the value
          is None, support rescaling behaviour is determined by the
          `zoom` parameter passed to the object initializer, otherwise
          that selection is overridden.
        gray : bool or None, optional (default None)
          Flag indicating whether RGB images should be converted to
          grayscale. If the value is None, behaviour is determined by
          the `gray` parameter passed to the object initializer.

        Returns
        -------
        img : ndarray
          Image array

        Raises
        ------
        IOError
          If the image is not accessible
        """

        if scaled is None:
            scaled = self.scaled
        if dtype is None:
            if self.dtype is None:
                dtype = np.uint8
            else:
                dtype = self.dtype
        if scaled and np.issubdtype(dtype, np.integer):
            dtype = np.float32
        if zoom is None:
            zoom = self.zoom
        if gray is None:
            gray = self.gray
        if group is None:
            pth = os.path.join(self.bpth, fname)
        else:
            pth = os.path.join(self.bpth, group, fname)

        try:
            img = imageio.imread(pth).astype(dtype)
        except IOError:
            raise IOError('Could not access image %s in group %s' %
                          (fname, group))

        if scaled:
            img /= 255.0
        if idxexp is not None:
            img = img[idxexp]
        if zoom is not None:
            if img.ndim == 2:
                img = sni.zoom(img, zoom)
            else:
                img = sni.zoom(img, (zoom, ) * 2 + (1, ) * (img.ndim - 2))
        if gray:
            img = signal.rgb2gray(img)

        return img
Ejemplo n.º 2
0
 def test_04(self):
     rgb = np.random.randn(64, 64, 3)
     gry = signal.rgb2gray(rgb)
     assert gry.shape == rgb.shape[0:2]
Ejemplo n.º 3
0
import numpy as np
import imageio

from sporco.admm import rpca
from sporco import signal
from sporco import plot
"""
Load example video.
"""

reader = imageio.get_reader('imageio:newtonscradle.gif')
nfrm = reader.get_length()
frmlst = []
for i, frm in enumerate(reader):
    frmlst.append(signal.rgb2gray(frm[..., 0:3].astype(np.float32) / 255.0))
v = np.stack(frmlst, axis=2)
"""
Construct matrix with each column consisting of a vectorised video frame.
"""

S = v.reshape((-1, v.shape[-1]))
"""
Set options for the Robust PCA solver, create the solver object, and solve, returning the estimates of the low rank and sparse components ``X`` and ``Y``. Unlike most other SPORCO classes for optimisation problems, :class:`.rpca.RobustPCA` has a meaningful default regularization parameter, as used here.
"""

opt = rpca.RobustPCA.Options({
    'Verbose': True,
    'gEvalY': False,
    'MaxMainIter': 200,
    'RelStopTol': 1e-3,
Ejemplo n.º 4
0
             x,
             y,
             xlbl='x',
             ylbl='y',
             title='Contour Plot Example',
             fig=fig,
             ax=ax[1])
fig.show()
"""
Load an example colour image and create a corresponding grayscale version.
"""

imgc = util.ExampleImages().image('kodim23.png',
                                  scaled=True,
                                  idxexp=np.s_[150:500, 30:380])
imgg = signal.rgb2gray(imgc)
"""
Display the example colour image.
"""

plot.imview(imgc, title='Image View Example', fgsz=(6, 6))
"""
Display the grayscale image with a false-colour colour map, with a
colour bar display of the color map.
"""

plot.imview(imgg,
            cmap=plot.cm.coolwarm,
            title='Image View Example',
            cbar=True,
            fgsz=(7, 6))
Ejemplo n.º 5
0
from scipy.ndimage import zoom
import imageio

from sporco.dictlrn import cbpdndl
from sporco import util
from sporco import signal
from sporco import plot
"""
Construct 3D training array from video data
"""

reader = imageio.get_reader('imageio:cockatoo.mp4')
frmlst = []
for i, frm in enumerate(reader):
    if i >= 250:
        frm = zoom(signal.rgb2gray(frm.astype(np.float32) / 255.0), 0.25)
        frmlst.append(frm[20:-20, 70:-70])
vid = np.stack(frmlst, axis=2)
"""
Highpass filter video frames.
"""

npd = 16
fltlmbd = 10
vl, vh = signal.tikhonov_filter(vid, fltlmbd, npd)
"""
Construct initial dictionary.
"""

np.random.seed(12345)
D0 = np.random.randn(5, 5, 3, 25)