Example #1
0
def get_image_quality(
    patterns: Union[np.ndarray, da.Array],
    frequency_vectors: Optional[np.ndarray] = None,
    inertia_max: Union[None, int, float] = None,
    normalize: bool = True,
) -> np.ndarray:
    """Compute the image quality in a chunk of EBSD patterns.

    The image quality is calculated based on the procedure defined by
    Krieger Lassen [Lassen1994]_.

    Parameters
    ----------
    patterns
        EBSD patterns.
    frequency_vectors
        Integer 2D array with values corresponding to the weight given
        each FFT spectrum frequency component. If None (default), these
        are calculated from
        :func:`~kikuchipy.util.pattern.fft_frequency_vectors`.
    inertia_max
        Maximum inertia of the FFT power spectrum of the image. If None
        (default), this is calculated from the `frequency_vectors`.
    normalize
        Whether to normalize patterns to a mean of zero and standard
        deviation of 1 before calculating the image quality. Default
        is True.

    Returns
    -------
    image_quality_chunk : numpy.ndarray
        Image quality of patterns.
    """
    dtype_out = np.float64

    image_quality_chunk = np.empty(patterns.shape[:-2], dtype=dtype_out)

    for nav_idx in np.ndindex(patterns.shape[:-2]):
        # Get (normalized) pattern
        if normalize:
            pattern = pattern_processing.normalize_intensity(
                pattern=patterns[nav_idx])
        else:
            pattern = patterns[nav_idx]

        # Compute image quality
        image_quality_chunk[nav_idx] = pattern_processing.get_image_quality(
            pattern=pattern,
            normalize=False,
            frequency_vectors=frequency_vectors,
            inertia_max=inertia_max,
        )

    return image_quality_chunk
Example #2
0
def normalize_intensity(
    patterns: Union[np.ndarray, da.Array],
    num_std: int = 1,
    divide_by_square_root: bool = False,
    dtype_out: Optional[np.dtype] = None,
) -> np.ndarray:
    """Normalize intensities in a chunk of EBSD patterns to a mean of
    zero with a given standard deviation.

    Parameters
    ----------
    patterns
        Patterns to normalize the intensity in.
    num_std
        Number of standard deviations of the output intensities. Default
        is 1.
    divide_by_square_root
        Whether to divide output intensities by the square root of the
        pattern size. Default is False.
    dtype_out
        Data type of normalized patterns. If None (default), the input
        patterns' data type is used.

    Returns
    -------
    normalized_patterns : numpy.ndarray
        Normalized patterns.

    Notes
    -----
    Data type should always be changed to floating point, e.g.
    ``np.float32`` with :meth:`numpy.ndarray.astype`, before normalizing
    the intensities.
    """
    if dtype_out is None:
        dtype_out = patterns.dtype.type

    normalized_patterns = np.empty_like(patterns, dtype=dtype_out)

    for nav_idx in np.ndindex(patterns.shape[:-2]):
        normalized_patterns[nav_idx] = pattern_processing.normalize_intensity(
            pattern=patterns[nav_idx],
            num_std=num_std,
            divide_by_square_root=divide_by_square_root,
        )

    return normalized_patterns