Ejemplo n.º 1
0
def get_dH2(lab1, lab2):
    """squared hue difference term occurring in deltaE_cmc and deltaE_ciede94

    Despite its name, "dH" is not a simple difference of hue values.  We avoid
    working directly with the hue value, since differencing angles is
    troublesome.  The hue term is usually written as:
        c1 = sqrt(a1**2 + b1**2)
        c2 = sqrt(a2**2 + b2**2)
        term = (a1-a2)**2 + (b1-b2)**2 - (c1-c2)**2
        dH = sqrt(term)

    However, this has poor roundoff properties when a or b is dominant.
    Instead, ab is a vector with elements a and b.  The same dH term can be
    re-written as:
        |ab1-ab2|**2 - (|ab1| - |ab2|)**2
    and then simplified to:
        2*|ab1|*|ab2| - 2*dot(ab1, ab2)
    """
    a1, b1 = cp.rollaxis(lab1, -1)[1:3]
    a2, b2 = cp.rollaxis(lab2, -1)[1:3]

    # magnitude of (a, b) is the chroma
    C1 = cp.hypot(a1, b1)
    C2 = cp.hypot(a2, b2)

    term = (C1 * C2) - (a1 * a2 + b1 * b2)
    return 2 * term
Ejemplo n.º 2
0
def _distance_matrix2d(xy, xyi):
    """
    Description
    -----------
    (2D only)
    return element-wise distance matrix (pair-wise)
    """
    # Make a distance matrix between pairwise observations
    # Note: from <http://stackoverflow.com/questions/1871536>
    # (Yay for ufuncs!)
    '''d01 = np.subtract.outer(xy[:, 0], xyi[:, 0])  # size(xy) * size(xyi)
    d11 = np.subtract.outer(xy[:, 1], xyi[:, 1])'''

    d0 = xy[:, 0].ravel()[:, cp.newaxis] - xyi[:, 0].ravel()[cp.newaxis, :]
    d1 = xy[:, 1].ravel()[:, cp.newaxis] - xyi[:, 1].ravel()[cp.newaxis, :]
    # hypot : element-wise sqrt(d0**2 + d1**2)
    return cp.hypot(d0, d1)
Ejemplo n.º 3
0
 def match_score(image, frequency):
     gabor_responses = gabor(image, frequency)
     return cp.mean(cp.hypot(*gabor_responses)).get()
