66 template <
typename PixelType> [[nodiscard]] std::vector<PixelId> sort(
const ImagePtr<PixelType>& image)
const {
67 const int numPixels = image->
getSize();
68 std::vector<PixelId> orderedPixels(
static_cast<std::size_t
>(numPixels));
69 const PixelType* values = image->rawData();
71 if constexpr (!std::is_same_v<PixelType, std::uint8_t>) {
73 std::cout <<
"Sorting image with comparison sort, size: " << numPixels << std::endl;
75 std::iota(orderedPixels.begin(), orderedPixels.end(), 0);
77 std::stable_sort(orderedPixels.begin(), orderedPixels.end(), [&](
PixelId lhs,
PixelId rhs) { return values[lhs] < values[rhs]; });
79 std::stable_sort(orderedPixels.begin(), orderedPixels.end(), [&](
PixelId lhs,
PixelId rhs) { return values[lhs] > values[rhs]; });
83 std::cout <<
"Sorting uint8 image with counting sort, size: " << numPixels << std::endl;
86 int maxValue =
static_cast<int>(values[0]);
87 for (
int index = 1; index < numPixels; ++index) {
88 if (maxValue < values[index]) {
89 maxValue = values[index];
93 std::vector<std::uint32_t> counter(
static_cast<std::size_t
>(maxValue) + 1, 0);
95 for (
int index = 0; index < numPixels; ++index) {
96 ++counter[values[index]];
98 for (
int level = 1; level <= maxValue; ++level) {
99 counter[level] += counter[level - 1];
101 for (
int index = numPixels - 1; index >= 0; --index) {
102 orderedPixels[--counter[values[index]]] = index;
105 for (
int index = 0; index < numPixels; ++index) {
106 ++counter[maxValue - values[index]];
108 for (
int level = 1; level <= maxValue; ++level) {
109 counter[level] += counter[level - 1];
111 for (
int index = numPixels - 1; index >= 0; --index) {
112 orderedPixels[--counter[maxValue - values[index]]] = index;
116 return orderedPixels;
132 template <
typename PixelType>
133 [[nodiscard]] std::tuple<std::vector<PixelId>, std::vector<PixelId>,
int> buildTree(
const ImagePtr<PixelType>& image)
const {
134 std::vector<PixelId> orderedPixels = sort(image);
135 const PixelType* values = image->rawData();
137 const int numPixels = image->getSize();
138 std::vector<PixelId> unionParent(
static_cast<std::size_t
>(numPixels),
InvalidPixel);
139 std::vector<PixelId> pixelParent(
static_cast<std::size_t
>(numPixels),
InvalidPixel);
140 auto findRoot = [&](
PixelId pixel) {
141 while (unionParent[pixel] != pixel) {
142 unionParent[pixel] = unionParent[unionParent[pixel]];
143 pixel = unionParent[pixel];
148 for (
int index = numPixels - 1; index >= 0; --index) {
149 const PixelId pixel = orderedPixels[
static_cast<std::size_t
>(index)];
150 pixelParent[pixel] = pixel;
151 unionParent[pixel] = pixel;
152 for (
PixelId neighbor : detail::CommittedGridAccess::neighbors(adjacency_, pixel)) {
156 const PixelId neighborRoot = findRoot(neighbor);
157 if (pixel != neighborRoot) {
158 pixelParent[neighborRoot] = pixel;
159 unionParent[neighborRoot] = pixel;
165 for (
int index = 0; index < numPixels; ++index) {
166 const PixelId pixel = orderedPixels[
static_cast<std::size_t
>(index)];
167 const PixelId parent = pixelParent[pixel];
168 if (values[pixelParent[parent]] == values[parent]) {
169 pixelParent[pixel] = pixelParent[parent];
171 if (pixelParent[pixel] == pixel || values[pixelParent[pixel]] != values[pixel]) {
176 return {std::move(pixelParent), std::move(orderedPixels), numNodes};
197 template <AltitudeValue T>
199 const std::vector<PixelId>& orderedPixels,
int numNodes) {
200 const MorphologicalTreeKind kind = isMaxTree_ ? MorphologicalTreeKind::MaxTree : MorphologicalTreeKind::MinTree;
201 const GridDomain2D gridDomain{image->getNumRows(), image->getNumColumns()};
202 const auto* imageValues = image->rawData();
203 std::vector<NodeId> nodeParent(
static_cast<std::size_t
>(numNodes));
204 std::vector<T> nodeValuation(
static_cast<std::size_t
>(numNodes));
205 std::vector<NodeId> smallestNode(
static_cast<std::size_t
>(image->getSize()));
208 for (
const PixelId pixel : orderedPixels) {
209 const PixelId parent = pixelParent[pixel];
210 if (pixel == parent || imageValues[pixel] != imageValues[parent]) {
211 assert(pixel != parent || idNode == 0);
212 smallestNode[pixel] = idNode;
213 nodeParent[idNode] = pixel == parent ? idNode : smallestNode[parent];
214 nodeValuation[idNode] = imageValues[pixel];
217 smallestNode[pixel] = smallestNode[parent];
221 assert(idNode == numNodes);
222 return makeNativeHierarchyOwned<T>(std::move(nodeParent), std::move(smallestNode), std::move(nodeValuation), 0, gridDomain,
233 : adjacency_(std::move(adjacency)), isMaxTree_(isMaxTree) {}
245 assert(adjacency_.getNumRows() == image->getNumRows() && adjacency_.getNumColumns() == image->getNumColumns());
246 auto [pixelParent, orderedPixels, numNodes] = buildTree(image);
247 return computeIntrinsicAttributes(image, pixelParent, orderedPixels, numNodes);
Shape metadata optionally attached to the pixel domain.