Example #1
0
    def test_inverse_transform(self, example_shp, expected):
        x, y, _ = spatial_efd.ProcessGeometryNorm(example_shp[2])
        coeffs = spatial_efd.CalculateEFD(x, y, 10)
        a, b = spatial_efd.inverse_transform(coeffs)

        ntest.assert_almost_equal(a[:5], expected['inverse_transform']['a'])
        ntest.assert_almost_equal(b[:5], expected['inverse_transform']['b'])
Example #2
0
 def test_save_shapefile(self, example_shp, tmpdir):
     x, y, _ = spatial_efd.ProcessGeometry(example_shp[1])
     coeffs = spatial_efd.CalculateEFD(x, y, 10)
     shape = spatial_efd.generateShapefile()
     shape = spatial_efd.writeGeometry(coeffs, x, y, 4, shape, 1)
     spatial_efd.saveShapefile(tmpdir.strpath, shape, prj=None)
     assert os.path.isfile('{}.shp'.format(tmpdir))
Example #3
0
 def test_write_geometry_prj(self, example_shp, tmpdir, shp_paths):
     x, y, _ = spatial_efd.ProcessGeometry(example_shp[1])
     coeffs = spatial_efd.CalculateEFD(x, y, 10)
     shape = spatial_efd.generateShapefile(tmpdir.strpath, prj=shp_paths[1])
     shape = spatial_efd.writeGeometry(coeffs, x, y, 4, shape, 1)
     assert os.path.isfile('{}.shp'.format(tmpdir))
     assert os.path.isfile('{}.prj'.format(tmpdir))
Example #4
0
 def test_plot_comparison(self, example_shp, tmpdir):
     matplotlib.pyplot.clf()
     x, y, _ = spatial_efd.ProcessGeometry(example_shp[0])
     coeffs = spatial_efd.CalculateEFD(x, y, 10)
     ax = spatial_efd.InitPlot()
     spatial_efd.plotComparison(ax, coeffs, 10, x, y)
     spatial_efd.SavePlot(ax, 10, tmpdir, 'png')
     assert os.path.isfile('{0}_10.png'.format(tmpdir))
Example #5
0
    def test_normalize_efd(self, example_shp, expected):
        x, y, _ = spatial_efd.ProcessGeometryNorm(example_shp[0])
        coeffs = spatial_efd.CalculateEFD(x, y, 10)
        coeffs, rotation = spatial_efd.normalize_efd(coeffs)

        ntest.assert_almost_equal(coeffs[9],
                                  expected['normalize_efd']['coeffs'])
        assert pytest.approx(rotation) == expected['normalize_efd']['rotation']
Example #6
0
 def test_plot_comparison_norm(self, example_shp, tmpdir):
     x, y, _ = spatial_efd.ProcessGeometry(example_shp[1])
     coeffs = spatial_efd.CalculateEFD(x, y, 10)
     coeffs, rotation = spatial_efd.normalize_efd(coeffs,
                                                  size_invariant=False)
     ax = spatial_efd.InitPlot()
     spatial_efd.plotComparison(ax, coeffs, 7, x, y, rotation=rotation)
     spatial_efd.SavePlot(ax, 7, tmpdir, 'png')
     assert os.path.isfile('{0}_7.png'.format(tmpdir))
Example #7
0
 def test_plotting_savefig(self, example_shp, tmpdir):
     matplotlib.pyplot.clf()
     x, y, _ = spatial_efd.ProcessGeometryNorm(example_shp[2])
     coeffs = spatial_efd.CalculateEFD(x, y, 10)
     a, b = spatial_efd.inverse_transform(coeffs)
     ax = spatial_efd.InitPlot()
     spatial_efd.PlotEllipse(ax, a, b, color='k', width=1.)
     spatial_efd.SavePlot(ax, 5, tmpdir, 'png')
     assert os.path.isfile('{0}_5.png'.format(tmpdir))