Ejemplo n.º 4
0
def canny(image,
          sigma=1.,
          low_threshold=None,
          high_threshold=None,
          mask=None,
          use_quantiles=False):
    """Edge filter an image using the Canny algorithm.

    Parameters
    -----------
    image : 2D array
        Grayscale input image to detect edges on; can be of any dtype.
    sigma : float, optional
        Standard deviation of the Gaussian filter.
    low_threshold : float, optional
        Lower bound for hysteresis thresholding (linking edges).
        If None, low_threshold is set to 10% of dtype's max.
    high_threshold : float, optional
        Upper bound for hysteresis thresholding (linking edges).
        If None, high_threshold is set to 20% of dtype's max.
    mask : array, dtype=bool, optional
        Mask to limit the application of Canny to a certain area.
    use_quantiles : bool, optional
        If True then treat low_threshold and high_threshold as quantiles of the
        edge magnitude image, rather than absolute edge magnitude values. If
        True, then the thresholds must be in the range [0, 1].

    Returns
    -------
    output : 2D array (image)
        The binary edge map.

    See also
    --------
    skimage.sobel

    Notes
    -----
    The steps of the algorithm are as follows:

    * Smooth the image using a Gaussian with ``sigma`` width.

    * Apply the horizontal and vertical Sobel operators to get the gradients
      within the image. The edge strength is the norm of the gradient.

    * Thin potential edges to 1-pixel wide curves. First, find the normal
      to the edge at each point. This is done by looking at the
      signs and the relative magnitude of the X-Sobel and Y-Sobel
      to sort the points into 4 categories: horizontal, vertical,
      diagonal and antidiagonal. Then look in the normal and reverse
      directions to see if the values in either of those directions are
      greater than the point in question. Use interpolation to get a mix of
      points instead of picking the one that's the closest to the normal.

    * Perform a hysteresis thresholding: first label all points above the
      high threshold as edges. Then recursively label any point above the
      low threshold that is 8-connected to a labeled point as an edge.

    References
    -----------
    .. [1] Canny, J., A Computational Approach To Edge Detection, IEEE Trans.
           Pattern Analysis and Machine Intelligence, 8:679-714, 1986
           :DOI:`10.1109/TPAMI.1986.4767851`
    .. [2] William Green's Canny tutorial
           https://en.wikipedia.org/wiki/Canny_edge_detector

    Examples
    --------
    >>> import cupy as cp
    >>> from cucim.skimage import feature
    >>> # Generate noisy image of a square
    >>> im = cp.zeros((256, 256))
    >>> im[64:-64, 64:-64] = 1
    >>> im += 0.2 * cp.random.rand(*im.shape)
    >>> # First trial with the Canny filter, with the default smoothing
    >>> edges1 = feature.canny(im)
    >>> # Increase the smoothing for better results
    >>> edges2 = feature.canny(im, sigma=3)
    """

    #
    # The steps involved:
    #
    # * Smooth using the Gaussian with sigma above.
    #
    # * Apply the horizontal and vertical Sobel operators to get the gradients
    #   within the image. The edge strength is the sum of the magnitudes
    #   of the gradients in each direction.
    #
    # * Find the normal to the edge at each point using the arctangent of the
    #   ratio of the Y sobel over the X sobel - pragmatically, we can
    #   look at the signs of X and Y and the relative magnitude of X vs Y
    #   to sort the points into 4 categories: horizontal, vertical,
    #   diagonal and antidiagonal.
    #
    # * Look in the normal and reverse directions to see if the values
    #   in either of those directions are greater than the point in question.
    #   Use interpolation to get a mix of points instead of picking the one
    #   that's the closest to the normal.
    #
    # * Label all points above the high threshold as edges.
    # * Recursively label any point above the low threshold that is 8-connected
    #   to a labeled point as an edge.
    #
    # Regarding masks, any point touching a masked point will have a gradient
    # that is "infected" by the masked point, so it's enough to erode the
    # mask by one and then mask the output. We also mask out the border points
    # because who knows what lies beyond the edge of the image?
    #
    check_nD(image, 2)
    dtype_max = dtype_limits(image, clip_negative=False)[1]

    if low_threshold is None:
        low_threshold = 0.1
    elif use_quantiles:
        if not (0.0 <= low_threshold <= 1.0):
            raise ValueError("Quantile thresholds must be between 0 and 1.")
    else:
        low_threshold = low_threshold / dtype_max

    if high_threshold is None:
        high_threshold = 0.2
    elif use_quantiles:
        if not (0.0 <= high_threshold <= 1.0):
            raise ValueError("Quantile thresholds must be between 0 and 1.")
    else:
        high_threshold = high_threshold / dtype_max

    _gaussian = functools.partial(gaussian, sigma=sigma)

    def fsmooth(x, mode='constant'):
        return img_as_float(_gaussian(x, mode=mode))

    if mask is None:
        smoothed = fsmooth(image, mode='reflect')
        # mask that is ones everywhere except the borders
        eroded_mask = cp.ones(image.shape, dtype=bool)
        eroded_mask[:1, :] = 0
        eroded_mask[-1:, :] = 0
        eroded_mask[:, :1] = 0
        eroded_mask[:, -1:] = 0
    else:
        smoothed = smooth_with_function_and_mask(image, fsmooth, mask)
        #
        # Make the eroded mask. Setting the border value to zero will wipe
        # out the image edges for us.
        #
        s = generate_binary_structure(2, 2)
        eroded_mask = binary_erosion(mask, s, border_value=0)

    jsobel = ndi.sobel(smoothed, axis=1)
    isobel = ndi.sobel(smoothed, axis=0)
    abs_isobel = cp.abs(isobel)
    abs_jsobel = cp.abs(jsobel)
    magnitude = cp.hypot(isobel, jsobel)
    eroded_mask = eroded_mask & (magnitude > 0)
    # TODO: implement custom kernel to compute local maxima

    #
    # --------- Find local maxima --------------
    #
    # Assign each point to have a normal of 0-45 degrees, 45-90 degrees,
    # 90-135 degrees and 135-180 degrees.
    #
    local_maxima = cp.zeros(image.shape, bool)

    isobel_gt_0 = isobel >= 0
    jsobel_gt_0 = jsobel >= 0
    isobel_lt_0 = isobel <= 0
    jsobel_lt_0 = jsobel <= 0
    abs_isobel_lt_jsobel = abs_isobel <= abs_jsobel
    abs_isobel_gt_jsobel = abs_isobel >= abs_jsobel

    # ----- 0 to 45 degrees ------
    pts_plus = isobel_gt_0 & jsobel_gt_0
    pts_minus = isobel_lt_0 & jsobel_lt_0
    pts_tmp = (pts_plus | pts_minus) & eroded_mask
    pts = pts_tmp & abs_isobel_gt_jsobel
    # Get the magnitudes shifted left to make a matrix of the points to the
    # right of pts. Similarly, shift left and down to get the points to the
    # top right of pts.

    c1 = magnitude[1:, :][pts[:-1, :]]
    c2 = magnitude[1:, 1:][pts[:-1, :-1]]
    m = magnitude[pts]
    w = abs_jsobel[pts] / abs_isobel[pts]
    c_plus = _fused_comparison(w, c1, c2, m)
    c1 = magnitude[:-1, :][pts[1:, :]]
    c2 = magnitude[:-1, :-1][pts[1:, 1:]]
    c_minus = _fused_comparison(w, c1, c2, m)
    local_maxima[pts] = c_plus & c_minus
    # ----- 45 to 90 degrees ------
    # Mix diagonal and vertical
    #
    pts = pts_tmp & abs_isobel_lt_jsobel
    c1 = magnitude[:, 1:][pts[:, :-1]]
    c2 = magnitude[1:, 1:][pts[:-1, :-1]]
    m = magnitude[pts]
    w = abs_isobel[pts] / abs_jsobel[pts]
    c_plus = _fused_comparison(w, c1, c2, m)
    c1 = magnitude[:, :-1][pts[:, 1:]]
    c2 = magnitude[:-1, :-1][pts[1:, 1:]]
    c_minus = _fused_comparison(w, c1, c2, m)
    local_maxima[pts] = c_plus & c_minus
    # ----- 90 to 135 degrees ------
    # Mix anti-diagonal and vertical
    #
    pts_plus = isobel_lt_0 & jsobel_gt_0
    pts_minus = isobel_gt_0 & jsobel_lt_0
    pts_tmp = (pts_plus | pts_minus) & eroded_mask
    pts = pts_tmp & abs_isobel_lt_jsobel
    c1a = magnitude[:, 1:][pts[:, :-1]]
    c2a = magnitude[:-1, 1:][pts[1:, :-1]]
    m = magnitude[pts]
    w = abs_isobel[pts] / abs_jsobel[pts]
    c_plus = _fused_comparison(w, c1a, c2a, m)
    c1 = magnitude[:, :-1][pts[:, 1:]]
    c2 = magnitude[1:, :-1][pts[:-1, 1:]]
    c_minus = _fused_comparison(w, c1, c2, m)
    local_maxima[pts] = c_plus & c_minus
    # ----- 135 to 180 degrees ------
    # Mix anti-diagonal and anti-horizontal
    #
    pts = pts_tmp & abs_isobel_gt_jsobel
    c1 = magnitude[:-1, :][pts[1:, :]]
    c2 = magnitude[:-1, 1:][pts[1:, :-1]]
    m = magnitude[pts]
    w = abs_jsobel[pts] / abs_isobel[pts]
    c_plus = _fused_comparison(w, c1, c2, m)
    c1 = magnitude[1:, :][pts[:-1, :]]
    c2 = magnitude[1:, :-1][pts[:-1, 1:]]
    c_minus = _fused_comparison(w, c1, c2, m)
    local_maxima[pts] = c_plus & c_minus

    #
    # ---- If use_quantiles is set then calculate the thresholds to use
    #
    if use_quantiles:
        high_threshold = cp.percentile(magnitude, 100.0 * high_threshold)
        low_threshold = cp.percentile(magnitude, 100.0 * low_threshold)

    #
    # ---- Create two masks at the two thresholds.
    #
    high_mask = local_maxima & (magnitude >= high_threshold)
    low_mask = local_maxima & (magnitude >= low_threshold)

    #
    # Segment the low-mask, then only keep low-segments that have
    # some high_mask component in them
    #
    labels, count = ndi.label(low_mask, structure=cp.ones((3, 3), bool))
    if count == 0:
        return low_mask

    nonzero_sums = cp.unique(labels[high_mask])
    good_label = cp.zeros((count + 1, ), bool)
    good_label[nonzero_sums] = True
    output_mask = good_label[labels]
    return output_mask
