Obtain clusters#
Calculate clusters#
Extract clusters (‘islands’ of connected pixels with the same value).
import GooseEYE
import numpy as np
# generate image
img = GooseEYE.dummy_circles((500, 500), periodic=True)
# clusters
labels = GooseEYE.clusters(img, periodic=False)
# clusters, if the image is periodic
labels_periodic = GooseEYE.clusters(img, periodic=True)
#include <GooseEYE/GooseEYE.h>
#include <xtensor/xarray.hpp>
#include <xtensor/xio.hpp>
int main()
{
// generate image
auto I = GooseEYE::dummy_circles({100, 100}, true);
// clusters
auto labels = GooseEYE::clusters(I, false);
// clusters, if the image is periodic
auto labels_periodic = GooseEYE::clusters(I, true);
return 0;
}
Calculate clusters and centers#
import GooseEYE
import numpy as np
# generate image
img = GooseEYE.dummy_circles((500, 500), periodic=True)
# clusters
labels = GooseEYE.clusters(img, periodic=False)
names = np.unique(labels)
centers = GooseEYE.labels_centers(labels, names, periodic=False)
# clusters, if the image is periodic
labels_periodic = GooseEYE.clusters(img, periodic=True)
names_periodic = np.unique(labels_periodic)
centers_periodic = GooseEYE.labels_centers(labels_periodic, names_periodic, periodic=True)
#include <GooseEYE/GooseEYE.h>
#include <highfive/H5Easy.hpp>
#define MYASSERT(expr) MYASSERT_IMPL(expr, __FILE__, __LINE__)
#define MYASSERT_IMPL(expr, file, line) \
if (!(expr)) { \
throw std::runtime_error( \
std::string(file) + ':' + std::to_string(line) + \
": assertion failed (" #expr ") \n\t"); \
}
int main()
{
// generate image
auto I = GooseEYE::dummy_circles({500, 500}, true);
// clusters
auto labels = GooseEYE::clusters(I, false);
auto names = xt::unique(labels);
auto centers = GooseEYE::labels_centers(labels, names, false);
// clusters, if the image is periodic
auto labels_periodic = GooseEYE::clusters(I, true);
auto names_periodic = xt::unique(labels_periodic);
auto centers_periodic = GooseEYE::labels_centers(labels_periodic, names_periodic, true);
// check against previous versions
H5Easy::File data("clusters_centers.h5", H5Easy::File::ReadOnly);
MYASSERT(xt::all(xt::equal(I, H5Easy::load<decltype(I)>(data, "I"))));
MYASSERT(xt::all(xt::equal(labels, H5Easy::load<decltype(labels)>(data, "labels"))));
MYASSERT(xt::allclose(centers, H5Easy::load<decltype(centers)>(data, "centers")));
MYASSERT(xt::all(xt::equal(
labels_periodic, H5Easy::load<decltype(labels_periodic)>(data, "labels_periodic"))));
MYASSERT(xt::allclose(
centers_periodic, H5Easy::load<decltype(centers_periodic)>(data, "centers_periodic")));
return 0;
}
Dilate clusters (differently)#
import GooseEYE
import numpy as np
# generate image
img = np.zeros((21, 21), dtype="bool")
img[4, 4] = True
img[14, 15] = True
img[15, 15] = True
img[16, 15] = True
img[15, 14] = True
img[15, 16] = True
# clusters
C = GooseEYE.clusters(img)
# dilate
CD = GooseEYE.dilate(C)
Note
There is an additional example to show the effect of periodicity:
clusters_dilate_periodic.py
clusters_dilate_periodic.svg
#include <GooseEYE/GooseEYE.h>
#include <highfive/H5Easy.hpp>
#define MYASSERT(expr) MYASSERT_IMPL(expr, __FILE__, __LINE__)
#define MYASSERT_IMPL(expr, file, line) \
if (!(expr)) { \
throw std::runtime_error( \
std::string(file) + ':' + std::to_string(line) + \
": assertion failed (" #expr ") \n\t"); \
}
int main()
{
// generate image
xt::xarray<int> I = xt::zeros<int>({21, 21});
I(4, 4) = 1;
I(14, 15) = 1;
I(15, 15) = 1;
I(16, 15) = 1;
I(15, 14) = 1;
I(15, 16) = 1;
// clusters
auto C = GooseEYE::clusters(I);
// dilate
auto CD = GooseEYE::dilate(C);
// check against previous versions
H5Easy::File data("clusters_dilate.h5", H5Easy::File::ReadOnly);
MYASSERT(xt::all(xt::equal(I, H5Easy::load<decltype(I)>(data, "I"))));
MYASSERT(xt::all(xt::equal(C, H5Easy::load<decltype(C)>(data, "C"))));
MYASSERT(xt::all(xt::equal(CD, H5Easy::load<decltype(CD)>(data, "CD"))));
return 0;
}