Example #8
0
    def test_average_sd(self, example_shp, expected):
        coeffsList = []

        for i in range(3):
            x, y, _ = spatial_efd.ProcessGeometryNorm(example_shp[i])
            coeffsList.append(spatial_efd.CalculateEFD(x, y, 10))

        avg = spatial_efd.AverageCoefficients(coeffsList)
        sd = spatial_efd.AverageSD(coeffsList, avg)
        ntest.assert_almost_equal(sd[3], expected['average_sd']['sd'])
def extract_cnts_coeffs_from_mask(mask, harmonic=10, size_invariant=True):
    coeffs = collections.OrderedDict()
    cnts = collections.OrderedDict()
    rots = collections.OrderedDict()

    if isinstance(mask, np.ndarray):
        labels = np.unique(mask)[1:]
    elif isinstance(mask, list):
        labels = mask

    for idx, label in enumerate(tqdm(labels)):
        if isinstance(mask, np.ndarray):
            binary = (mask == label).astype(np.uint8)
        elif isinstance(mask, list):
            binary = label.astype(np.uint8)

        (
            num_labels,
            labels,
            stats,
            centroids,
        ) = cv2.connectedComponentsWithStats(binary, connectivity=4)

        if len(np.unique(labels)) == 2:
            # make area size flexible
            contours, hierarchies = cv2.findContours(labels.astype(np.uint8),
                                                     cv2.RETR_TREE,
                                                     cv2.CHAIN_APPROX_NONE)
            if not np.all(hierarchies, -1).all():
                print(
                    "hierarchical contours found for {}. Only selecting the outermost contours"
                    .format(idx))
                # continue
            for n, (contour,
                    hierarchy) in enumerate(zip(contours, hierarchies)):
                if hierarchy[-1][-1] == -1:
                    contour = contour.squeeze()
                    if len(contour.shape) == 2:
                        raw = spatial_efd.CalculateEFD(contour[:, 0],
                                                       contour[:, 1],
                                                       harmonics=harmonic)
                        normalized, rotation = spatial_efd.normalize_efd(
                            raw, size_invariant=size_invariant)
                        coeffs[idx] = normalized
                        cnts[idx] = contour
                        rots[idx] = rotation
                    else:
                        print(
                            "shape only 1 pixel long, skipping {}".format(idx))
                else:
                    pass
                    # print(hierarchy)
        else:
            print("more than one compoment found, skipping {}".format(idx))
    return coeffs, cnts, rots
Example #10
0
    def test_write_prj_wrong(self, example_shp, tmpdir, shp_paths,
                             warn_wrong_prj):
        x, y, _ = spatial_efd.ProcessGeometry(example_shp[1])
        coeffs = spatial_efd.CalculateEFD(x, y, 10)

        with warnings.catch_warnings(record=True) as w:
            spatial_efd.generateShapefile(tmpdir.strpath, prj=shp_paths[0])

            assert os.path.isfile('{0}.shp'.format(tmpdir))
            assert not os.path.isfile('{0}.prj'.format(tmpdir))
            assert len(w) == 1
            assert issubclass(w[0].category, UserWarning)
            assert clean_warning(w[0].message) == warn_wrong_prj
Example #11
0
 def test_write_geometry(self, example_shp):
     x, y, _ = spatial_efd.ProcessGeometry(example_shp[1])
     coeffs = spatial_efd.CalculateEFD(x, y, 10)
     shape = spatial_efd.generateShapefile()
     shape = spatial_efd.writeGeometry(coeffs, x, y, 4, shape, 1)
     assert isinstance(shape, shp.Writer)
Example #12
0
 def test_fourier_power(self, example_shp):
     x, y, _ = spatial_efd.ProcessGeometryNorm(example_shp[2])
     coeffs = spatial_efd.CalculateEFD(x, y, 500)
     n = spatial_efd.FourierPower(coeffs, x)
     assert n == 19
Example #13
0
 def test_calculate_efd(self, example_shp, expected):
     x, y, _ = spatial_efd.ProcessGeometryNorm(example_shp[2])
     coeffs = spatial_efd.CalculateEFD(x, y, 10)
     ntest.assert_almost_equal(coeffs[6],
                               expected['calculate_efd']['coeffs'])