#include <iostream>
#include <vigra/multi_array.hxx>
#include <vigra/multi_resize.hxx>
#include <vigra/colorconversions.hxx>
#include <vigra/multi_convolution.hxx>
#include <vigra/multi_watersheds.hxx>
#include <vigra/multi_gridgraph.hxx>
#include <vigra/accumulator.hxx>
#include <vigra/adjacency_list_graph.hxx>
#include <vigra/graph_algorithms.hxx>
#include <vigra/hierarchical_clustering.hxx>
#include <vigra/metrics.hxx>
int main (int argc, char ** argv)
{
float sigmaGradMag = 3.0f;
float beta = 0.5f;
float wardness = 0.8f;
int numClusters = 30;
if(argc != 3)
{
std::cout << "Usage: " << argv[0] << " infile outfile" << std::endl;
std::cout << "(supported formats: " << impexListFormats() << ")" << std::endl;
std::cout << "(only color images)" << std::endl;
return 1;
}
try
{
vigra_precondition(info.
numBands() == 3,
"an RGB image is required.");
imageArrayLab(info.
shape());
importImage(info, imageArrayRGB);
gaussianGradientMagnitude(imageArrayLab, gradMag, sigmaGradMag);
unsigned int max_label =
watershedsMultiArray(gradMag, labelArray, DirectNeighborhood,
resizeMultiArraySplineInterpolation(imageArrayRGB, imageArrayBig);
regionImageToCrackEdgeImage(labelArray, imageArrayBig,
ImageGraph imageGraph(labelArray.shape());
RAG rag;
RAG::EdgeMap<std::vector<ImageGraph::Edge>> affiliatedEdges(rag);
makeRegionAdjacencyGraph(imageGraph, labelArray, rag, affiliatedEdges);
RAG::EdgeMap<float> edgeWeights(rag),
edgeLengths(rag);
for(RAG::EdgeIt rag_edge(rag); rag_edge != lemon::INVALID; ++rag_edge)
{
for(unsigned int k = 0; k < affiliatedEdges[*rag_edge].size(); ++k)
{
auto const & grid_edge = affiliatedEdges[*rag_edge][k];
auto start = imageGraph.u(grid_edge),
end = imageGraph.v(grid_edge);
double grid_edge_gradient = 0.5 * (gradMag[start] + gradMag[end]);
edgeWeights[*rag_edge] += grid_edge_gradient;
}
edgeLengths[*rag_edge] = affiliatedEdges[*rag_edge].size();
edgeWeights[*rag_edge] /= edgeLengths[*rag_edge];
}
using namespace acc;
AccumulatorChainArray<CoupledArrays<2, TinyVector<float, 3>, unsigned int>,
Select<DataArg<1>, LabelArg<2>,
Count, Mean> >
features;
extractFeatures(imageArrayLab, labelArray, features);
RAG::NodeMap<TinyVector<float, 3>> meanColor(rag);
RAG::NodeMap<unsigned int> regionSize(rag);
for(unsigned int k=0; k<=max_label; ++k)
{
meanColor[k] = get<Mean>(features, k);
regionSize[k] = get<Count>(features, k);
}
RAG::NodeMap<unsigned int> nodeLabels(rag);
hierarchicalClustering(rag,
edgeWeights, edgeLengths, meanColor, regionSize,
nodeLabels,
.nodeFeatureImportance(beta)
.sizeImportance(wardness)
.nodeFeatureMetric(metrics::L2Norm)
);
transformMultiArray(labelArray, labelArray,
[&nodeLabels](unsigned int oldlabel)
{
return nodeLabels[oldlabel];
});
regionImageToCrackEdgeImage(labelArray, imageArrayBig,
exportImage(imageArrayBig, argv[2]);
}
catch (std::exception & e)
{
std::cout << e.what() << std::endl;
return 1;
}
return 0;
}
undirected adjacency list graph in the LEMON API
Definition: adjacency_list_graph.hxx:228
Options object for hierarchical clustering.
Definition: hierarchical_clustering.hxx:647
Define a grid graph in arbitrary dimensions.
Definition: multi_gridgraph.hxx:1429
Argument object for the function importImage().
Definition: imageinfo.hxx:391
MultiArrayShape< 2 >::type shape() const
Main MultiArray class containing the memory management.
Definition: multi_array.hxx:2477
Convert linear (raw) RGB into perceptual uniform CIE L*a*b*.
Definition: colorconversions.hxx:1589
Class for a single RGB value.
Definition: rgbvalue.hxx:128
Options object for watershed algorithms.
Definition: watersheds.hxx:775
image import and export functions