def test_orientation():
    """counter-clockwise vs clockwise"""
    # Helper definitions to get an ellipse
    major = 10
    minor = 5
    ellip = get_ellipse_coords(a=major,
                               b=minor,
                               x=minor,
                               y=5,
                               angle=0,
                               k=100)
    # obtain the centroid (corresponds to pos_x and pos_lat)
    cx, cy = centroid_of_polygon(ellip)

    v1 = get_volume(cont=ellip,
                    pos_x=cx,
                    pos_y=cy,
                    pix=1)

    # Turn contour around
    v2 = get_volume(cont=ellip[::-1, :],
                    pos_x=cx,
                    pos_y=cy,
                    pix=1)

    assert np.all(v1 == v2)
def test_volume():
    ds = new_dataset(retrieve_data("rtdc_data_hdf5_mask_contour.zip"))
    mask = [mi for mi in ds["mask"]]
    cont1 = [ci for ci in ds["contour"]]
    cont2 = get_contour(mask)

    kw = dict(pos_x=ds["pos_x"],
              pos_y=ds["pos_y"],
              pix=ds.config["imaging"]["pixel size"])

    v1 = get_volume(cont=cont1, **kw)
    v2 = get_volume(cont=cont2, **kw)

    assert np.allclose(v1, v2)
    cleanup()
Exemplo n.º 3
0
def features_from_mask(mask, pixel_size=0.34):
    """Compute dclab features from a binary mask image

    This essentially mimicks what Shape-In does with OpenCV.

    Parameters
    ----------
    mask: 2d boolean ndarray
        binary mask image
    pixel_size: float
        Detector pixel size [um]
    """
    cont_raw = get_contour(mask)
    # this is how Shape-In does things
    cont = cv2.convexHull(cont_raw)
    arc = cv2.arcLength(cont, True)
    mu = cv2.moments(cont, False)
    pos_x = (mu["m10"] / mu["m00"]) * pixel_size
    pos_y = (mu["m01"] / mu["m00"]) * pixel_size
    feats = {
        # scalar features
        "area_um": mu["m00"] * pixel_size**2,
        "deform": 1 - 2.0 * np.sqrt(np.pi * mu["m00"]) / arc,
        "inert_ratio_cvx": np.sqrt(mu["mu20"] / mu["mu02"]),
        "pos_x": pos_x,
        "pos_y": pos_y,
        "volume": get_volume(cont_raw, pos_x, pos_y, pixel_size),
        # image data
        "image": np.array(254 * mask, dtype=np.uint8),
        "mask": mask,
    }
    return feats
Exemplo n.º 4
0
def test_xpos():
    """xpos is not necessary to compute volume dense ellipse"""
    major = 10
    minor = 5
    ellip = get_ellipse_coords(a=major,
                               b=minor,
                               x=minor,
                               y=5.0,
                               angle=0,
                               k=100)
    cx, cy = centroid_of_polygon(ellip)
    # no lists
    v0 = get_volume(cont=ellip, pos_x=cx, pos_y=cy, pix=1)
    for cxi in np.linspace(0, 2 * cx, 10):
        vi = get_volume(cont=ellip, pos_x=cxi, pos_y=cy, pix=1)
        assert np.allclose(v0, vi)
Exemplo n.º 5
0
def test_shape():
    major = 10
    minor = 5
    ellip = get_ellipse_coords(a=major,
                               b=minor,
                               x=minor,
                               y=5.0,
                               angle=0,
                               k=100)
    cx, cy = centroid_of_polygon(ellip)
    # no lists
    volume = get_volume(cont=ellip, pos_x=cx, pos_y=cy, pix=1)
    assert isinstance(volume, float)

    volumelist = get_volume(cont=[ellip], pos_x=[cx], pos_y=[cy], pix=1)
    assert isinstance(volumelist, np.ndarray)
def test_xpos():
    """xpos is not necessary to compute volume dense ellipse"""
    major = 10
    minor = 5
    ellip = get_ellipse_coords(a=major,
                               b=minor,
                               x=minor,
                               y=5.0,
                               angle=0,
                               k=100)
    cx, cy = centroid_of_polygon(ellip)
    # no lists
    v0 = get_volume(cont=ellip,
                    pos_x=cx,
                    pos_y=cy,
                    pix=1)
    for cxi in np.linspace(0, 2 * cx, 10):
        vi = get_volume(cont=ellip,
                        pos_x=cxi,
                        pos_y=cy,
                        pix=1)
        assert np.allclose(v0, vi)
def test_shape():
    major = 10
    minor = 5
    ellip = get_ellipse_coords(a=major,
                               b=minor,
                               x=minor,
                               y=5.0,
                               angle=0,
                               k=100)
    cx, cy = centroid_of_polygon(ellip)
    # no lists
    volume = get_volume(cont=ellip,
                        pos_x=cx,
                        pos_y=cy,
                        pix=1)
    assert isinstance(volume, float)

    volumelist = get_volume(cont=[ellip],
                            pos_x=[cx],
                            pos_y=[cy],
                            pix=1)
    assert isinstance(volumelist, np.ndarray)
Exemplo n.º 8
0
def test_orientation():
    """counter-clockwise vs clockwise"""
    # Helper definitions to get an ellipse
    major = 10
    minor = 5
    ellip = get_ellipse_coords(a=major, b=minor, x=minor, y=5, angle=0, k=100)
    # obtain the centroid (corresponds to pos_x and pos_lat)
    cx, cy = centroid_of_polygon(ellip)

    v1 = get_volume(cont=ellip,
                    pos_x=cx,
                    pos_y=cy,
                    pix=1,
                    fix_orientation=True)

    # Turn contour around
    v2 = get_volume(cont=ellip[::-1, :],
                    pos_x=cx,
                    pos_y=cy,
                    pix=1,
                    fix_orientation=True)

    assert np.all(v1 == v2)
Exemplo n.º 9
0
def test_get_volume():
    # Helper definitions to get an ellipse
    major = 10
    minor = 5
    ellip = get_ellipse_coords(a=major, b=minor, x=minor, y=5, angle=0, k=100)
    # obtain the centroid (corresponds to pos_x and pos_lat)
    cx, cy = centroid_of_polygon(ellip)
    volume = get_volume(cont=[ellip],
                        pos_x=[cx],
                        pos_y=[cy],
                        pix=1,
                        fix_orientation=True)

    # Analytic solution for volume of ellipsoid:
    v = 4 / 3 * np.pi * major * minor**2
    msg = "Calculation of volume is faulty!"
    assert np.allclose(np.array(volume), np.array(v)), msg
Exemplo n.º 10
0
def test_get_volume():
    # Helper definitions to get an ellipse
    major = 10
    minor = 5
    ellip = get_ellipse_coords(a=major,
                               b=minor,
                               x=minor,
                               y=5,
                               angle=0,
                               k=100)
    # obtain the centroid (corresponds to pos_x and pos_lat)
    cx, cy = centroid_of_polygon(ellip)
    elliplist = []
    elliplist.append(ellip)
    volume = get_volume(cont=elliplist,
                        pos_x=[cx],
                        pos_y=[cy],
                        pix=1)

    # Analytic solution for volume of ellipsoid:
    V = 4 / 3 * np.pi * major * minor**2
    msg = "Calculation of volume is faulty!"
    assert np.allclose(np.array(volume), np.array(V)), msg