Ejemplo n.º 5
0
t_width=(s1[1:]-s1[:-1])/t_step[:]
sim_k=int(t_step.sum())
sim_k=sim_k+1
#print(sim_k)
mac_ang,mac_spd,dmac_ang,dmac_spd,pelect=cp.zeros((5,sim_k,ngen),dtype=cp.float64)
eprime=cp.zeros((sim_k,ngen),dtype=cp.complex128)

theta=cp.radians(b_ang)
bus_volt=V*cp.exp(jay*theta)
mva=basmva/g_m
tst1=bus_int[g_bus-1].astype(cp.int)
eterm=V[tst1-1] # terminal bus voltage
pelect[0]=b_pg[tst1-1]     # BUS_pg
qelect=b_qg[tst1-1]     # BUS_qg
#compute the initial values for generator dynamics
curr=cp.hypot(pelect[0],qelect)/(eterm*mva)
phi=cp.arctan2(qelect,pelect[0])
v=eterm*cp.exp(jay*theta[tst1-1])
curr=curr*cp.exp(jay*(theta[tst1-1]-phi))
eprime[0]=v+jay*g_dtr*curr
mac_ang[0]=cp.arctan2(eprime[0].imag,eprime[0].real)
mac_spd[0]=1.0
rot=jay*cp.exp(-jay*mac_ang[0])
eprime[0]=eprime[0]*rot
edprime=cp.copy(eprime[0].real)
eqprime=cp.copy(eprime[0].imag)
pmech=cp.copy(pelect[0]*mva)

steps3=int(t_step.sum())
steps2=int(t_step[:2].sum())
steps1=int(t_step[0])
Ejemplo n.º 6
0
 def match_score(image, frequency):
     gabor_responses = gabor(image, frequency)
     assert all(r.dtype == dtype for r in gabor_responses)
     return float(cp.mean(cp.hypot(*gabor_responses)))
Ejemplo n.º 7
0
def deltaE_ciede2000(lab1, lab2, kL=1, kC=1, kH=1):
    """Color difference as given by the CIEDE 2000 standard.

    CIEDE 2000 is a major revision of CIDE94.  The perceptual calibration is
    largely based on experience with automotive paint on smooth surfaces.

    Parameters
    ----------
    lab1 : array_like
        reference color (Lab colorspace)
    lab2 : array_like
        comparison color (Lab colorspace)
    kL : float (range), optional
        lightness scale factor, 1 for "acceptably close"; 2 for "imperceptible"
        see deltaE_cmc
    kC : float (range), optional
        chroma scale factor, usually 1
    kH : float (range), optional
        hue scale factor, usually 1

    Returns
    -------
    deltaE : array_like
        The distance between `lab1` and `lab2`

    Notes
    -----
    CIEDE 2000 assumes parametric weighting factors for the lightness, chroma,
    and hue (`kL`, `kC`, `kH` respectively).  These default to 1.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Color_difference
    .. [2] http://www.ece.rochester.edu/~gsharma/ciede2000/ciede2000noteCRNA.pdf
           :DOI:`10.1364/AO.33.008069`
    .. [3] M. Melgosa, J. Quesada, and E. Hita, "Uniformity of some recent
           color metrics tested with an accurate color-difference tolerance
           dataset," Appl. Opt. 33, 8069-8077 (1994).
    """
    warnings.warn(
        "The numerical accuracy of this function on the GPU is reduced "
        "relative to the CPU version"
    )
    unroll = False
    if lab1.ndim == 1 and lab2.ndim == 1:
        unroll = True
        if lab1.ndim == 1:
            lab1 = lab1[None, :]
        if lab2.ndim == 1:
            lab2 = lab2[None, :]
    L1, a1, b1 = cp.rollaxis(lab1, -1)[:3]
    L2, a2, b2 = cp.rollaxis(lab2, -1)[:3]

    # distort `a` based on average chroma
    # then convert to lch coordines from distorted `a`
    # all subsequence calculations are in the new coordiantes
    # (often denoted "prime" in the literature)
    Cbar = 0.5 * (cp.hypot(a1, b1) + cp.hypot(a2, b2))
    c7 = Cbar ** 7
    G = 0.5 * (1 - cp.sqrt(c7 / (c7 + 25 ** 7)))
    scale = 1 + G
    C1, h1 = _cart2polar_2pi(a1 * scale, b1)
    C2, h2 = _cart2polar_2pi(a2 * scale, b2)
    # recall that c, h are polar coordiantes.  c==r, h==theta

    # cide2000 has four terms to delta_e:
    # 1) Luminance term
    # 2) Hue term
    # 3) Chroma term
    # 4) hue Rotation term

    # lightness term
    Lbar = 0.5 * (L1 + L2)
    tmp = Lbar - 50
    tmp *= tmp
    SL = 1 + 0.015 * tmp / cp.sqrt(20 + tmp)
    L_term = (L2 - L1) / (kL * SL)

    # chroma term
    Cbar = 0.5 * (C1 + C2)  # new coordiantes
    SC = 1 + 0.045 * Cbar
    C_term = (C2 - C1) / (kC * SC)

    # hue term
    h_diff = h2 - h1
    h_sum = h1 + h2
    CC = C1 * C2

    dH = h_diff.copy()
    dH[h_diff > np.pi] -= 2 * np.pi
    dH[h_diff < -np.pi] += 2 * np.pi
    dH[CC == 0.] = 0.  # if r == 0, dtheta == 0
    dH_term = 2 * cp.sqrt(CC) * cp.sin(dH / 2)

    Hbar = h_sum.copy()
    mask = cp.logical_and(CC != 0., cp.abs(h_diff) > np.pi)
    Hbar[mask * (h_sum < 2 * np.pi)] += 2 * np.pi
    Hbar[mask * (h_sum >= 2 * np.pi)] -= 2 * np.pi
    Hbar[CC == 0.] *= 2
    Hbar *= 0.5

    T = (1 -
         0.17 * cp.cos(Hbar - np.deg2rad(30)) +
         0.24 * cp.cos(2 * Hbar) +
         0.32 * cp.cos(3 * Hbar + np.deg2rad(6)) -
         0.20 * cp.cos(4 * Hbar - np.deg2rad(63))
         )
    SH = 1 + 0.015 * Cbar * T

    H_term = dH_term / (kH * SH)

    # hue rotation
    c7 = Cbar ** 7
    Rc = 2 * cp.sqrt(c7 / (c7 + 25 ** 7))
    tmp = (cp.rad2deg(Hbar) - 275) / 25
    tmp *= tmp
    dtheta = np.deg2rad(30) * cp.exp(-tmp)
    R_term = -cp.sin(2 * dtheta) * Rc * C_term * H_term

    # put it all together
    dE2 = L_term * L_term
    dE2 += C_term * C_term
    dE2 += H_term * H_term
    dE2 += R_term
    cp.sqrt(cp.maximum(dE2, 0, out=dE2), out=dE2)
    if unroll:
        dE2 = dE2[0]
    return dE2
Ejemplo n.º 8
0
 def filt_func(self, r, c):
     return cp.exp(-cp.hypot(r, c) / 1)